PHP 的 strtotime() 函數支援豐富的自然語言時間描述,利用它可以極簡單地取得今天、昨天、明天的時間戳(Unix Timestamp),無需手動計算 86400 秒。
echo strtotime('now'), '<br>'; // 當下時間戳
echo strtotime('today'), '<br>'; // 今天 00:00:00
echo strtotime('tomorrow'), '<br>'; // 明天 00:00:00
echo strtotime('yesterday'), '<br>'; // 昨天 00:00:00
1469674869 // now 1469664000 // today (00:00:00) 1469750400 // tomorrow (00:00:00) 1469577600 // yesterday (00:00:00)
$time_remain = strtotime('tomorrow') - time();
// 輸出範例:75531 (秒)
這在「每日任務重置倒數」、「限時活動倒數」等場景非常實用。
Unix 時間戳轉換工具:https://tools.jb51.net/code/unixtime
善用 strtotime() 的英文關鍵字,能讓日期時間處理代碼更簡潔、可讀性更高!