2012-09-18 56 views
0

看看这个代码,并请告诉我这怎么可能,这完美的作品星期三,而不是星期二:PHP strtotime错误?

<?php 
$current_time = strtotime('now'); 
if ($current_time > strtotime('tuesday this week 8:45pm') && $current_time < strtotime('tuesday this week 11:45pm')) { 
$background = 1; 
} 
if ($current_time > strtotime('wednesday this week 8:45pm') && $current_time < strtotime('wednesday this week 11:45pm')) { 
$background = 1; 
} 
else{ 
$background = 0; 
} 

?> 

    <script> 
    var backday = <?php echo json_encode($background); ?>; 
    </script> 

周二,则返回0,但周三则返回1,如这应该。为什么?

+2

取决于你设置的时区。对我来说,他们都应该返回0. –

+0

我假设你的意思是这个时间间隔在星期二执行时不起作用,但是它在两个星期三之间的星期三执行时有效吗? – ajon

+0

当你运行这个服务器时,你的服务器(不是本地)是什么时间? – 2012-09-18 20:32:28

回答

3

您的逻辑错误。第一个条件可能会返回1,但是你打到第二个条件。如果在第二个条件中的第一个条件为假,那么它将在您的else块中将该变量设置为0,而不管第一个条件中的设置如何。你需要让你的第二条语句如果是这样的话:

if ($current_time > strtotime('tuesday this week 8:45pm') && $current_time < strtotime('tuesday this week 11:45pm')) { 
    $background = 1; 
} 
else if ($current_time > strtotime('wednesday this week 8:45pm') && $current_time < strtotime('wednesday this week 11:45pm')) { 
    $background = 1; 
} 
else{ 
    $background = 0; 
} 
+0

这解决了它,我忘了把其他如果! – gazdac