Check if todays date is between two other dates

后端 未结 3 613
攒了一身酷
攒了一身酷 2021-01-24 06:27

I am trying to check if todays date is between START and STOP date of a period, Winter, summer, spring etc..

and if the todays date is between, lets say.. the winter per

3条回答
  •  庸人自扰
    2021-01-24 07:02

    You can stick with timestamps. Don't convert back to dates. You are making invalid comparisons such as the assumption that 30-01 is less than 28-02. The computer will compare the very first 3 to the 2 and tell you that 30-01 is CORRECTLY greater than 28-02. So...

    $startSummer = mktime(0,0,0, 6, 1, 2000); // The year doesn't matter according to your code
    $endSummer = mktime(0,0,0, 8, 31, 2000);
    

    Now, is some date between those? Assume I am checking $month and $day...

    $myday = mktime(0,0,0, $month, $day, 2000);
    if($myday>=$startSummer && $myday<=$endSummer) $season = "Summer";
    

提交回复
热议问题