Code Snippet

Home » Code Snippets » PHP » Change Graphics Based on Season

Change Graphics Based on Season

Function

<?php

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";
       endif;

       $image_path = $icons[$season];

       echo $image_path;
}

?>

Usage

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

Subscribe to The Thread

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

  2. 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:

    <php
    $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";
    ?>

    • yep.. or:

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

    • omg.. sorry

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

  3. sean

    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?

    • sean

      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.

    • sean

      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. How can you alter this to do a time check, if AM, display AM image, if night, display a night image..

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

  5. Jacob

    Sean,

    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. 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. Lorenzo

    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?

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~