繁体   English   中英

PHP strtotime错误?

[英]PHP strtotime bug?

看一下这段代码,请告诉我这在星期三而不是星期二是如何完美工作的:

 <?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。 为什么?

您的逻辑有误。 第一个条件可能返回1,但随后您遇到了第二个条件。 如果第二个条件中的第一个条件为false,则它将在else块中将变量设置为0,而与第一个条件中的设置无关。 您需要将第二个if语句设为else if,例如:

 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM