How to display range of hours

前端 未结 3 1443
北荒
北荒 2021-01-16 21:37

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

3条回答
  •  隐瞒了意图╮
    2021-01-16 21:55

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

提交回复
热议问题