補充說明 strtotime() 解析相對時間字串的強大功能,與 date() 格式化輸出,並提供「僅允許特定時段發送簡訊」的實務範例。
strtotime('now'); // 現在
strtotime('+5 hours');
strtotime('+1 week');
strtotime('+1 week 3 days 7 hours 5 seconds');
strtotime('next Monday');
strtotime('last Sunday');
strtotime('2013-04-21'); // 固定日期
<?php
$y = date('Y');
$m = date('m');
$d = date('d');
$start = mktime(9, 0, 0, $m, $d, $y); // 09:00
$end = mktime(19, 0, 0, $m, $d, $y); // 19:00
$now = time();
if ($now >= $start && $now <= $end) {
// 允許發送簡訊
}
?>
$time = time();
$date = date('Y-m-d', $time);
echo $date; // 2024-08-08
strtotime('today') 取得今日 00:00:00 時間戳。mktime() 組合時分秒產生時間戳,避免字串解析效能開銷。DateTime 類別(modify()、add()、sub()、diff())。