Adding class to body based on seasons/time

I’m using WordPress (which may or may not be relevant) but I’m trying to add a class to the body based on the time of the year.

I have three ‘themes’ on the site; spring, summer and autumn. If the period is Feb-May, for example, the class on the body would be ‘spring’. If the period is Jun-September the class on the body would be ‘summer’, and so forth.

Read More

Any ideas how I would achieve this?

Currently the body already has relevant classes based on which page you are on (WordPress default) which looks like this

<body <?php body_class(); ?>>

But you can add your own class by doing

<body <?php body_class('a-new-class'); ?>>

Any help would be appreciated — I’m guessing a simple PHP if statement to do with dates?

Cheers,
R

Related posts

Leave a Reply

1 comment

  1. PHP:

    function getSeasonCssClass() {
      $month = date('n'); // current month number without leading 0
      $season = array(
        1 => 'winter',
        2 => 'winter',
        3 => 'winter',
        4 => 'spring',
        5 => 'spring',
        6 => 'spring',
        7 => 'summer',
        8 => 'summer',
        9 => 'summer',
        10 => 'autumn',
        11 => 'autumn',
        12 => 'autumn',
      );
      return $season[$month];
    }
    

    HTML:

    <body <?php body_class(getSeasonCssClass());?>>