Skip to content

Instantly share code, notes, and snippets.

@TheHeat
Last active December 16, 2015 11:09
Show Gist options
  • Save TheHeat/5425598 to your computer and use it in GitHub Desktop.
Save TheHeat/5425598 to your computer and use it in GitHub Desktop.
Set default business opening hours as a multidimensional array. Allow for 2 types of exceptions (closed and bank holidays) then return the opening hours in a number of useful ways, including a JS powered "we are open/closed" thingy.
<?php
date_default_timezone_set('Europe/London');
// Define normal opening hours in nested arrays
$days = array(
'monday' => array('label' => 'Monday', 'open' => '09:30', 'close' => '22:00'),
'tuesday' => array('label' => 'Tuesday', 'open' => '09:30', 'close' => '22:00'),
'wednesday' => array('label' => 'Wednesday','open' => '09:30', 'close' => '22:00'),
'thursday' => array('label' => 'Thursday', 'open' => '09:30', 'close' => '22:00'),
'friday' => array('label' => 'Friday', 'open' => '09:30', 'close' => '21:00'),
'saturday' => array('label' => 'Saturday', 'open' => '10:00', 'close' => '18:00'),
'sunday' => array('label' => 'Sunday', 'open' => '10:00', 'close' => '18:00')
);
// Check what day and date it is now
$todayday = date('l');
$todaydate = date('Ymd');
// Pull in exceptions
$closed = array();
$bankholidays = array();
// If today is in the bank holiday list, apply Sunday opening hours.
// Otherwise, pull in todays opening hours from the $days array,
if(in_array($todaydate, $bankholidays)): $todayhours = $days['sunday'];
elseif($todayday == 'Monday'): $todayhours = $days['monday'];
elseif( $todayday == 'Tuesday'): $todayhours = $days['tuesday'];
elseif( $todayday == 'Wednesday'): $todayhours = $days['wednesday'];
elseif( $todayday == 'Thursday'): $todayhours = $days['thursday'];
elseif( $todayday == 'Friday'): $todayhours = $days['friday'];
elseif( $todayday == 'Saturday'): $todayhours = $days['saturday'];
elseif( $todayday == 'Sunday'): $todayhours = $days['sunday'];
endif;
// Set up a range by concatenating todays date and opening and closing times from the array above
$todayopen = date('Y-m-d ') . $todayhours['open'];
$todayclosed = date('Y-m-d ') . $todayhours['close'];
$now = date('Y-m-d H:i');
?>
<h1>Opening Hours</h1>
<p>Standard opening hours with no exceptions.</p>
<table>
<thead>
<tr>
<th>Day</th>
<th>Open</th>
<th>Closed</th>
</tr>
</thead>
<?php foreach ($days as $day): ?>
<tr>
<td data-title="Day"><?php echo $day['label']; ?></td>
<td data-title="Open"><?php echo $day['open']; ?></td>
<td data-title="Centre Closing"><?php echo $day['close']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<!-- Check if now is later than today's opening time and earlier than todays closing time -->
<div id="door-sign"><?php echo $todayhours['open'] . ' - ' . $todayhours['close']; ?></div>
<script type="text/javascript">
now = new Date();
nowHours = now.getHours();
nowMinutes = now.getMinutes();
nowTime = (nowHours + "." + nowMinutes)
open = <?php echo date('G.i' , strtotime($todayhours['open'])); ?>;
close = <?php echo date('G.i' , strtotime($todayhours['close'])); ?>;
if(open <= nowTime && nowTime < close){
document.getElementById('door-sign').innerHTML = 'We Are Open';
}else{
document.getElementById('door-sign').innerHTML = 'We Aint Open';
}
</script>
<h1>Today&rsquo;s Opening Hours</h1>
<?php if(in_array($todaydate, $closed)): ?>
<p>We are closed today.</p>
<?php else: ?>
<dl>
<dt>Open</dt>
<dd><?php echo $todayhours['open']; ?></dd>
<dt>Closed</dt>
<dd><?php echo $todayhours['close']; ?></dd>
</dl>
<?php endif; ?>
<h1>The Next 7 Days</h1>
<table>
<thead>
<tr>
<th>Day</th>
<th>Open</th>
<th>Closed</th>
</tr>
</thead>
<?php
// For each of the next 14 days
foreach( range(0,6) as $add ):
$weekday_calc = date('Ymd',strtotime( "today + $add day") );
$weekday = date('D jS F',strtotime( "today + $add day") );
$weekday_hours_day = date('l',strtotime( "today + $add day") );
if(in_array($weekday_calc, $bankholidays)): $weekdayhours = $days['sunday'];
elseif( $weekday_hours_day == 'Monday'): $weekdayhours = $days['monday'];
elseif( $weekday_hours_day == 'Tuesday'): $weekdayhours = $days['tuesday'];
elseif( $weekday_hours_day == 'Wednesday'): $weekdayhours = $days['wednesday'];
elseif( $weekday_hours_day == 'Thursday'): $weekdayhours = $days['thursday'];
elseif( $weekday_hours_day == 'Friday'): $weekdayhours = $days['friday'];
elseif( $weekday_hours_day == 'Saturday'): $weekdayhours = $days['saturday'];
elseif( $weekday_hours_day == 'Sunday'): $weekdayhours = $days['sunday'];
endif;
?>
<tr>
<td data-title="Day"><?php echo $weekday ?></td>
<td data-title="Open"><?php if(in_array($weekday_calc, $closed)): echo '[CLOSED]'; else: echo $weekdayhours['open']; endif; ?></td>
<td data-title="Closing"><?php if(in_array($weekday_calc, $closed)): echo '[CLOSED]'; else: echo $weekdayhours['close']; endif; ?></td>
</tr>
<?php endforeach; ?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment