diff --git a/store-hours.php b/store-hours.php index 23073ff..bb1b47d 100644 --- a/store-hours.php +++ b/store-hours.php @@ -1,9 +1,11 @@ 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' ); @@ -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 = "

Yes, we're open! Today's hours are %open% until %closed%.

"; -$closed_now = "

Sorry, we're closed. Today's hours are %open% until %closed%.

"; -$closed_all_day = "

Sorry, we're closed on %day%.

"; -$exception = "

Sorry, we're closed for %exception%.

"; - -// 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' => "

Yes, we're open! Today's hours are {%hours%}.

", + 'closed' => "

Sorry, we're closed. Today's hours are {%hours%}.

", + '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. @@ -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'); } ?>