如何显示两次之间的所有时间段

时间:2020-12-23 10:23:01

标签: laravel laravel-5

我有一个 Laravel 应用程序,我需要在其中以 15 分钟为间隔显示上午 9 点到凌晨 3 点之间的所有时间段。 时间段不包含日期,只有时间格式,如(AM 和 PM)

2 个答案:

答案 0 :(得分:5)

你碳CarbonPeriod

use Carbon\CarbonPeriod;


$period = new CarbonPeriod('03:00', '15 minutes', '14:00'); // for create use 24 hours format later change format 
$slots = [];
foreach($period as $item){
    array_push($slots,$item->format("h:i A"));
}

return $slots;

根据您的要求。你需要像这样使用


$today =  today()->format('Y-m-d') . " 2:00 PM";
$tomorrow =  today()->addDays('1')->format('Y-m-d')  . " 3:00 AM";
$period = new CarbonPeriod(new Carbon($today), '15 minutes', new Carbon($tomorrow));
$slots = [];
foreach ($period as $item) {
    array_push($slots, $item->format("h:i A"));
}

它重新运行

[
    "02:00 PM",
    "02:15 PM",
    "02:30 PM",
    "02:45 PM",
    "03:00 PM",
    "03:15 PM",
    "03:30 PM",
    "03:45 PM",
    "04:00 PM",
    "04:15 PM",
    "04:30 PM",
    "04:45 PM",
    "05:00 PM",
    "05:15 PM",
    "05:30 PM",
    "05:45 PM",
    "06:00 PM",
    "06:15 PM",
    "06:30 PM",
    "06:45 PM",
    "07:00 PM",
    "07:15 PM",
    "07:30 PM",
    "07:45 PM",
    "08:00 PM",
    "08:15 PM",
    "08:30 PM",
    "08:45 PM",
    "09:00 PM",
    "09:15 PM",
    "09:30 PM",
    "09:45 PM",
    "10:00 PM",
    "10:15 PM",
    "10:30 PM",
    "10:45 PM",
    "11:00 PM",
    "11:15 PM",
    "11:30 PM",
    "11:45 PM",
    "12:00 AM",
    "12:15 AM",
    "12:30 AM",
    "12:45 AM",
    "01:00 AM",
    "01:15 AM",
    "01:30 AM",
    "01:45 AM",
    "02:00 AM",
    "02:15 AM",
    "02:30 AM",
    "02:45 AM",
    "03:00 AM"
]

参考链接 https://carbon.nesbot.com/docs/#api-period

答案 1 :(得分:1)

这是一个与框架无关的函数

 /**
         * get a list of hours btw two range
         *
         * @param int  $lower  start hours in secondes
         * @param int  $upper  end hours in secondes
         * @param int  $step   hop btw hours in seconde
         * @param null $format output hours format
         *
         * @return array with hours range interval  
         * Exemple of use: // Every 30 Minutes from 8 AM - 5 PM, using Custom Time Format:  hoursRange( 28800, 61200, 60 * 30, 'h:i a' );
         */
       function hoursRange($lower = 0, $upper = 86400, $step = 1800, $format = 'H:i')
        {
            $times = [];
    
            if (empty($format))
            {
                $format = 'H:i';
            }
    
            foreach (range($lower, $upper, $step) as $increment)
            {
                $increment = gmdate('H:i', $increment);
                list($hour, $minutes) = explode(':', $increment);
    
                $date = new \DateTime($hour.':'.$minutes);
    
                $times[(string)$increment] = $date->format($format);
            }
    
            return $times;
        }

输出使用示例:

hoursRange(0, 86400, 1800, 'H:i')

array:48 [▼
  "00:00" => "00:00"
  "00:30" => "00:30"
  "01:00" => "01:00"
  "01:30" => "01:30"
  "02:00" => "02:00"
  "02:30" => "02:30"
  "03:00" => "03:00"
  "03:30" => "03:30"
  "04:00" => "04:00"
  "04:30" => "04:30"
  "05:00" => "05:00"
  "05:30" => "05:30"
  "06:00" => "06:00"
  "06:30" => "06:30"
  "07:00" => "07:00"
  "07:30" => "07:30"
  "08:00" => "08:00"
  "08:30" => "08:30"
  "09:00" => "09:00"
  "09:30" => "09:30"
  "10:00" => "10:00"
  "10:30" => "10:30"
  "11:00" => "11:00"
  "11:30" => "11:30"
  "12:00" => "12:00"
  "12:30" => "12:30"
  "13:00" => "13:00"
  "13:30" => "13:30"
  //etc .......
]
相关问题