PHP

Background that adapts to the current season

TODAY

Sunday: May 18, 2025

spring

What does this PHP script do?

This PHP script defines a function called GetCurrentSeason() that determines the current season based on the current date, following the seasonal pattern of the Northern Hemisphere . The result returned by GetCurrentSeason() can be used to dynamically select and display a background image corresponding to the current season — for example, an image labeled "spring.jpg" when it is springtime.

PHP
<?php
  function GetCurrentSeason() {
  // Approximate start dates for seasons in the Northern Hemisphere
  $seasonDates = array(
  '01-01' => 'winter',
  '03-21' => 'spring',
  '06-21' => 'summer',
  '09-21' => 'autumn',
  '12-21' => 'winter' // winter starts again
  );

$currentYear = date("Y");
  $currentTime = strtotime("now");

$currentSeason = 'winter'; // default fallback

foreach ($seasonDates as $date => $season) {
  $seasonStart = strtotime("$currentYear-$date");
  if ($currentTime >= $seasonStart) {
  $currentSeason = $season;
  }
  }

return $currentSeason;
  }

$returnCurrentSeason = GetCurrentSeason();
  // echo "Current season: " . $returnCurrentSeason;
?>
HTML and PHP
<section style="background: url(images/<?php echo $returnCurrentSeason; ?>.jpg);"> </section>