int strtotime(string $time [, int $now = time()])
將英文文字敘述的日期時間解析為 Unix 時間戳 (自 1970-01-01 00:00:00 GMT 起的秒數)。
$time:日期時間字串,支援 GNU 日期輸入格式$now:基準時間戳,預設為當前 time()。可指定其他時間作為相對計算基準january, february, march, april, may, june, july, august, september, sept, october, november, december
sunday, monday, tuesday, tues, wednesday, wednes, thursday, thur, thurs, friday, saturday
year, month, fortnight(兩週), week, day, hour, minute/min, second/sec
am/pm (上午/下午)
| 關鍵字 | 含義 | 範例 |
|---|---|---|
+n / -n | 加減 n 個單位 | +1 day, -2 weeks |
ago | 往前推算 | 24 hours ago |
tomorrow | 明天同一時間 | |
yesterday | 昨天同一時間 | |
today / now | 當前日期時間 | |
last | 上一個 | last tuesday |
this | 今天指定時間 / 下一個週期 | this 7am, this week |
next | 下一個 | next hour |
gmt, utc, est, edt, cst, cdt, pst, pdt, jst, cest 等
<?php
// 基準時間:2009-01-22 09:40:25
$base = strtotime('2009-01-22 09:40:25');
echo date('Y-m-d H:i:s', strtotime('+1 day', $base)); // 2009-01-23 09:40:25
echo date('Y-m-d H:i:s', strtotime('-1 day', $base)); // 2009-01-21 09:40:25
echo date('Y-m-d H:i:s', strtotime('+1 week', $base)); // 2009-01-29 09:40:25
echo date('Y-m-d H:i:s', strtotime('next Thursday', $base)); // 2009-01-29 00:00:00
echo date('Y-m-d H:i:s', strtotime('last Thursday', $base)); // 2009-01-15 00:00:00
?>
-1,之後回傳 falsedate_default_timezone_set() 明確設定時區第二個參數 $now 可用於「以某歷史時間點為基準推算相對時間」,例如計算「某訂單建立 30 天後」的到期日:
$order_time = strtotime('2023-01-15');
$due_date = strtotime('+30 days', $order_time);