1

我在根据两个预先指定的时间比较当前时间时遇到问题。让我告诉我想要什么:如果当前时间在晚上 10 点到早上 8 点之间,我想显示一条消息。我的代码是

$current_time = strtotime('now');
if ( $current_time > strtotime('10:00pm') && $current_time < strtotime('8:00am') ) { 
   //Display message
}

但它不起作用,因为在上午 12:00 之后, strtotime('10:00pm') 正在计算今天晚上 10 点的值,并且无法在给定条件下传递。所以我需要你的建议。

4

2 回答 2

1

试试这个,

     $current_hour = date("G", strtotime("now"));
     if ($current_hour > 23 || $current_hour < 8) {
     if ( $current_time > strtotime('10:00pm -1 Day') && $current_time < strtotime('8:00am') ) { //after 
     //Display message
    }
     }else{
     if ( $current_time > strtotime('10:00pm') && $current_time < strtotime('8:00am +1 Day') ) { 
    //Display message
      }
    }
于 2013-05-09T06:08:42.357 回答
1

date("G", strtotime("now"))将显示一小时的 24 小时格式,不带前导零。

你想要的代码是:

$current_hour = date("G", strtotime("now"));
if ($current_hour >= 22 || $current_hour < 8) {
    //Display message
}
于 2013-05-09T06:37:23.407 回答