php data、time函数
大道至简
date/time 函数允许您提取并格式化服务器上的日期和时间。
Runtime配置
PHP Date / Time 函数
1、checkdate()
checkdate() 函数验证一个格里高里日期。
如果指定的值合法,则该函数返回 true,否则返回 false。
日期在下列情况下为合法:
month 介于且包括 1 - 12
Day 的值在给定的 month 所应该具有的天数范围之内,闰年已经考虑进去了。
year 介于且包括 1 到 32767
例子
<?php
vardump(checkdate(12,31,2000));
vardump(checkdate(2,29,2003));
var_dump(checkdate(2,29,2004));
?>
输出:
bool(true)
bool(false)
bool(true)
2、datedefaulttimezone_get()
例子
<?php
echo(datedefaulttimezone_get());
?>
输出:
PRC
3、datedefaulttimezone_set()
datedefaulttimezoneset() 函数设置用在脚本中所有日期/时间函数的默认时区。
例子
<?php
echo(datedefaulttimezoneset("Europe/Paris"));
?>
输出:
1
合法时区的列表:http://www.php.net/manual/en/timezones.php
4、date_sunrise() 函数
datesunrise() 函数返回指定的日期与地点的日出时间。
datesunrise(timestamp,format,latitude,longitude,zenith,gmt_offset)
例子
<?php
//计算葡萄牙里斯本的日出时间
//Latitude: 北纬 38.4 度
//Longitude: 西经 9 度
//Zenith ~= 90
//offset: +1 GMT
echo("Date: " . date("D M d Y") . "
");
echo("Sunrise time: ");
echo(datesunrise(time(),SUNFUNCSRET_STRING,38.4,-9,90,1));
?>
输出:
Date: Tue Jan 24 2006
Sunrise time: 08:52
5、date_sunset() 函数
同上
6、date() 函数
date() 函数格式化一个本地时间/日期
data(返回格式,时间戳);
例子
<?php
echo("Result with date():");
echo(date("l") );
echo(date("l dS \of F Y h:i:s A") );
echo("Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975)));
echo(date(DATE_RFC822) );
echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) );
echo("Result with gmdate():");
echo(gmdate("l") );
echo(gmdate("l dS \of F Y h:i:s A") );
echo("Oct 3,1975 was on a ".gmdate("l", mktime (0,0,0,10,3,1975)));
echo(gmdate(DATE_RFC822));
echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)));
?>
输出:
Result with date():
Tuesday
Tuesday 24th of January 2006 02:41:22 PM
Oct 3,1975 was on a Friday
Tue, 24 Jan 2006 14:41:22 CET
1975-10-03T00:00:00+0100
Result with gmdate():
Tuesday
Tuesday 24th of January 2006 01:41:22 PM
Oct 3,1975 was on a Thursday
Tue, 24 Jan 2006 13:41:22 GMT
1975-10-02T23:00:00+0000
注意:mktime() 函数返回一个日期的 Unix 时间戳。
7、getdate() 函数
getdate() 函数取得日期/时间信息。
8、gettimeofday() 函数
gettimeofday() 函数返回一个包含当前时间信息的数组。