簡體   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