写给愿意动手的 PHP 开发者
PHP学习网 PHP学习网
公告PHP学习网为公益性网站,致力于为广大PHP爱好者服务
日期与时间 PHP PHP 4 strtotime

strtotime()

把日期字符串解析成时间戳,支持 +1 day、next Monday 这类相对表达。

strtotime(string $datetime, ?int $baseTimestamp = null): int|false

参数

参数类型说明
$datetimestring日期字符串或相对表达
$baseTimestampint基准时间戳,省略则用当前时间

返回值

int|false — 时间戳,解析失败返回 false。

示例

PHP
echo date('Y-m-d', strtotime('+7 days'));
echo date('Y-m-d', strtotime('last day of this month'));

注意

  • 解析失败返回 false,不要直接拿去 date(),会得到 1970 年。
  • 「+1 month」不是加 30 天,而是月份加 1,1 月 31 日加一个月会溢出到 3 月 3 日。
  • 相对表达很强但不好读,团队里建议加注释。

相关函数