phphtmldatedatetime

Check season of year in php


I want to print a different image every season of the year. Based on a date from my database. I want to print it in php/html5.

Found some things on the internet but none of them worked with my database date.


Solution

  • Just need to format the values o match yours

    $someDay = "2018-12-01";
    
    $spring = (new DateTime('March 20'))->format("Y-m-d");
    $summer = (new DateTime('June 20'))->format("Y-m-d");
    $fall = (new DateTime('September 22'))->format("Y-m-d");
    $winter = (new DateTime('December 21'))->format("Y-m-d");
    
    switch(true) {
        case $someDay >= $spring && $someDay < $summer:
            echo 'It\'s Spring!';
            break;
    
        case $someDay >= $summer && $someDay < $fall:
            echo 'It\'s Summer!';
            break;
    
        case $someDay >= $fall && $someDay < $winter:
            echo 'It\'s Fall!';
            break;
    
        default:
            echo 'It must be Winter!';
    }
    

    Try it online!