Code for the Seasons!
If, Elseif, switch Example
This page determines the Season based on the current Date.
Month: 12 Day: 21
This page determines the Season based on the current Date.
Month: 12 Day: 21
<?php
include("includes/header.php");
error_reporting(E_ALL); // set error reporting to all
date_default_timezone_set("America/Detroit"); // set time zone for here, not the server
echo("<h1>Code for the Seasons!</h1>");
echo("<h1>If, Elseif, switch Example</h1>");
echo("<p>This page determines the Season based on the current Date.</p>");
$day = date('d'); //the day of the month with leading zeros
$month = date('m');//the month as a number with leading zeros
$season = '-';
//define constants
$SPRING = "Spring";
$SUMMER = "Summer";
$FALL = "Fall";
$WINTER = "Winter";
echo("<p><b>Month:</b> $month <b>Day:</b> $day</p>");
//see if we are in a month with an equinox or solstice
switch($month)
{
case($month == 3)://winter and spring are both in march
{
if($day < 20)//the spring equinox is the 20th for most years...
{
$season = $WINTER;
}
else
{
$season = $SPRING;
}
break;
}
case($month == 6)://spring and summer are both in june
{
if($day < 21)//the summer solstice is the 21th for most years...
{
$season = $SPRING;
}
else
{
$season = $SUMMER;
}
break;
}
case($month == 9)://summer and fall are both in september
{
if($day < 22)//the autumnal equinox is the 22th for many and this year...
{
$season = $SUMMER;
}
else
{
$season = $FALL;
}
break;
}
case($month == 12)://fall and winter are both in december
{
if($day < 21)//the winter solstice is the 21th for most years...
{
$season = $FALL;
}
else
{
$season = $WINTER;
}
break;
}
default://ok, if we are here, its a non solstice/equinox month, we know the season based on month then
{
if ($month == 1 || $month == 2)//jan and feb are nothing but winter
{
$season = $WINTER;
}
elseif ($month == 4 || $month == 5)//april and may are all spring
{
$season = $SPRING;
}
elseif ($month == 7 || $month == 8)//july and august are all summer
{
$season = $SUMMER;
}
elseif ($month == 10 || $month == 11)//oct and nov are all in fall
{
$season = $FALL;
}
}
}
echo("<p><h1>Greetings! It is currently $season!</h1></p>");
$image = '';
switch ($season)
{
case $SPRING:
$image = "spring.png";
break;
case $FALL:
$image = "fall.png";
break;
case $WINTER:
$image = "winter.png";
break;
case $SUMMER:
$image = "summer.png";
break;
}
echo("<img src=\"images/$image\" class=\"floatL\" >");
include("includes/footer.php");
?>