首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP strtotime bug?

PHP strtotime bug?
EN

Stack Overflow用户
提问于 2012-09-19 04:27:52
回答 1查看 279关注 0票数 0

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

代码语言:javascript
复制
 <?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。为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-19 04:35:40

你的逻辑中有一个错误。第一个条件可能会返回1,但随后您就会遇到第二个条件。如果第二个条件中第一个If为false,它将在else块中将变量设置为0,而不管它在第一个条件中设置的值是什么。您需要将第二个if语句作为else if语句,如下所示:

代码语言:javascript
复制
 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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12484470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档