温馨提示×

php strtotime函数的用法是什么

PHP
小亿
82
2023-12-29 18:51:26
栏目: 编程语言

strtotime函数是PHP中的一个日期/时间函数,用于将一个英文文本的日期时间描述解析为Unix时间戳。

strtotime函数的用法如下:

strtotime(string $time, int $now = time()): int|false
  • $time:表示要解析的日期/时间字符串。可以是一个绝对日期,如"2021-01-01";也可以是一个相对日期,如"tomorrow"表示明天,"+1 week"表示一周后。
  • $now(可选):表示用于计算相对日期的基准时间。默认为当前的Unix时间戳。

函数返回解析后的Unix时间戳,如果解析失败,则返回false。

以下是一些strtotime函数的常见用法示例:

echo strtotime("now");  // 当前时间的Unix时间戳

echo strtotime("2021-01-01");  // 指定日期的Unix时间戳

echo strtotime("tomorrow");  // 明天的Unix时间戳

echo strtotime("+1 week");  // 一周后的Unix时间戳

echo strtotime("next Monday");  // 下个周一的Unix时间戳

echo strtotime("last day of next month");  // 下个月最后一天的Unix时间戳

echo strtotime("2021-01-01 12:00:00");  // 指定日期时间的Unix时间戳

strtotime函数对于处理日期时间字符串非常方便,可以快速解析各种格式的日期时间描述。

0