2013-05-09 36 views
1

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

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

但它不工作,因为,经过上午12:00,的strtotime('10:00')是计算今天的10PM价值,是不能够在给定的条件通过。所以我需要你的建议。

+0

从的strtotime返回的值()包含的最新信息数据,这是实际问题 – Shin 2013-05-09 06:00:23

+0

你想下午10:00 - 8:00 AM(第二天)??? – Shin 2013-05-09 06:04:21

+0

是的,正确。 – IFightCode 2013-05-09 11:49:55

回答

1

date("G", strtotime("now"))将呈现一个小时的24小时制,没有前导零。

你想要的代码将是:

$current_hour = date("G", strtotime("now")); 
if ($current_hour >= 22 || $current_hour < 8) { 
    //Display message 
} 
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 
     } 
    }