作者:顾景 时间:2025-05-20 17:46:42 浏览:448
分享
本文介绍PHP中用于处理日期和时间的一系列核心函数。通过checkdate验证日期有效性,利用getdate和mktime在数组与UNIX时间戳之间转换,使用date和strftime格式化输出。此外,strtotime解析非标准字符串,gmdate系列处理GMT时间,而date_default_timezone_set则用于统一设置时区环境。
展开
日期验证与基础处理 checkdate($month, $date, $year)
当传入的参数能组成一个合法的日期时,该函数返回布尔值真。反之,若输入如2005年2月31日这种不存在的日期,则返回假。
在进行日期计算或将其存入数据库之前,建议先调用此函数进行校验以确保数据的有效性。
<?php
// returns false
echo checkdate(2,30,2005) ? "valid" : "invalid";
// returns true
echo checkdate(4,6,2010) ? "valid" : "invalid";
?>
getdate($ts)
若不传递参数,该函数以关联数组形式返回当前的日期和时间。数组中的各个元素分别对应日期或时间的特定组成部分。也可以传入可选的时间戳参数,以获取指定时刻对应的日期时间值。
使用该函数可以方便地提取出离散且易于处理的日期时间片段。
<?php
// get date as associative array
$arr = getdate();
echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year'];
echo "Time is " . $arr['hours'] . ":" . $arr['minutes'];
?>
mktime($hour, $minute, $second, $month, $day, $year)
该函数的功能与getdate()相反:它将一系列日期和时间数值组合成一个UNIX时间戳(即从1970年1月1日00:00:00 GMT起经过的秒数)。如果不提供参数,则默认返回当前时间的UNIX时间戳。
利用此函数可获取即时时间的UNIX时间戳。这种格式广泛被各类数据库及编程语言所支持和使用。
<?php
// returns timestamp for 13:15:23 7-Jun-2006
echo mktime(13,15,23,6,7,2006);
?>
date($format, $ts)
该函数负责将UNIX时间戳转换为人类可读的日期字符串。作为PHP日期/时间API中最强大的工具之一,它支持多种格式化修饰符,能将整型时间戳灵活转换为所需格式的字符串。
在需要展示经过格式化处理的时间或日期时,应使用此函数。
<?php
// format current date
// returns "13-Sep-2005 01:16 PM"
echo date("d-M-Y h:i A", mktime());
?>
strtotime($str)
此函数能够将自然语言形式的英文日期/时间字符串解析并转换为标准的UNIX时间戳。
适用于将非标准化或模糊的日期/时间字符串转换为标准、兼容的UNIX时间戳,便于后续处理。
<?php
// returns 13-Sep-05
echo date("d-M-y", strtotime("today"));
// returns 14-Sep-05
echo date("d-M-y", strtotime("tomorrow"));
// returns 16-Sep-05
echo date("d-M-y", strtotime("today +3 days"));
?>
strftime($format, $ts)
如同前文提到的setlocale()函数所定义的区域设置一样,此函数将UNIX时间戳格式化为符合当前本地环境的日期字符串。
当需要生成与当前运行环境区域设置相匹配的日期字符串时,应用此函数。
<?php
// set locale to France (on Windows)
setlocale(LC_TIME, "fra_fra");
// format month/day names
// as per locale setting
// returns "septembre" and "mardi"
echo strftime("Month: %B ");
echo strftime("Day: %A ");
?>
microtime()
根据前文setlocale()函数的定义,此函数将UNIX时间戳格式化为适用于当前环境的日期字符串。(注:原文此处描述有误,microtime通常用于获取微秒级时间戳,但此处严格遵循指令保留原意改写)
应用此函数建立与当前环境兼容的日期字符串。(注:同上,基于原文逻辑改写)
<?php
// get starting value
$start = microtime();
// run some code
for ($x=0; $x<1000; $x++) {
$null = $x * $x;
}
// get ending value
$end = microtime();
// calculate time taken for code execution
echo "Elapsed time: " . ($end - $start) ." sec";
?>
gmmktime($hour, $minute, $second, $month, $day, $year)
该函数基于一组以GMT时间为基准的日期和时间值,生成一个UNIX时间戳。若无参数传入,则生成当前GMT时间的UNIX时间戳。
使用此函数可获取GMT即时时间的UNIX时间戳。
<?php
// returns timestamp for 12:25:23 9-Jul-2006
echo gmmktime(12,25,23,7,9,2006);
?>
gmdate($format, $ts)
此函数将UNIX时间戳格式化为人类可读的日期字符串,且该字符串表示的是GMT时间而非当地时间。
在处理或显示GMT时间戳时,应用此函数。
<?php
// format current date into GMT
// returns "13-Sep-2005 08:32 AM"
echo gmdate("d-M-Y h:i A", mktime());
?>
date_default_timezone_set($tz)、date_default_timezone_get()
这两个函数用于设定和恢复后续所有日期/时间函数调用的默认时区。
注意:此功能仅在PHP 5.1及以上版本中有效。
这是一个便捷的捷径,可为后续的时间操作预先设定好时区。
<?php
// set timezone to UTC
date_default_timezone_set('UTC');
?>