Skip to content

Instantly share code, notes, and snippets.

@kyleturman
Created October 27, 2012 22:33
Show Gist options
  • Save kyleturman/3966683 to your computer and use it in GitHub Desktop.
Save kyleturman/3966683 to your computer and use it in GitHub Desktop.
OpenClosed
<?php
/*
OpenClosed function
Author: Kyle Turman
Displays whether a business is open or closed based on hours given.
*/
function openClosed() {
// Set timezone to local timezone for business.
date_default_timezone_set('America/Chicago');
// List the open and closed hours for each day, use HHMMSS format.
// Put -1 on closing hours portion if closed that day, but keep opening hours.
// If the closing hours go past midnight, add it to 240000 instead (e.g. 260000 instead of 020000) for ease of testing.
$hours = array(array(110000, -1), // Sunday
array(110000, -1), // Monday
array(110000, 240000), // Tuesday
array(110000, 240000), // Wednesday
array(110000, 240000), // Thursday
array(110000, 260000), // Friday
array(110000, 260000)); // Saturday
// Get Current Date Variables for testing.
$now = date('His');
$today = date('w');
$time = date('h:i a');
// If it's in the wee hours of the mernin'.
if ($now < $hours[$today][0]) {
// Add 240000 to compare properly with closing time.
$now = $now + 240000;
// Pull back the day to current day to make sure day is correct.
$today = ($today == 0 ? 6 : $today - 1);
}
// Test to see if modified current time is within open and closed times.
if ($hours[$today][0] <= $now && $now < $hours[$today][1]) {
echo "it's $time, you know we're open. come on in.";
} else {
echo "sorry folks, it's $time, so we're closed right now";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment