|
|
 |
strtotime (PHP 3>= 3.0.12, PHP 4 , PHP 5) strtotime --
将任何英文文本的日期时间描述解析为 UNIX 时间戳
说明int strtotime ( string time [, int now])
本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为
UNIX 时间戳。如果 time 的格式是绝对时间则 now
参数不起作用。如果 time 的格式是相对时间则其所相对的时间由
now 提供,或者如果未提供 now 参数时用当前时间。失败时返回 -1。
因为 strtotime() 的行为是依照 GNU
日期语法的,因此请看看 GNU 手册中的
Date Input
Formats。这里记述了 time 参数的合法语法。
例子 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";
?>
|
|
例子 2. 检查失败 |
<?php
$str = 'Not Good';
if (($timestamp = strtotime($str)) === -1) {
echo "The string ($str) is bogus";
} else {
echo "$str == ". date('l dS of F Y h:i:s A',$timestamp);
}
?>
|
|
注:
有效的时间戳典型范围是从格林威治时间 1901 年 12 月 13 日 星期五 20:45:54 到
2038年 1 月 19 日 星期二 03:14:07。(该日期根据 32 位有符号整数的最小值和最大值而来。)
kyle at frozenonline dot com
02-Jan-2004 07:24
I was having trouble parsing Apache log files that consisted of a time
entry (denoted by %t for Apache configuration). An example Apache-date
looks like: [21/Dec/2003:00:52:39 -0500]
Apache claims this to be a
'standard english format' time. strtotime() feels otherwise.
I came
up with this function to assist in parsing this peculiar
format.
<?php function from_apachedate($date) {
list($d, $M, $y, $h, $m, $s, $z) = sscanf($date,
"[%2d/%3s/%4d:%2d:%2d:%2d %5s]"); return
strtotime("$d $M $y $h:$m:$s $z"); } ?>
Hope it
helps anyone else seeking such a conversion.
| |