溫馨提示×

PHP中怎么判斷當天時間段

PHP
小億
183
2024-01-19 13:54:31
欄目: 編程語言

要判斷當天的時間段,可以使用PHP中的時間函數來獲取當前時間,并進行判斷。下面是一個示例代碼:

<?php
$current_time = strtotime(date('H:i')); // 獲取當前時間的時間戳
$morning_start = strtotime('06:00'); // 早上開始時間
$noon_start = strtotime('12:00'); // 中午開始時間
$afternoon_start = strtotime('13:00'); // 下午開始時間
$evening_start = strtotime('18:00'); // 晚上開始時間

if ($current_time >= $morning_start && $current_time < $noon_start) {
    echo "早上";
} elseif ($current_time >= $noon_start && $current_time < $afternoon_start) {
    echo "中午";
} elseif ($current_time >= $afternoon_start && $current_time < $evening_start) {
    echo "下午";
} else {
    echo "晚上";
}
?>

在上面的代碼中,我們使用strtotime函數將時間字符串轉換為時間戳,并使用date函數獲取當前時間的小時和分鐘部分。然后,我們比較當前時間的時間戳與預定義的時間段的時間戳來判斷當前時間屬于哪個時間段,并輸出相應的結果。

注意,上述代碼只是一個示例,你可以根據自己的需求來定義時間段的開始和結束時間。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女