Change Graphics Based on Season



function current_season() {
       // Locate the icons
       $icons = array(
               "spring" => "images/spring.png",
               "summer" => "images/summer.png",
               "autumn" => "images/autumn.png",
               "winter" => "images/winter.png"

       // What is today's date - number
       $day = date("z");

       //  Days of spring
       $spring_starts = date("z", strtotime("March 21"));
       $spring_ends   = date("z", strtotime("June 20"));

       //  Days of summer
       $summer_starts = date("z", strtotime("June 21"));
       $summer_ends   = date("z", strtotime("September 22"));

       //  Days of autumn
       $autumn_starts = date("z", strtotime("September 23"));
       $autumn_ends   = date("z", strtotime("December 20"));

       //  If $day is between the days of spring, summer, autumn, and winter
       if( $day >= $spring_starts && $day <= $spring_ends ) :
               $season = "spring";
       elseif( $day >= $summer_starts && $day <= $summer_ends ) :
               $season = "summer";
       elseif( $day >= $autumn_starts && $day <= $autumn_ends ) :
               $season = "autumn";
       else :
               $season = "winter";

       $image_path = $icons[$season];

       echo $image_path;



<img src="<?php current_season() ?>" alt="" />


  1. User Avatar
    Baylor Rae'
    Permalink to comment#

    In the days of Autumn, I accidentally put autumn starts on Sep. 22. That needs to be changed to Sep 23.

  2. User Avatar
    Adam Haskell
    Permalink to comment#

    In the interests of efficiency, it might be better to simply know what number day of the year each season starts on, then compare. So like:

    $day = date(“z”);
    if( $day < 79) $season = "winter";
    elseif( $day < 171) $season = "spring";
    elseif( $day < 265) $season = "summer";
    elseif( $day < 354) $season = "autumn";
    else $season = "winter";

    • User Avatar

      yep.. or:

      $today = getdate();
      $today = $today[‘mon’];
      if ($today>=1 && $today=3 && $today=6 && $today<=8) return 'summer';
      else return 'autumn';

    • User Avatar

      omg.. sorry

      =1 && $today=3 && $today=6 && $today

    • User Avatar
      Permalink to comment#

      Incorrect Adam, because that changes over the years.

      On a leap year, spring actually starts on day 172 (because of feb. having one more day, feb. 29). So every season (except winter) shifts one day. Next year, its back to normal.

      But you are correct that it could be more efficient:

      // seasonselection
      // find daynumber of today
      $day = date("z");
      // startdate season
      $spring = date("z", strtotime("March 21"));
      $summer = date("z", strtotime("June 21"));
      $autumn = date("z", strtotime("September 23"));
      // set season accoring to startdate
      if ($day < $spring) { $season = "winter"; }
      elseif($day < $summer) { $season = "spring"; }
      elseif($day < $autumn) { $season = "summer"; }
      else { $season = "autumn"; }
  3. User Avatar
    Permalink to comment#

    I’m having trouble with this script.

    I have created a PHP file, “current_season.php” in the same directory as my Web page.

    I have included in the Web page

    <?php include 'current_season.php'; ?&rt;

    I have included in the Web page

    <img src="<?php current_season() ?&rt;"&rt;<

    but it is not working.

    Any help, please?

    • User Avatar
      Permalink to comment#

      Furthermore, even if I put the php scripting in the head of my Web page, it doesn’t work when I attempt to display the image.

      I’m wanting to put the php script in a .php file instead of on each individual page.

    • User Avatar
      Permalink to comment#

      Furthermore, the files are named the same as in the script: “spring, summer, autumn, and winter,” and are in the “images” folder within the same directory.

  4. User Avatar
    Permalink to comment#

    How can you alter this to do a time check, if AM, display AM image, if night, display a night image..

    • User Avatar
      mike ilz
      Permalink to comment#

      You can use this for the Day / Night switch. That way you switch the CSS file depending on time (that’d work if the image was a background image, etc.)

  5. User Avatar
    Permalink to comment#


    In this section of code:

    <img src="” alt=”” />

    Make sure you have a “;” at the end of current_season().

    After correction:
    <img src="” alt=”” />

    Hope this helps, when I used it I was able to display the image just fine.

  6. User Avatar
    Permalink to comment#

    Used this code to change page header 26 times per year. Discovered if you put a start date like December 21 and a stop date January 4 – to purpose being to cross over from the old year to the new year – since this code does not include the year – it screws the math up when it tries to count what day of the year it is…..

  7. User Avatar

    Hello there i get a error in me firebug dialog:

    Failed to load given url when i hover the following line:

    <img src="” alt=”” />

    Already uploaded the images in the correct folder

    Anyone has a suggestion?

  8. User Avatar
    Permalink to comment#

    I would love to accomplish the same script with a css file. How hard would that be?

    • User Avatar
      Permalink to comment#

      JavaScript can help you with that ;)

      First, define a function (written by Juuitchan from ):

      function dayofyear(d) {   // d is a Date object
      var yn = d.getFullYear();
      var mn = d.getMonth();
      var dn = d.getDate();
      var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
      var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
      var ddiff = Math.round((d2-d1)/864e5);
      return ddiff+1; }

      Then, compare it:

      var curdate=new Date(); // gets today's date
      var cdnum=dayofyear(curdate);
      if ( cdnum < 79) { season = "winter"; }
      else if ( cdnum < 171) { season = "spring"; }
      else if ( cdnum < 265) { season = "summer"; }
      else if ( cdnum < 354) { season = "autumn"; }
      else season = "winter";

      And with a simple help of jQuery (or you can use some different library), you can add the season class to the desired object:

    • User Avatar
      Philip Osborne
      Permalink to comment#

      You can use (almost) exactly the same PHP. Instead of paths to your image files, specify paths to your CSS files. You may want to change $image_path to $css_path just for the sake of clarity.

      Then in your HTML, instead of using

      <img src="<?php current_season() ?>" alt="" />

      in the <body> of your page, just use

      <link rel="stylesheet" href="<?php current_season() ?>" type="text/css" />

      in the <head> of your page.

      Just make sure that the PHP appears BEFORE the CSS link.

  9. User Avatar
    Permalink to comment#

    If u provide demos regarding the articles posted.. it will be quiet useful..

  10. User Avatar
    Marcus Duke
    Permalink to comment#

    Would love to figure out how to do this with not just an image, but an image rollover. Not sure your method is the way to go, however (because I don’t know enough about PHP).

  11. User Avatar
    Permalink to comment#

    Very good. Now how would one preload the images first since this is really useful to display some fantastic large size background image. many thanks.

  12. User Avatar
    Philip Osborne
    Permalink to comment#

    Wondering if it’s possible to have hyperlinks that would force a particular season to give visitors the chance to preview the other seasons?

  13. User Avatar
    Matthew McMullen

    Is there any way you can add the year to the php? I have a client that is using this for Holidays, since holidays change from year to year I just like to know if this is possible?

    • User Avatar
      Permalink to comment#

      This works great for me…
      Zero at the end makes year goto any year. Do not have to update it.

      $holiDate10 = date(“Y-m-d”,strtotime(“Second Monday of October 0”)); // Columbus Day

  14. User Avatar


    Simple question, but I forget my php / html sorry. I want to put the variable $season from the o.p in the alt and title tags (to show a greeting), but I am getting the text ‘array’ when I hover over the image, when I want it to display something like:

    Happy <?php title="Happy ”

    Ex: Happy Summer!

  15. User Avatar

    Sorry, here’s my codepen:

  16. User Avatar

    Sorry for the incomplete code:

    <img src="<?php current_season() ?>" alt="<?php $season ?>" title="Happy <?php echo [$season]; ?>" />

  17. User Avatar
    Permalink to comment#

    Nice script, but nobody seems to mind about how to connect the php file (different then the functions.php we have into child theme, I suppose) and the image code… Is there missing an includes code somewhere…?

  18. User Avatar
    Brian Jester
    Permalink to comment#


    I need a php script that can display a random image from a bunch of images in a folder and that will only display one image at a time for a 24 hour period, and then display another the same way not repeating the images until the full list has been displayed one time. This is essentially a pic of the day script in php. Can you provide a link to one that can do just that?

    Thank you in advance!

  19. User Avatar
    Brian Jester

    I’m going to post some code for others as it might do them good in the long term:

    <link rel="stylesheet" href="” type=”text/css” />
    “summer” => “css/summer.css”,
    “autumn” => “css/autumn.css”,
    “winter” => “css/winter.css”
    // What is today’s date – number
    $day = date(“z”);
    // Days of spring
    $spring_starts = date(“z”, strtotime(“March 21”));
    $spring_ends = date(“z”, strtotime(“June 20”));
    // Days of summer
    $summer_starts = date(“z”, strtotime(“June 21”));
    $summer_ends = date(“z”, strtotime(“September 22”));
    // Days of autumn
    $autumn_starts = date(“z”, strtotime(“September 23”));
    $autumn_ends = date(“z”, strtotime(“December 20”));
    // If $day is between the days of spring, summer, autumn, and winter
    if( $day >= $spring_starts && $day = $summer_starts && $day = $autumn_starts && $day```

    Then, instead of the images folder change that folder to css and put the files below in it:


    Add whatever styles you want in the css.

    As a final note: look at my code above and compare with the original code, and you will see the $images_path was replaced with $css_path, and the images/spring.png was replaced with css/spring.css etc..etc..

    Good Luck and have fun!

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.