PHP snippet to display the season

The Spring Equinox has just begun, and I needed a PHP snippet that would display the current season, where Dec, Jan and Feb are Winter, etc. Here’s how.

<?php
$seasons = array("Winter","Spring","Summer","Autumn");
echo $seasons[(int)((date("n") %12)/3)];
?>

Notes

  • First we create an array of season names, $seasons[0]=”Winter”, etc.
  • date(“n”) produces a numeric month 1..12 (1=Jan, 12=Dec)
  • date(“n”) %12 (modulus 12) converts month 12 (Dec) to 0. Modulus is also the remainder, so 12%12 is 1 remained 0.
  • Then we divide by 3 and convert to an integer to produce four groups of months, 0,1,2,3.
  • Then we display the nth array element.

Summary

Month: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
date(“n”): 1 2 3 4 5 6 7 8 9 10 11 12
date(“n”)%12: 1 2 3 4 5 6 7 8 9 10 11 0
(date(“n”)%12)/3: 1/3 2/3 1 4/3 5/3 2 7/3 8/3 3 10/3 11/3 0
int(date(“n”)%12)/3: 0 0 1 1 1 2 2 2 3 3 3 0
$season[x]: Winter Spring Summer Autumn Win