Jim Smith
Jim Smith

Reputation: 3

Seasonal Hours using PHP

I'm trying to create a PHP script that will show the seasonal hours of "Saturday 9am - 3pm" but only April 25 thru September 25 and my attempt is not working:

$day = date("d");
$month = date("m");

//default:
$sathours = "Sat by appt only";

//Premise: Appt only 04/25 thru 09/25

if($day >= 25 && $month >= 09 && $month <= 04) { 
$sathours = 'Sat by appt only. <br />Please call.';
} else {
$sathours = 'Sat 9am-3pm';
}

echo "$sathours";

As a non-programmer, I think I've looked at this too much and can't see where I've gone astray.

Upvotes: 0

Views: 42

Answers (1)

John Conde
John Conde

Reputation: 219834

Use DateTime() objects as they are comparable (and much more readable)

$today   = new DateTime();
$april25 = new DateTime('April 25th');
$sept25  = new DateTime('September 25th');

//Premise: Appt only 04/25 thru 09/25
$sathours = 'Sat by appt only. <br />Please call.';
if($today >= $april25 && $today <= $sept25) { 
    $sathours = 'Sat 9am-3pm';
}

echo $sathours;

Demo

Upvotes: 4

Related Questions