HTML

A Useful Addition to any Business Website

1 Comment 05 August 2008


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. See a demo in action here.

<?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

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

Related posts:

  1. 10 Ways to Save Time While Building a Website
  2. What Does Your Business Website Need?
  3. 10 Ways to Save Time While Building a Website: Improved
  4. Website Background Images : Examples and Implementation
  5. 13 beautiful green website designs for Inspiration

Author

Anders Haig

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


Contact the author

Your Comments

1 comment


Share your view

Post a comment

          

Community News

  • 25 Beautiful Free Twitter Icon Resources

    I though i would write this article to help bloggers, developers, and many others. Web icons are a huge element when it comes to web designing. Most people want a free, fast, and easy resource the…

    March 17, 2010
  • 60 Amazing Print Beer ads

    Happy St. Patrick’s Day! In honor of this holiday that is so closely linked with beer, we’ve pulled together the top 60 malt liquor advertisements.

    March 17, 2010
  • 110 Excellent Free Icon Sets For Your Next Design …

    In this post we present a 110 Excellent and Beautiful Free Icon Sets For Your Next Design or Project of Part I that are available for free download and (sometimes only for personal) use. Please mak…

    March 17, 2010
  • Amazing T-Shirt Graphic Designs Collection

    Many of the graphic designers around the world would have surely tried to come up with something new design for the t-shirts that they wear. In this post our try is to inspire these people to get i…

    March 17, 2010
  • Icon tutorial: Speaker icon with Illustrator (win7…

    In this tutorial we will use Adobe Illustrator CS4 to create a speaker/music icon inspired in Windows 7 style.

    March 16, 2010






© 2010 [Re]Encoded.com. Powered by Wordpress.