中山php|最优网络中山做网站 中山php建站

最优良人

2011/08/19 at 16:56

php时间日期函数date,getdate,strtotime,strftime,strptime,time,mktime,microtime汇总对比

date — 格式化一个本地时间/日期(把时间戳变成文本格式)
string date ( string $format [, int $timestamp ] )
timestamp 是可选的,默认值为 time()。
$today = date("H:i:s"); // 17:16:17

getdate — 取得日期/时间信息(把时间戳的信息存到数组)
array getdate ([ int $timestamp ] )
<?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
)

strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 (把文本格式变成时间戳)
int strtotime ( string $time [, int $now ] )
函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。
<?php
echo strtotime("2011-9-9"), "\n";
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";
?>
strtotime的第一个参数可以是我们常见的英文时间格式,比如"2008-8-20"或"10 September 2000"等等。也可以是以参数now为基准的时间描述,比如"+1 day"等等。

下面是后一种方式的可使用参数清单,其中"当前时间"是指strtotime第二个参数now的值,默认为当前时间。

1. 月,日英文名及其常用缩写清单:
january,february,march,april,may,june,july,august,september,sept,october,november,december,
sunday,monday,tuesday,tues,wednesday,wednes,thursday,thur,thurs,friday,saturday

2. 时间参数和祥细描述:
am: the time is before noon 上午
pm: the time is noon or later 下午
year: one year; for example, "next year" 年,比如"next year"代表明年
month: one month; for example, "last month" 月,比如"last month"代表上一月
fortnight: two weeks; for example, "a fortnight ago" 两周,比如"a fortnight ago"代表两周前
week: one week 周
day: a day 天
hour: an hour 小时
minute: a minute 分钟
min: same as minute 同"minute"
second: a second 秒
sec: same as second 同"second"

3.相关和顺序说明:
+n/-n:以当前时间算,加个减指定的时间,比如"+1 hour"是指当前时间加一小时
ago:time relative to now; such as "24 hours ago"  以当前时间往前算,比如"24 hours ago"代表"24小时前"
tomorrow: 24 hours later than the current date and time 以当前时间(包括日期和时间)为标准,明天同一时间
yesterday: 24 hours earlier than the current date and time 以当前时间(包括日期和时间)为标准,昨天同一时间
today: the current date and time 当前时间(包括日期和时间)
now: the current date and time 当前时间(包括日期和时间)
last: modifier meaning "the preceding"; for example, "last tuesday" 代表"上一个",比如"last tuesday"代表"上周二同一时间"
this: the given time during the current day or the next occurrence of the given time; for example, "this 7am" gives the timestamp for 07:00 on the current day, while "this week" gives the timestamp for one week from the current time 当天的指定时间或下面一个时间段的时间戳,比如"this 7am"给出当天7:00的时间戳,而"this week"给出的是从当前时间开始的一整周的时间戳,也就是当前时间(经本人测试:strtotime('this week')=strtotime('now'));
next: modifier meaning the current time value of the subject plus one; for example, "next hour" 当前时间加上指定的时间,比如"next hour"是指当前时间加上一小时,即加3600

first: ordinal modifier, esp. for months; for example, "May first" (actually, it's just the same as next)
third: see first (note that there is no "second" for ordinality, since that would conflict with the second time value)
fourth: see first
fifth: see first
sixth: see first
seventh: see first
eighth: see first
ninth: see first
tenth: see first
eleventh: see first
twelfth: see first

4. 时区描述:
gmt: Greenwich Mean Time
ut: Coordinated Universal Time
utc: same as ut
wet: Western European Time
bst: British Summer Time
wat: West Africa Time
at: Azores Time
ast: Atlantic Standard Time
adt: Atlantic Daylight Time
est: Eastern Standard Time
edt: Eastern Daylight Time
cst: Central Standard Time
cdt: Central Daylight Time
mst: Mountain Standard Time
mdt: Mountain Daylight Time
pst: Pacific Standard Time
pdt: Pacific Daylight Time
yst: Yukon Standard Time
ydt: Yukon Daylight Time
hst: Hawaii Standard Time
hdt: Hawaii Daylight Time
cat: Central Alaska Time
akst: Alaska Standard Time
akdt: Alaska Daylight Time
ahst: Alaska-Hawaii Standard Time
nt: Nome Time
idlw: International Date Line West
cet: Central European Time
met: Middle European Time
mewt: Middle European Winter Time
mest: Middle European Summer Time
mesz: Middle European Summer Time
swt: Swedish Winter Time
sst: Swedish Summer Time
fwt: French Winter Time
fst: French Summer Time
eet: Eastern Europe Time, USSR Zone 1
bt: Baghdad Time, USSR Zone 2
zp4: USSR Zone 3
zp5: USSR Zone 4
zp6: USSR Zone 5
wast: West Australian Standard Time
wadt: West Australian Daylight Time
cct: China Coast Time, USSR Zone 7
jst: Japan Standard Time, USSR Zone 8
east: Eastern Australian Standard Time
eadt: Eastern Australian Daylight Time
gst: Guam Standard Time, USSR Zone 9
nzt: New Zealand Time
nzst: New Zealand Standard Time
nzdt: New Zealand Daylight Time
idle: International Date Line East

strftime — 根据区域设置格式化本地时间/日期 (把时间戳变成文本格式)
string strftime ( string $format [, int $timestamp ] )
strftime()工作的方式和date()没有什么不同,除了特殊格式化字符的前面必须添加一个百分号%。
strftime()有两个好处。第一个好处是如果你使用setlocale()函数,你可以通过strftime得到相应语言的月份的名称。另外的一个好处是你可以将特别的日期和时间的格式化字符包含在你的字符串中。这同时也意味着无论你是否要学习date()函数的所有特殊格式化字符,你都必须学习一整套完全不同的格式化字符。
一般phper用date会比较顺手吧。

strptime — 解析由 strftime() 生成的日期/时间 (把文本格式的日期信息存到数组)
array strptime ( string $date , string $format )
类似于date函数的getdate
<?php
$format = '%d/%m/%Y %H:%M:%S';
$strf = strftime($format);

echo "$strf\n";

print_r(strptime($strf, $format));
?>
输出
03/10/2004 15:54:19

Array
(
[tm_sec] => 19
[tm_min] => 54
[tm_hour] => 15
[tm_mday] => 3
[tm_mon] => 9
[tm_year] => 104
[tm_wday] => 0
[tm_yday] => 276
[unparsed] =>
)

time — 返回当前的 Unix 时间戳
int time ( void )

mktime — 取得一个日期的 Unix 时间戳
int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )
参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值,参数全部为空则与time()相同

microtime — 返回当前 Unix 时间戳和微秒数
microtime() 当前 Unix 时间戳以及微秒数。本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。
如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
如果给出了 get_as_float 参数并且其值等价于 TRUE,microtime() 将返回一个浮点数。默认为false
<?php
echo microtime(true);//1313743963.77
echo microtime(false);//0.10190200 1313744036
?>

标签:,
-