在 PHP 中验证时间是否在午夜和凌晨 1 点之间


Verify if time is between midnight and 1am in PHP

我编写了以下行,用于验证时间是否在午夜(00:00)和凌晨1点(01:00)之间。正确吗?

$current_time = strtotime('now');
if ($current_time > strtotime('12:00pm') && $current_time < strtotime('1:00am')) { DO SOMETHING }

我不确定中午 12:00...提前感谢!

您正在尝试在中午(中午 12:00)和凌晨 1 点之间进行验证。 上午 12:00 是午夜。所以你应该把它改成

if ($current_time > strtotime('12:00am') && $current_time < strtotime('01:00am')) { DO SOMETHING }

考虑一下:

<?php
$current_time   =   date('d M Y H:i:s');
$current_hour   =   date('H', strtotime($current_time));
if($current_hour < 1){
    //do something
}
else{
    //do something else
    }
?>

"H"将以仅小时格式返回时间。只要时间在上午 12:00 和凌晨 1:00 之间,条件就会返回 TRUE。