Skip to content

Commit

Permalink
Version 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
coryetzkorn committed Dec 1, 2014
1 parent fa8a64c commit c4c4fd5
Showing 1 changed file with 77 additions and 50 deletions.
127 changes: 77 additions & 50 deletions store-hours.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

// -------- PHP STORE HOURS ---------
// ---------- Version 2.0 -----------
// -------- BY CORY ETZKORN ---------
// -------- coryetzkorn.com ---------
// ----------------------------------
// PHP STORE HOURS
// ----------------------------------
// Version 2.1.0
// Written by Cory Etzkorn
// https://github.com/coryetzkorn/php-store-hours


// -------- EDIT FOLLOWING SECTION ONLY ---------
Expand All @@ -18,20 +20,20 @@
// If open multiple times in one day, enter time ranges separated by a comma
// If open late (ie. 6pm - 1am), add hours after midnight to the next day (ie. 00:00-1:00)
$hours = array(
'mon' => array('11:00-20:30'),
'tue' => array('11:00-16:00', '18:00-20:30'),
'wed' => array('11:00-20:30'),
'thu' => array('11:00-20:30'),
'fri' => array('11:00-20:30'),
'sat' => array('11:00-20:00'),
'sun' => array('11:00-20:30')
'mon' => array('11:00-20:30'),
'tue' => array('11:00-16:00', '18:00-20:30'),
'wed' => array('11:00-20:30'),
'thu' => array('11:00-20:30'),
'fri' => array('11:00-20:30'),
'sat' => array('11:00-20:00'),
'sun' => array('11:00-21:30')
);

// Optional: add exceptions (great for holidays etc.)
// Works best with format day/month
// Leave array empty if no exceptions
$exceptions = array(
//'Christmas' => '10/22',
'11/30' => array('11:00-16:00', '21:00-2:30')
//'New Years Day' => '1/1'
);

Expand All @@ -40,14 +42,14 @@
// Warning: %open% and %closed% will NOT work if you have multiple time ranges assigned to a single day.
// Optional: use %day% to make your "closed all day" message more dynamic.
// Optional: use %exception% to make your exception messages dynamic.
$open_now = "<h3>Yes, we're open! Today's hours are %open% until %closed%.</h3>";
$closed_now = "<h3>Sorry, we're closed. Today's hours are %open% until %closed%.</h3>";
$closed_all_day = "<h3>Sorry, we're closed on %day%.</h3>";
$exception = "<h3>Sorry, we're closed for %exception%.</h3>";

// Enter custom time format if using %open% and %closed%
// (options listed here: http://php.net/manual/en/function.date.php)
$time_format = 'g:ia';
$template = array(
'open' => "<h3>Yes, we're open! Today's hours are {%hours%}.</h3>",
'closed' => "<h3>Sorry, we're closed. Today's hours are {%hours%}.</h3>",
'separator' => ' - ',
'join' => ' and ',
'format' => 'g:ia', // (options listed here: http://php.net/manual/en/function.date.php)
'hours' => '{%open%}{%separator%}{%closed%}'
);

// The %day% shortcode is replaced by these days of the week.
// Edit these if you'd like to use a language other than English.
Expand All @@ -66,51 +68,76 @@

$day = strtolower(date("D"));
$today = strtotime('today midnight');
$now = strtotime(date("G:i"));
$now = strtotime('today 5pm');
//$now = strtotime(date("G:i"));
$is_open = 0;
$is_exception = false;
$is_closed_all_day = false;

// Check if closed all day
if($hours[$day][0] == '00:00-00:00') {
if($hours_today == '00:00-00:00') {
$is_closed_all_day = true;
}


// Check for exceptions, else use regular hours
if($exceptions) {
foreach($exceptions as $ex_day => $ex_hours) {
if(strtotime($ex_day) == $today) {
// Today is an exception, use alternate hours instead
$hours_today = $ex_hours;
} else {
// Today is not an exception, use regular hours
$hours_today = $hours[$day];
}
}
} else {
$hours_today = $hours[$day];
}

// Check if currently open
foreach($hours[$day] as $range) {
$range = explode("-", $range);
$start = strtotime($range[0]);
$end = strtotime($range[1]);
if (($start <= $now) && ($end >= $now)) {
$is_open ++;
}
foreach($exceptions as $ex_day => $ex_hours) {
foreach($hours_today as $range) {
$range = explode("-", $range);
$start = strtotime($range[0]);
$end = strtotime($range[1]);
// Add one day if the end time is past midnight
if($end <= $start) {
$end = strtotime($range[1] . ' + 1 day');
}
if (($start <= $now) && ($end >= $now)) {
$is_open ++;
}
}
}

// Check if today is an exception
foreach($exceptions as $ex => $ex_day) {
$ex_day = strtotime($ex_day);
if($ex_day === $today) {
$is_open = 0;
$is_exception = true;
$the_exception = $ex;
}
function render_output($template_name) {
global $template;
global $hours_today;
$output = '';
$index = 0;
foreach($hours_today as $range) {
$range = explode("-", $range);
$start = strtotime($range[0]);
$end = strtotime($range[1]);
if($index >= 1) {
$hours_template .= $template['join'];
}
$hours_template .= $template['hours'];
$hours_template = str_replace('{%open%}', date($template['format'], $start), $hours_template);
$hours_template = str_replace('{%closed%}', date($template['format'], $end), $hours_template);
$hours_template = str_replace('{%separator%}', $template['separator'], $hours_template);
$index ++;
}
$output .= str_replace('{%hours%}', $hours_template, $template[$template_name]);
echo $output;
}

// Output HTML
if($is_exception) {
$exception = str_replace('%exception%', $the_exception, $exception);
echo $exception;
} elseif($is_closed_all_day) {
$closed_all_day = str_replace('%day%', $days[$day], $closed_all_day);
echo $closed_all_day;
} elseif($is_open > 0) {
$open_now = str_replace('%open%', date($time_format, $start), $open_now);
$open_now = str_replace('%closed%', date($time_format, $end), $open_now);
echo $open_now;
if($is_open > 0) {
render_output('open');
} else {
$closed_now = str_replace('%open%', date($time_format, $start), $closed_now);
$closed_now = str_replace('%closed%', date($time_format, $end), $closed_now);
echo $closed_now;
render_output('closed');
}

?>

0 comments on commit c4c4fd5

Please sign in to comment.