I have a database table storing opening hours of a restaurant as a time range in TIME format. Eg if the restaurant\'s opening hours are \'9am-5pm\', there will be 2 columns
ON my current project I had to loop trough the 24 hours of the day ... and wanted them to be displayed in a certain format.
$interval = new DateInterval('PT1H'); // creating the interval for 1hour
$date = new DateTime();
$end = clone($date);
$hoursRange = new DatePeriod($date, $interval ,$end->modify("+24 hours"));
And then all I had was to iterate trough the hoursRange values using a foreach.
foreach ($hoursRange as $key => $value)
{
echo $value->format('ga');
}
can anyone point me in the right direction?
Of course, here is everything you will need:
$start = strtotime('6:00am');
$end = strtotime('09:00am');
$range = array();
while ($start !== $end)
{
$start = strtotime('+30 minutes',$start);
$range[] = date('h:ia', $start);
}
print_r($range);