How to display range of hours

前端 未结 3 1444
北荒
北荒 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');
    }
    
    0 讨论(0)
  • 2021-01-16 21:58

    can anyone point me in the right direction?

    Of course, here is everything you will need:

    • http://php.net/manual/en/function.mktime.php
    • http://php.net/manual/en/control-structures.while.php
    • 30 m = 1800 s
    • http://php.net/manual/en/language.operators.arithmetic.php
    • http://php.net/manual/en/function.date.php
    0 讨论(0)
  • 2021-01-16 22:12
    $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);
    
    0 讨论(0)
提交回复
热议问题