- 论坛徽章:
- 0
|
请问在PHP中怎么样将一个字符串的日期转成timestamp?
strtotime
(PHP 3>;= 3.0.12, PHP 4 )
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 位有符号整数的最小值和最大值而来。) |
|