Check if todays date is between two other dates

后端 未结 3 571
攒了一身酷
攒了一身酷 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 06:59

    If you use DateTime object—which is by far the best approach—you are able to compare these with the regular comparators, e.g.:

    $date1 = new DateTime('today');
    $date2 = new DateTime('2014-04-04');
    
    if ($date1 < $date2) echo 'Past';
    else if ($date1 == $date2) echo 'Present';
    else echo 'Future';
    

    See documentation: http://php.net/manual/en/datetime.diff.php#example-2368

    0 讨论(0)
  • 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";
    
    0 讨论(0)
  • 2021-01-24 07:03

    Remember that a variable can be overwritten - just as the year progresses through the seasons, your variable can as well - as long as we do it in order we'll end up on the correct one. This means we only have to test if our date is after the date that a season changes.

    // Since we're testing today's date
    // we use the current year timestamps
    $year = date('Y');
    $startSpring = strtotime("$year-03-01");
    $startSummer = strtotime("$year-06-01");
    $startAutum = strtotime("$year-09-01");
    $startWinter = strtotime("$year-12-01");
    
    $today = time();
    
    // The year starts with Winter
    $season = 'Winter';
    if($today > $startSpring) $season = 'Spring'; // Past the 1st day of spring?
    if($today > $startSummer) $season = 'Summer'; // Etc...
    if($today > $startAutumn) $season = 'Autumn';
    if($today > $startWinter) $season = 'Winter';
    
    echo 'It is currently '.$season;
    

    Here's the same logic cleaned up in a pretty function that will check any date for you and return the season:

    // Accepts an optional unix timestamp
    // Uses the current date by default
    function getSeason($test_date=FALSE){
        $test_date = $test_date ? $test_date : time();
    
        // Use the year of the date we're testing
        $year = date('Y', $test_date);
    
        // The year starts with Winter
        $season = 'Winter';
        if($test_date > strtotime("$year-03-01")) $season = 'Spring'; // Past the 1st day of spring?
        if($test_date > strtotime("$year-06-01")) $season = 'Summer'; // Etc...
        if($test_date > strtotime("$year-09-01")) $season = 'Autumn';
        if($test_date > strtotime("$year-12-01")) $season = 'Winter';
    
        return $season;
    }
    
    0 讨论(0)
提交回复
热议问题