函数名:strtotime()
适用版本:PHP 4, PHP 5, PHP 7
用法:strtotime() 函数将任何英文文本的日期时间描述转换为Unix时间戳。
语法:int strtotime ( string $time [, int $now = time() ] )
参数:
- time:必需,要转换的日期时间描述。常见的格式包括"now"、"10 September 2000"、"tomorrow"、"+1 day"等。支持的日期时间格式请参考 PHP 官方文档。
- now:可选,用于计算相对日期时间的基准时间。默认为当前时间的 Unix 时间戳。
返回值:成功时返回一个表示时间的 Unix 时间戳,失败时返回 FALSE。
示例 1:
$date = strtotime("10 September 2000");
echo $date; // 输出:968217200
示例 2:
$date = strtotime("+1 day", strtotime("2022-01-01"));
echo date("Y-m-d", $date); // 输出:2022-01-02
示例 3:
$date = strtotime("next Monday");
echo date("Y-m-d", $date); // 输出:2022-05-02(如果当前日期是2022-04-29)
注意事项:
- strtotime() 函数对于给定的日期时间描述非常灵活,可以接受多种不同格式的输入。
- 如果给定的日期时间描述无法被解析为有效的日期时间,strtotime() 函数将返回 FALSE。
- 在 PHP 5.1.0 之前的版本中,strtotime() 函数对于未来的年份有限制,只能解析1970年到2038年之间的日期时间。在 PHP 5.1.0 及之后的版本中,已解除了这个限制。
- 使用 strtotime() 函数时,建议先确保系统的时区设置正确,以避免出现时间偏移的问题。