请看一下这段代码,请告诉我这是如何在周三完美工作的,而不是周二的:
<?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。为什么?
发布于 2012-09-19 04:35:40
你的逻辑中有一个错误。第一个条件可能会返回1,但随后您就会遇到第二个条件。如果第二个条件中第一个If为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;
}https://stackoverflow.com/questions/12484470
复制相似问题