A Useful Addition to any Business Website

August 5th, 2008 Posted in HTML

I’ve used this code in a large amount of business websites I’ve produced. It allows users to see at a glance whether a business is open or closed. If the time is within the specified hours, it will display an open image, if it any other time, it will display the closed image. It is easy to implement and looks great on a store hours page. It goes by the time of your server however so if your server is in a different timezone, adjust the hours accordingly.

<?php
/**
* Open / Closed script
* Try to edit only the ‘$hours’ array and the <img /> tags later on
* Editing anything else could give undesired results
*/

// These are the hours, correlating with the above days, in AM and PM format separated by ‘-‘
$hours = array(‘Sunday’ => ‘Closed’,
‘Monday’ => ‘By Appointment’,
‘Tuesday’ => ’10AM-5PM’,
‘Wednesday’ => ’10AM-5PM’,
‘Thursday’ => ’10AM-5PM’,
‘Friday’ => ’10AM-6PM’,
‘Saturday’ => ’10AM-5PM’);

/**
* Do not alter any code in this function unless you know what you are doing!

*/
function isOpen($hours) {
$days = array(0 => ‘Sunday’,
1 => ‘Monday’,
2 => ‘Tuesday’,
3 => ‘Wednesday’,
4 => ‘Thursday’,
5 => ‘Friday’,
6 => ‘Saturday’);

// Determine today’s weekday
$weekday = date(‘w’);

// Make sure that there are no errors in the formatting
if (!isset($days[$weekday])) {
return false;
} else if (!isset($hours[$days[$weekday]])) {
return false;
}

// Get the current day in string format
$day = $days[$weekday];

// Determine if hours are given
if (!is_numeric($hours[$day][0])) {
return false;
}

// Get the hours
$hours = explode(‘-‘, $hours[$day]);

// Again, make sure that there are no errors in the formatting
if (sizeof($hours) != 2) {
return false;
}

// Calculate the given hours
foreach ($hours as $index => $hour) {
// Check the formatting of the hours
if (!preg_match(‘~^(\d{1,2})(A|P)M$~s’, $hour, $time)) {
return false;
}

// Get the raw hour
$hour = (int)$time[1];

// Handle AM and PM times
switch ($time[2]) {
case ‘P’:
if ($hour != 12) {
$hour += 12;
}
break;
case ‘A’:
if ($hour == 12) {
$hour = 24;
}
break;
default:
return false;
}

// Ensure that the time is valid
if (($hour <= 0) || ($hour > 24)) {
return false;
} else if ($hour == 24) {
$hour = 0;
}

$hours[$index] = $hour;
}

// Determine if the business is open
$now = date(‘G’);

if (($now < $hours[0]) || ($now >= $hours[1])) {
return false;
}

return true;
}
?>

The next section goes wherever you’d like the code to appear in your website.

<?php
if (isOpen($hours)) {
// Opened image
echo ‘<img src=”images/open.gif” alt=”Open” title=”Open” />’;
} else {
// Closed image
echo ‘<img src=”images/closed.gif” alt=”Closed” title=”Closed” />’;
}
?>

Feel free to edit and use this code as you please. Download the PHP file here: PHP Hours File

Author -

who has written 16 posts on [Re]Encoded.com.


Contact the author


One Response to “A Useful Addition to any Business Website”

  1. SMS Says:

    Big THX !!!


Leave a Reply