欢迎访问宙启技术站
智能推送

PHP时间日期函数操作指南

发布时间:2023-06-26 14:42:55

PHP中的时间和日期函数非常丰富,可以用于获取和处理各种时间和日期格式。在本篇文章中,我们将介绍常用的时间日期函数,并提供一些实例,以帮助您更好地了解和使用它们。

1. date()函数

date()函数用于获取当前时间或指定时间的格式化字符串。该函数的第一个参数是格式化字符串,用于确定返回值的格式。以下是一些常用的格式化字符串和它们的输出示例:

| 格式化字符串 | 输出示例 |

| ------------ | ------------- |

| Y | 2021 |

| m | 03 |

| d | 08 |

| H | 13 |

| i | 45 |

| s | 59 |

| D | Mon |

| M | Mar |

| jS | 8th |

| Y-m-d H:i:s | 2021-03-08 13:45:59 |

以下是一些使用date()函数的示例:

// 获取当前年份
$current_year = date('Y');

// 获取当前月份
$current_month = date('m');

// 获取完整的日期和时间
$current_date_time = date('Y-m-d H:i:s');

// 格式化指定的日期和时间
$timestamp = strtotime('2020-12-31');
$formatted_date = date('jS M Y', $timestamp);

echo $current_year . '<br>';
echo $current_month . '<br>';
echo $current_date_time . '<br>';
echo $formatted_date . '<br>';

输出:

2021
03
2021-03-08 13:45:59
31st Dec 2020

2. time()函数

time()函数用于获取当前时间的时间戳。该函数没有参数,直接调用即可。

以下是一个使用time()函数的示例:

$current_timestamp = time();
echo $current_timestamp;

输出:

1615175059

3. strtotime()函数

strtotime()函数用于将日期和时间字符串转换为时间戳。该函数的第一个参数是待转换的日期和时间字符串,格式可以是绝大部分通用的日期时间格式,例如"2020-12-31"或"next Monday",第二个参数是一个可选的时间基准值。

以下是一些使用strtotime()函数的示例:

// 将日期字符串转换为时间戳
$date_string = '2020-12-31';
$timestamp = strtotime($date_string);
echo $timestamp . '<br>';

// 将时间字符串转换为时间戳
$time_string = '06:30:00';
$timestamp = strtotime($time_string);
echo $timestamp . '<br>';

// 将相对日期字符串转换为时间戳
$relative_date_string = 'next Monday';
$timestamp = strtotime($relative_date_string);
echo $timestamp . '<br>';

输出:

1609353600
1615186200
1615353600

4. mktime()函数

mktime()函数用于根据指定的时、分、秒、月、日、年等参数构建一个时间戳。该函数的第一个参数是小时数,第二个参数是分钟数,第三个参数是秒数,第四个参数是月份,第五个参数是日期,第六个参数是年份。

以下是一个使用mktime()函数的示例:

$timestamp = mktime(0, 0, 0, 12, 31, 2021);
echo date('Y-m-d', $timestamp);

输出:

2021-12-31

5. checkdate()函数

checkdate()函数用于检查指定的年、月、日是否合法。该函数的第一个参数是月份,第二个参数是日期,第三个参数是年份。

以下是一个使用checkdate()函数的示例:

$is_valid = checkdate(2, 29, 2021);
echo $is_valid ? '合法' : '不合法';

输出:

不合法

6. strtotime()、date()和strtotime()联合使用

strtotime()函数可以解析绝大部分日期时间格式,因此可以与date()和strtotime()函数联合使用,实现更加复杂的时间日期操作。以下是一些使用这些函数联合使用的示例:

// 获取2021年3月15日的前一天日期
$date_string = '2021-03-15';
$timestamp = strtotime($date_string);
$timestamp -= 24 * 3600;
echo date('Y-m-d', $timestamp) . '<br>';

// 获取2021年3月1日到2021年3月31日之间的日期列表
$start_timestamp = strtotime('2021-03-01');
$end_timestamp = strtotime('2021-03-31');
for ($i = $start_timestamp; $i <= $end_timestamp; $i += 24 * 3600) {
    echo date('Y-m-d', $i) . '<br>';
}

// 获取从现在开始1个月后的日期
$now_timestamp = time();
$one_month_later_timestamp = strtotime('+1 month', $now_timestamp);
echo date('Y-m-d', $one_month_later_timestamp) . '<br>';

输出:

2021-03-14
2021-03-01
2021-03-02
2021-03-03
2021-03-04
2021-03-05
2021-03-06
2021-03-07
2021-03-08
2021-03-09
2021-03-10
2021-03-11
2021-03-12
2021-03-13
2021-03-14
2021-03-15
2021-03-16
2021-03-17
2021-03-18
2021-03-19
2021-03-20
2021-03-21
2021-03-22
2021-03-23
2021-03-24
2021-03-25
2021-03-26
2021-03-27
2021-03-28
2021-03-29
2021-03-30
2021-03-31
2021-04-08

总结

本篇文章介绍了PHP中的常用时间日期函数及其使用方法,包括date()、time()、strtotime()、mktime()和checkdate()。通过这些函数的使用,我们可以更加灵活地操作时间和日期,完成一些常见的时间处理任务。