Reputation: 118
I have a Laravel application in which I need to display all time slots between 9AM to 3AM on the basis of 15 minutes interval. the time slots does not contain no date, only times format like (AM and PM)
Upvotes: 1
Views: 3364
Reputation: 12391
you carbon 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;
as per your req. you need to use like this
$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"));
}
it retrun
[
"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"
]
ref link https://carbon.nesbot.com/docs/#api-period
Upvotes: 9
Reputation: 1641
Here is a framework agnostic function
/**
* 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;
}
Example of use with output:
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 .......
]
Upvotes: 2