Skip to main content

日期时间函数

简介

Date/Time 函数

日期和时间信息在 PHP 内部是以 64 位数字存储的, 它可以覆盖当前时间前后 2920 亿年的时间,这个范围之广,足以满足现有应用的实际需求。

Note: 需要注意的是, 这些函数都是依赖服务器的区域设置的, 所以在使用它们的时候,要考虑夏令时 (例如:使用 $date = strtotime('+7 days', $date) 而不是 $date += 7*24*60*60) 和闰年的情况。

checkdate

验证一个格利高利日期,通常用来验证用户输入日期的合法性

说明

checkdate ( int $month , int $day , int $year ) : bool

​ 检查由参数构成的日期的合法性。如果每个参数都正确定义了则会被认为是有效的。

参数

month

​ month 的值是从 1 到 12。

day

Day 的值在给定的 month 所应该具有的天数范围之内,闰年已经考虑进去了。

year

​ year 的值是从 1 到 32767。

返回值

​ 如果给出的日期有效则返回 TRUE,否则返回 FALSE


date_default_timezone_get

取得一个脚本中所有日期时间函数所使用的默认时区

<?php
echo date_default_timezone_get();

date_default_timezone_set

设定用于一个脚本中所有日期时间函数的默认时区

<?php
date_default_timezone_set('Asia/Shanghai');

查看PHP支持的所有时区列表


date

格式化一个本地时间/日期

说明

date ( string $format [, int $timestamp ] ) : string

​ 返回将整数 timestamp 按照给定的格式字串而产生的字符串。如果没有给出时间戳则使用本地当前时间。换句话说,timestamp 是可选的,默认值为 time()

常用format字符示例
format字符描述返回值示例
d一个月中的第几天,有前导0的2位数字01到31
m带有前导0的数字表示的月份01到12
Y4位数字的年份示例:1998或2020
y2位数字的年份示例:98或20
H小时,24小时制,有前导0的2位数字00到23
h小时,12小时制,有前导0的2位数字01到12
i分钟,有前导0的2位数字00到59
s秒,有前导0的2位数字00到59
l星期几,英文全称Sunday到Saturday
F月份英文全拼January到December
D3个字符表示的星期几Mon到Sun
M3个字符表示的月份的英文简拼从Jan到Dec
t给定的月份中包含多少天28到31
L是否位闰年如果是闰年,则返回1,反之返回0

详情参见官网

返回值

​ 返回格式化后的日期时间的字符串表达。 如果 timestamp 参数不是一个有效数值,则返回 FALSE 并引发 E_WARNING 级别的错误。


getdate

取得日期/时间信息

说明

getdate ([ int $timestamp = time() ] ) : array

​ 返回一个根据 timestamp 得出的包含有日期信息的关联数组 array。如果没有给出时间戳则认为是当前本地时间。

范例

Example #1 getdate() 例子

<?php
$today = getdate();
print_r($today);
?>

以上例程的输出类似于:

Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)

localtime

取得本地时间

说明

localtime ([ int $timestamp = time() [, bool $is_associative = false ]] ) : array

localtime() 函数返回一个数组,其结构和 C 函数调用返回的完全一样。

参数

timestamp

​ 可选的 timestamp 参数是一个 integer 的 Unix 时间戳,如未指定,参数值默认为当前本地时间。也就是说,其值默认为 time() 的返回值。

is_associative

​ 如果设为 FALSE 或未提供则返回的是普通的数字索引数组。如果该参数设为 TRUElocaltime() 函数返回包含有所有从 C 的 localtime 函数调用所返回的不同单元的关联数组。

范例

Example #1 localtime() 例子

<?php
$localtime = localtime();
$localtime_assoc = localtime(time(), true);
print_r($localtime);
print_r($localtime_assoc);
?>

以上例程的输出类似于:

Array
(
[0] => 24
[1] => 3
[2] => 19
[3] => 3
[4] => 3
[5] => 105
[6] => 0
[7] => 92
[8] => 1
)

Array
(
[tm_sec] => 24
[tm_min] => 3
[tm_hour] => 19
[tm_mday] => 3
[tm_mon] => 3
[tm_year] => 105
[tm_wday] => 0
[tm_yday] => 92
[tm_isdst] => 1
)

microtime

返回当前Unix时间戳和微秒数

microtime ([ bool $get_as_float ] ) : mixed

microtime() 当前 Unix 时间戳以及微秒数。本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。

​ 如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。

​ 如果给出了 get_as_float 参数并且其值等价于 TRUEmicrotime() 将返回一个浮点数。


mktime

取得一个日期的Unix时间戳


strtotime

将任何字符串的日期时间描述解析位Unix时间戳

strtotime ( string $time [, int $now = time() ] ) : int

返回值

成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1

示例

Example #1 strtotime() 例子

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>

time

返回当前的Unix时间戳

返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。