I was recently asked to code a little widget for someone’s Eshop to display a message at a certain time of day to say if they were open or closed.
The shop’s opening hours were 8:00am – 11:30am then 4:00pm – 10:00pm.
<?php
$open_AM = mktime(8,0,0); //Open 8:00AM
$close_AM = mktime(11,30,0); // Closing 11:30AM
$open_PM = mktime(16,0,0); // Open 4:00PM
$close_PM = mktime(22,0,0); // Close 10:00PM
$offset = 7200;
//Current Server Time
echo "Current server time: ";
$t=time();
echo(date("D F d Y H:i:s",$t));
if ($t >= $open_AM && $t <= $close_AM)
{
echo "Shop Open";
}
else if($t >= $open_PM && $t <= $close_PM)
{
echo "Shop Open";
}else{
echo "Shop Closed";
}
//Plus 2 Hours
$t=time() + $offset;
echo "Plus 2 hours: ";
echo(date("D F d Y H:i:s",$t));
?>
I will now break this code down for you.