Tomasz Ziobro
Tomasz Ziobro

Reputation: 21

How to display range of hours

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 'hours_open' and 'hours_close' where I store 9:00 in 'hours_open' and 17:00 in 'hours_close'. What I need to do is to display the range of hours eg: 9:30am 10:00am 10:30am 11:00am 11:30am until 5:00pm

can anyone point me in the right direction?

Upvotes: 0

Views: 5449

Answers (3)

Rosu Flavius
Rosu Flavius

Reputation: 91

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');
}

Upvotes: 0

mintobit
mintobit

Reputation: 2383

$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);

Upvotes: 5

Related Questions