strtotime()
把日期字符串解析成时间戳,支持 +1 day、next Monday 这类相对表达。
strtotime(string $datetime, ?int $baseTimestamp = null): int|false
参数
| 参数 | 类型 | 说明 |
|---|---|---|
$datetime | string | 日期字符串或相对表达 |
$baseTimestamp | int | 基准时间戳,省略则用当前时间 |
返回值
int|false — 时间戳,解析失败返回 false。
示例
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 日。
- 相对表达很强但不好读,团队里建议加注释。