If I have the following variables:
$starttime = '09:00'; // Start time
$endtime = '21:00'; // End time
$interval = '30'; // In minutes
What would be the best way to generate the following array?
[
"09:00" => "09:30",
"09:30" => "10:00",
"10:00" => "10:30",
"10:30" => "11:00",
...
"20:00" => "20:30",
"20:30" => "21:00"
]
There are some similar topics that show how to array time intervals but none of them have been able to show my specific issue.
Using DateTime class with DateInterval you can achieve what you need.
DateTime - https://www.php.net/manual/pt_BR/class.datetime.php
DateInterval - https://www.php.net/manual/pt_BR/class.dateinterval.php
<?php
$startTime = DateTime::createFromFormat("H:i", "09:00");
$endTime = DateTime::createFromFormat("H:i", "22:00");
$interval = DateInterval::createFromDateString("30 minutes");
$result = [];
while ($startTime <= $endTime) {
$result[$startTime->format('H:i')] = $startTime->add($interval)->format('H:i');
}
echo print_r($result, true);
You can add 30 min to by this command :
$time=date("H:i", strtotime('+30 minutes', $time));
For your problem can set a while loop(for check condition).
Then set and array and set index to :
date("H:i", strtotime('+'.$interval.' minutes', $time));`
And set value to :
date("H:i", strtotime('+'.(2*$interval).'minutes', $time));
Then update $time.
You should just be able to use a 'while' loop to increment a timestamp until $endtime is reached. Here's a working example:
$starttime = '09:00'; // Start time
$endtime = '21:00'; // End time
$interval = '30'; // In minutes
$result = [];
$last = strtotime($starttime);
$endtimestamp = strtotime($endtime);
while ($last < $endtimestamp) {
$next = strtotime("+{$interval} minutes", $last);
$result[date('H:i', $last)] = date('H:i', $next);
$last = $next;
}
var_dump($result);
Can be done using DateTime and DateInterval
<?php
date_default_timezone_set('UTC');
// define start/end
$begin = DateTime::createFromFormat('H:i', '09:00');
$end = DateTime::createFromFormat('H:i', '21:00');
// define the interval
$interval = new DateInterval('PT30M');
$interval->invert = 1;
// get date range
$daterange = new DatePeriod($begin, $interval, $end);
// loop and build your array
$range = [];
foreach ($daterange as $date){
$range[$date->format("H:i")] = $date->sub($interval)->format("H:i");
}
print_r($range);
Result:
Array
(
[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] => 14:00
[14:00] => 14:30
[14:30] => 15:00
[15:00] => 15:30
[15:30] => 16:00
[16:00] => 16:30
[16:30] => 17:00
[17:00] => 17:30
[17:30] => 18:00
[18:00] => 18:30
[18:30] => 19:00
[19:00] => 19:30
[19:30] => 20:00
[20:00] => 20:30
[20:30] => 21:00
)
https://3v4l.org/FqViY
Related
I made this Code to make time slots:
$duration = 60;
$cleanup = 0;
$start = "10:00";
$end = '24:00';
function timeslots($duration, $cleanup, $start, $end)
{
$start = new DateTime($start);
$end = new DateTime($end);
$interval = new DateInterval('PT' . $duration . 'M');
$cleanupinterval = new DateInterval('PT' . $cleanup . 'M');
$slots = array();
for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupinterval)) {
$endperiod = clone $intStart;
$endperiod->add($interval);
if ($endperiod > $end) {
break;
}
$slots[] = $intStart->format('H:iA') . '-' . $endperiod->format('H:iA');
}
return $slots;
}
?>
above code get results like this:
10:00 AM - 11:00 AM.
11:00 AM - 12:00 PM.
what I'm trying to do is something like this:
12:00 AM - 01:00 AM
01:00 AM - 02:00 AM
3. 10:00 AM - 11:00 AM
11:00 AM - 12:00 PM.
So I'm trying to make break time from 02:00 AM to 10:00 AM
so the day will start like:
12:00 AM To 02:00 AM and take breake hours from 02:00 AM To 10:00 AM.
Thanks in advance.
I Hope this will help You:
$duration = 60;
$cleanup = 0;
$start = "00:00";
$end = '24:00';
$break_start = '02:00'; // break start
$break_end = '10:00'; // break end
function timeslots($duration, $cleanup, $start, $end, $break_start, $break_end)
{
$start = new DateTime($start);
$end = new DateTime($end);
$break_start = new DateTime($break_start);
$break_end = new DateTime($break_end);
$interval = new DateInterval('PT' . $duration . 'M');
$cleanupinterval = new DateInterval('PT' . $cleanup . 'M');
$slots = array();
for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupinterval)) {
$endperiod = clone $intStart;
$endperiod->add($interval);
if (strtotime($break_start->format('H:i A')) < strtotime($endperiod->format('H:i A')) && strtotime($endperiod->format('H:i A')) < strtotime($break_end->format('H:i A'))) {
$endperiod = $break_start;
$slots[] = $intStart->format('H:i A') . ' - ' . $endperiod->format('H:i A');
$intStart = $break_end;
$endperiod = $break_end;
$intStart->sub($interval);
}
$slots[] = $intStart->format('H:iA') . '-' . $endperiod->format('H:iA');
}
return $slots;
}
and
$time_slots = timeslots($duration, $cleanup, $start, $end, $break_start, $break_end);
echo '<pre>';
print_r($time_slots);
echo '<pre>';
the result will be :
Array
(
[0] => 00:00 AM - 01:00 AM
[1] => 01:00 AM - 02:00 AM
[2] => 02:00 AM - 02:00 AM
[3] => 10:00 AM - 11:00 AM
[4] => 11:00 AM - 12:00 PM
[5] => 12:00 PM - 13:00 PM
[6] => 13:00 PM - 14:00 PM
[7] => 14:00 PM - 15:00 PM
[8] => 15:00 PM - 16:00 PM
[9] => 16:00 PM - 17:00 PM
[10] => 17:00 PM - 18:00 PM
[11] => 18:00 PM - 19:00 PM
[12] => 19:00 PM - 20:00 PM
[13] => 20:00 PM - 21:00 PM
[14] => 21:00 PM - 22:00 PM
[15] => 22:00 PM - 23:00 PM
[16] => 23:00 PM - 00:00 AM
)
Something like that?
function show_time_slots($start_time, $end_time, $duration, $break){
$time_slots = array();
$start_time = strtotime($start_time);
$end_time = strtotime($end_time);
$add_mins = $duration * 60;
while ($start_time <= $end_time)
{
$time_slots[] = date("H:i", $start_time);
$start_time += $add_mins;
}
$time_slots = array_diff( $time_slots, $break );
return $time_slots;
}
And
$start_time = '00:00';
$end_time = '23:00';
$duration = '60';
$break = ['03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00'];
$time_slots = show_time_slots($start_time, $end_time, $duration, $break);
echo '<pre>';
print_r($time_slots);
echo '</pre>';
Return
Array
(
[0] => 00:00
[1] => 01:00
[2] => 02:00
[10] => 10:00
[11] => 11:00
[12] => 12:00
[13] => 13:00
[14] => 14:00
[15] => 15:00
[16] => 16:00
[17] => 17:00
[18] => 18:00
[19] => 19:00
[20] => 20:00
[21] => 21:00
[22] => 22:00
[23] => 23:00
)
I tried to make a function that will help you to make all possible slots. the function will take the start_time and end_time and based on the duration(in minutes) it will make the slots and it will not create a slot during break time also you can add clean-up or refresh time after every slot if you want that argument will take time in minutes.
I also see that the approved answer of #Khaled Mostafa where copied by a lot of bloggers and they just copy and paste his answer in the same requirement as mine but it didn't help me in this kind of my requirement so after spending some hours I have found this solution.
function show_time_slots($start_time, $end_time, $duration, $break_start, $break_end, $cleanup = 0){
$time_slots = array();
$start_time = strtotime($start_time);
$end_time = strtotime($end_time);
$break_start = strtotime($break_start);
$break_end = strtotime($break_end);
$add_mins = ($duration + $cleanup) * 60;
while ($start_time <= $end_time)
{
$startSlotTime = date("h:i A", $start_time); // for 24 format date("H:i", $start_time);
$slotEndTime = $start_time + $add_mins;
$endSlotTime = date("h:i A", $slotEndTime); // for 24 format date("H:i", $slotEndTime);
// $sachin = strtotime($startSlotTime);
if($slotEndTime <= $end_time){
if(
($start_time > $break_start && $start_time < $break_end ) || ($slotEndTime > $break_start && $slotEndTime < $break_end )
|| ($break_start > $start_time && $break_start < $slotEndTime) || ($break_end > $start_time && $break_end < $slotEndTime)
){
$start_time = $break_end;
}else{
$time_slots[] = $startSlotTime . "-" . $endSlotTime;
$start_time += $add_mins;
}
}else{
$start_time += $add_mins;
}
}
// $time_slots = array_diff( $time_slots, $break );
return $time_slots;
}
$duration = 180; // minutes
$cleanup = 0; // minutes
$start_time = "10:00";
$end_time = '23:00';
$break_start = '16:00'; // break start
$break_end = '16:30'; // break end
$time_slots = show_time_slots($start_time, $end_time, $duration, $break_start, $break_end, $cleanup);
echo '<pre>';
print_r($time_slots);
echo '</pre>';
Out Put
Array
(
[0] => 10:00 AM-01:00 PM
[1] => 01:00 PM-04:00 PM
[2] => 04:30 PM-07:30 PM
[3] => 07:30 PM-10:30 PM
)
I am getting solution for multiple break too. pleas check my answer and tell me if anything wrong with this. no doubt I am using code of #Sachin Sarola I just changes few line as I required for multiple break slot.
function show_time_slots($start_time, $end_time, $duration, $break_arr, $cleanup = 0){
$time_slots = array();
$start_time = strtotime($start_time);
$end_time = strtotime($end_time);
$add_mins = ($duration + $cleanup) * 60;
while ($start_time <= $end_time)
{
$startSlotTime = date("h:i A", $start_time); // for 24 format date("H:i", $start_time);
$slotEndTime = $start_time + $add_mins;
$endSlotTime = date("h:i A", $slotEndTime); // for 24 format date("H:i", $slotEndTime);
if($slotEndTime <= $end_time){
$is_break = false;
$last_break_end_time = null;
foreach($break_arr as $key => $break){
$break_start = strtotime($break['break_start']);
$break_end = strtotime($break['break_end']);
if(
($start_time > $break_start && $start_time < $break_end ) || ($slotEndTime > $break_start && $slotEndTime < $break_end )
|| ($break_start > $start_time && $break_start < $slotEndTime) || ($break_end > $start_time && $break_end < $slotEndTime)
){
$is_break = true;
$last_break_end_time = $break_end;
break;
}
}
if($is_break && $last_break_end_time){
$start_time = $last_break_end_time;
}else{
$time_slots[] = $startSlotTime . "-" . $endSlotTime;
$start_time += $add_mins;
}
}else{
$start_time += $add_mins;
}
}
// $time_slots = array_diff( $time_slots, $break );
return $time_slots;
}
$duration = 180;
$cleanup = 0;
$start_time = "10:00";
$end_time = '23:00';
$break_start = '16:00'; // break start
$break_end = '16:30'; // break end
$break_arr = [
['break_start' => '16:00', 'break_end' => '16:30'],
['break_start' => '19:30', 'break_end' => '20:00']
];
$time_slots = show_time_slots($start_time, $end_time, $duration, $break_arr, $cleanup);
echo '<pre>';
print_r($time_slots);
echo '</pre>';
Out Put
Array
(
[0] => 10:00 AM-01:00 PM
[1] => 01:00 PM-04:00 PM
[2] => 04:30 PM-07:30 PM
[3] => 08:00 PM-11:00 PM
)
I needed a list of times like so in an array...
12am
12:30am
1:00pm
...
How can I do this with PHP?
Here's an improved version of Alex's function that uses seconds for more precision:
function hoursRange( $lower = 0, $upper = 86400, $step = 3600, $format = '' ) {
$times = array();
if ( empty( $format ) ) {
$format = 'g:i a';
}
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;
}
So, to make an array of times with 1-hour intervals over a 24-hour time period, use the defaults:
hoursRange();
Which will give you the following:
Array
(
[00:00] => 12:00 am
[01:00] => 1:00 am
[02:00] => 2:00 am
[03:00] => 3:00 am
[04:00] => 4:00 am
[05:00] => 5:00 am
[06:00] => 6:00 am
[07:00] => 7:00 am
[08:00] => 8:00 am
[09:00] => 9:00 am
[10:00] => 10:00 am
[11:00] => 11:00 am
[12:00] => 12:00 pm
[13:00] => 1:00 pm
[14:00] => 2:00 pm
[15:00] => 3:00 pm
[16:00] => 4:00 pm
[17:00] => 5:00 pm
[18:00] => 6:00 pm
[19:00] => 7:00 pm
[20:00] => 8:00 pm
[21:00] => 9:00 pm
[22:00] => 10:00 pm
[23:00] => 11:00 pm
)
Here are a few example uses:
// Every 15 Minutes, All Day Long
$range = hoursRange( 0, 86400, 60 * 15 );
// Every 30 Minutes from 8 AM - 5 PM, using Custom Time Format
$range = hoursRange( 28800, 61200, 60 * 30, 'h:i a' );
You can view a working snippet at CodePad.
Thank-you for reopening the question alex.
This is a solution that should resonate with functional programmers.
function halfHourTimes() {
$formatter = function ($time) {
if ($time % 3600 == 0) {
return date('ga', $time);
} else {
return date('g:ia', $time);
}
};
$halfHourSteps = range(0, 47*1800, 1800);
return array_map($formatter, $halfHourSteps);
}
I decided this one was better :)
function hoursRange($lower = 0, $upper = 23, $step = 1, $format = NULL) {
if ($format === NULL) {
$format = 'g:ia'; // 9:30pm
}
$times = array();
foreach(range($lower, $upper, $step) as $increment) {
$increment = number_format($increment, 2);
list($hour, $minutes) = explode('.', $increment);
$date = new DateTime($hour . ':' . $minutes * .6);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
We can simply use strtotime to increment our time by N amount here in this case 30 minutes and date function to format it to our desired output.
$startTime = strtotime('12 am');
$endTime = strtotime('11:59 pm');
$arrInterval = [];
while($endTime >= $startTime){
$arrInterval[] = date("h:ia", $startTime);
$startTime = strtotime('+30 minutes', $startTime);
}
Here is my suggestion :
$start = new \DateTime('00:00');
$times = 24 * 2; // 24 hours * 30 mins in an hour
for ($i = 0; $i < $times-1; $i++) {
$result[] = $start->add(new \DateInterval('PT30M'))->format('H:i A');
}
print_r($result);
Hope this help.
This is maybe a more elegant way, but it requires the times to be in seconds (that also makes it more flexible).
function time_range( $start, $end, $step = 1800 ) {
$return = array();
for( $time = $start; $time <= $end; $time += $step )
$return[] = date( 'g:ia', $time );
return $return;
}
Simplest solution
$h = 0;
while ($h < 24) {
$key = date('H:i', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$value = date('h:i A', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$formatter[$key] = $value;
$h++;
}
Result are:
Array
(
[00:00] => 12:00 AM
[01:00] => 01:00 AM
[02:00] => 02:00 AM
[03:00] => 03:00 AM
[04:00] => 04:00 AM
[05:00] => 05:00 AM
[06:00] => 06:00 AM
[07:00] => 07:00 AM
[08:00] => 08:00 AM
[09:00] => 09:00 AM
[10:00] => 10:00 AM
[11:00] => 11:00 AM
[12:00] => 12:00 PM
[13:00] => 01:00 PM
[14:00] => 02:00 PM
[15:00] => 03:00 PM
[16:00] => 04:00 PM
[17:00] => 05:00 PM
[18:00] => 06:00 PM
[19:00] => 07:00 PM
[20:00] => 08:00 PM
[21:00] => 09:00 PM
[22:00] => 10:00 PM
[23:00] => 11:00 PM
)
Here's a more flexible version that doesn't need DateTime (since we're already working with timestamps in seconds anyway). ;-)
function get_hours_range( $start = 0, $end = 86400, $step = 3600, $format = 'g:i a' ) {
$times = array();
foreach ( range( $start, $end, $step ) as $timestamp ) {
$hour_mins = gmdate( 'H:i', $timestamp );
if ( ! empty( $format ) )
$times[$hour_mins] = gmdate( $format, $timestamp );
else $times[$hour_mins] = $hour_mins;
}
return $times;
}
Added index value as hour:
Here is the code:
<?php
$time_slot= array();
if ( empty( $format ) ) {
$format = 'H:i';
}
$lower = 0; $upper = 86400; $step = 3600; $format = '';
$i = 0;
foreach ( range( $lower, $upper, $step ) as $increment ) {
$increment = gmdate( 'H:i', $increment );
$time_slot[$i] = $increment;
$i++;
}
print_r($time_slot);
?>
Here is the result:
Array
(
[0] => 00:00
[1] => 01:00
[2] => 02:00
[3] => 03:00
[4] => 04:00
[5] => 05:00
[6] => 06:00
[7] => 07:00
[8] => 08:00
[9] => 09:00
[10] => 10:00
[11] => 11:00
[12] => 12:00
[13] => 13:00
[14] => 14:00
[15] => 15:00
[16] => 16:00
[17] => 17:00
[18] => 18:00
[19] => 19:00
[20] => 20:00
[21] => 21:00
[22] => 22:00
[23] => 23:00
[24] => 00:00
)
Am trying to get hours from midnight from the current time
that is suppose now it's 02:34 then I expect to get
02:34 - 02:00,
01:00 - 02:00,
00:00 - 01:00
as an array
so I have tried
function getFromMidnight(){
$durations = [];
$currentTime = strtotime("now");
$iStartOfHour = $currentTime - ($currentTime % 3600);
$midnight = strtotime('today midnight');
$hours = floor(($iStartOfHour - $midnight)/3600);
array_push($durations,["from"=>$currentTime, "to"=>$iStartOfHour]);
if(floor(($iStartOfHour - $midnight)/3600) > 0){
for ($val = 1;$val <= $hours;$val++){
$newval = $val +=1;
array_push($durations, ["from"=>strtotime('- '.$val.' hours',$iStartOfHour),"to"=>strtotime('- '.$newval.' hours',$iStartOfHour)]);
}
}
return $durations;
}
For the first array has the correct durations e.g. from the above example 02:34-02:00 but the next arrays are messed up giving me wrong values with constant timestamps eg: 01:00 - 01:00
I suspect its my for loop with an error, what could be wrong?
I would not use that code, instead of working things out just use DateInterval inverted and work backwards. Then sub() the hour in the loop to get the offset.
<?php
date_default_timezone_set('UTC');
$begin = new DateTime('today midnight');
$end = new DateTime();
$interval = new DateInterval('PT60M');
$interval->invert = 1;
$daterange = new DatePeriod($begin, $interval, $end);
$range = [];
foreach ($daterange as $date){
$range[] = [
'from' => $date->format("H:i"),
'to' => $date->sub($interval)->format("H:i")
];
}
print_r($range);
https://3v4l.org/BMSbI
Result:
Array
(
[0] => Array
(
[from] => 00:00
[to] => 01:00
)
[1] => Array
(
[from] => 01:00
[to] => 02:00
)
[2] => Array
(
[from] => 02:00
[to] => 03:00
)
[3] => Array
(
[from] => 03:00
[to] => 04:00
)
)
I needed a list of times like so in an array...
12am
12:30am
1:00pm
...
How can I do this with PHP?
Here's an improved version of Alex's function that uses seconds for more precision:
function hoursRange( $lower = 0, $upper = 86400, $step = 3600, $format = '' ) {
$times = array();
if ( empty( $format ) ) {
$format = 'g:i a';
}
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;
}
So, to make an array of times with 1-hour intervals over a 24-hour time period, use the defaults:
hoursRange();
Which will give you the following:
Array
(
[00:00] => 12:00 am
[01:00] => 1:00 am
[02:00] => 2:00 am
[03:00] => 3:00 am
[04:00] => 4:00 am
[05:00] => 5:00 am
[06:00] => 6:00 am
[07:00] => 7:00 am
[08:00] => 8:00 am
[09:00] => 9:00 am
[10:00] => 10:00 am
[11:00] => 11:00 am
[12:00] => 12:00 pm
[13:00] => 1:00 pm
[14:00] => 2:00 pm
[15:00] => 3:00 pm
[16:00] => 4:00 pm
[17:00] => 5:00 pm
[18:00] => 6:00 pm
[19:00] => 7:00 pm
[20:00] => 8:00 pm
[21:00] => 9:00 pm
[22:00] => 10:00 pm
[23:00] => 11:00 pm
)
Here are a few example uses:
// Every 15 Minutes, All Day Long
$range = hoursRange( 0, 86400, 60 * 15 );
// Every 30 Minutes from 8 AM - 5 PM, using Custom Time Format
$range = hoursRange( 28800, 61200, 60 * 30, 'h:i a' );
You can view a working snippet at CodePad.
Thank-you for reopening the question alex.
This is a solution that should resonate with functional programmers.
function halfHourTimes() {
$formatter = function ($time) {
if ($time % 3600 == 0) {
return date('ga', $time);
} else {
return date('g:ia', $time);
}
};
$halfHourSteps = range(0, 47*1800, 1800);
return array_map($formatter, $halfHourSteps);
}
I decided this one was better :)
function hoursRange($lower = 0, $upper = 23, $step = 1, $format = NULL) {
if ($format === NULL) {
$format = 'g:ia'; // 9:30pm
}
$times = array();
foreach(range($lower, $upper, $step) as $increment) {
$increment = number_format($increment, 2);
list($hour, $minutes) = explode('.', $increment);
$date = new DateTime($hour . ':' . $minutes * .6);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
We can simply use strtotime to increment our time by N amount here in this case 30 minutes and date function to format it to our desired output.
$startTime = strtotime('12 am');
$endTime = strtotime('11:59 pm');
$arrInterval = [];
while($endTime >= $startTime){
$arrInterval[] = date("h:ia", $startTime);
$startTime = strtotime('+30 minutes', $startTime);
}
Here is my suggestion :
$start = new \DateTime('00:00');
$times = 24 * 2; // 24 hours * 30 mins in an hour
for ($i = 0; $i < $times-1; $i++) {
$result[] = $start->add(new \DateInterval('PT30M'))->format('H:i A');
}
print_r($result);
Hope this help.
This is maybe a more elegant way, but it requires the times to be in seconds (that also makes it more flexible).
function time_range( $start, $end, $step = 1800 ) {
$return = array();
for( $time = $start; $time <= $end; $time += $step )
$return[] = date( 'g:ia', $time );
return $return;
}
Simplest solution
$h = 0;
while ($h < 24) {
$key = date('H:i', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$value = date('h:i A', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$formatter[$key] = $value;
$h++;
}
Result are:
Array
(
[00:00] => 12:00 AM
[01:00] => 01:00 AM
[02:00] => 02:00 AM
[03:00] => 03:00 AM
[04:00] => 04:00 AM
[05:00] => 05:00 AM
[06:00] => 06:00 AM
[07:00] => 07:00 AM
[08:00] => 08:00 AM
[09:00] => 09:00 AM
[10:00] => 10:00 AM
[11:00] => 11:00 AM
[12:00] => 12:00 PM
[13:00] => 01:00 PM
[14:00] => 02:00 PM
[15:00] => 03:00 PM
[16:00] => 04:00 PM
[17:00] => 05:00 PM
[18:00] => 06:00 PM
[19:00] => 07:00 PM
[20:00] => 08:00 PM
[21:00] => 09:00 PM
[22:00] => 10:00 PM
[23:00] => 11:00 PM
)
Here's a more flexible version that doesn't need DateTime (since we're already working with timestamps in seconds anyway). ;-)
function get_hours_range( $start = 0, $end = 86400, $step = 3600, $format = 'g:i a' ) {
$times = array();
foreach ( range( $start, $end, $step ) as $timestamp ) {
$hour_mins = gmdate( 'H:i', $timestamp );
if ( ! empty( $format ) )
$times[$hour_mins] = gmdate( $format, $timestamp );
else $times[$hour_mins] = $hour_mins;
}
return $times;
}
Added index value as hour:
Here is the code:
<?php
$time_slot= array();
if ( empty( $format ) ) {
$format = 'H:i';
}
$lower = 0; $upper = 86400; $step = 3600; $format = '';
$i = 0;
foreach ( range( $lower, $upper, $step ) as $increment ) {
$increment = gmdate( 'H:i', $increment );
$time_slot[$i] = $increment;
$i++;
}
print_r($time_slot);
?>
Here is the result:
Array
(
[0] => 00:00
[1] => 01:00
[2] => 02:00
[3] => 03:00
[4] => 04:00
[5] => 05:00
[6] => 06:00
[7] => 07:00
[8] => 08:00
[9] => 09:00
[10] => 10:00
[11] => 11:00
[12] => 12:00
[13] => 13:00
[14] => 14:00
[15] => 15:00
[16] => 16:00
[17] => 17:00
[18] => 18:00
[19] => 19:00
[20] => 20:00
[21] => 21:00
[22] => 22:00
[23] => 23:00
[24] => 00:00
)
I needed a list of times like so in an array...
12am
12:30am
1:00pm
...
How can I do this with PHP?
Here's an improved version of Alex's function that uses seconds for more precision:
function hoursRange( $lower = 0, $upper = 86400, $step = 3600, $format = '' ) {
$times = array();
if ( empty( $format ) ) {
$format = 'g:i a';
}
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;
}
So, to make an array of times with 1-hour intervals over a 24-hour time period, use the defaults:
hoursRange();
Which will give you the following:
Array
(
[00:00] => 12:00 am
[01:00] => 1:00 am
[02:00] => 2:00 am
[03:00] => 3:00 am
[04:00] => 4:00 am
[05:00] => 5:00 am
[06:00] => 6:00 am
[07:00] => 7:00 am
[08:00] => 8:00 am
[09:00] => 9:00 am
[10:00] => 10:00 am
[11:00] => 11:00 am
[12:00] => 12:00 pm
[13:00] => 1:00 pm
[14:00] => 2:00 pm
[15:00] => 3:00 pm
[16:00] => 4:00 pm
[17:00] => 5:00 pm
[18:00] => 6:00 pm
[19:00] => 7:00 pm
[20:00] => 8:00 pm
[21:00] => 9:00 pm
[22:00] => 10:00 pm
[23:00] => 11:00 pm
)
Here are a few example uses:
// Every 15 Minutes, All Day Long
$range = hoursRange( 0, 86400, 60 * 15 );
// Every 30 Minutes from 8 AM - 5 PM, using Custom Time Format
$range = hoursRange( 28800, 61200, 60 * 30, 'h:i a' );
You can view a working snippet at CodePad.
Thank-you for reopening the question alex.
This is a solution that should resonate with functional programmers.
function halfHourTimes() {
$formatter = function ($time) {
if ($time % 3600 == 0) {
return date('ga', $time);
} else {
return date('g:ia', $time);
}
};
$halfHourSteps = range(0, 47*1800, 1800);
return array_map($formatter, $halfHourSteps);
}
I decided this one was better :)
function hoursRange($lower = 0, $upper = 23, $step = 1, $format = NULL) {
if ($format === NULL) {
$format = 'g:ia'; // 9:30pm
}
$times = array();
foreach(range($lower, $upper, $step) as $increment) {
$increment = number_format($increment, 2);
list($hour, $minutes) = explode('.', $increment);
$date = new DateTime($hour . ':' . $minutes * .6);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
We can simply use strtotime to increment our time by N amount here in this case 30 minutes and date function to format it to our desired output.
$startTime = strtotime('12 am');
$endTime = strtotime('11:59 pm');
$arrInterval = [];
while($endTime >= $startTime){
$arrInterval[] = date("h:ia", $startTime);
$startTime = strtotime('+30 minutes', $startTime);
}
Here is my suggestion :
$start = new \DateTime('00:00');
$times = 24 * 2; // 24 hours * 30 mins in an hour
for ($i = 0; $i < $times-1; $i++) {
$result[] = $start->add(new \DateInterval('PT30M'))->format('H:i A');
}
print_r($result);
Hope this help.
This is maybe a more elegant way, but it requires the times to be in seconds (that also makes it more flexible).
function time_range( $start, $end, $step = 1800 ) {
$return = array();
for( $time = $start; $time <= $end; $time += $step )
$return[] = date( 'g:ia', $time );
return $return;
}
Simplest solution
$h = 0;
while ($h < 24) {
$key = date('H:i', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$value = date('h:i A', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$formatter[$key] = $value;
$h++;
}
Result are:
Array
(
[00:00] => 12:00 AM
[01:00] => 01:00 AM
[02:00] => 02:00 AM
[03:00] => 03:00 AM
[04:00] => 04:00 AM
[05:00] => 05:00 AM
[06:00] => 06:00 AM
[07:00] => 07:00 AM
[08:00] => 08:00 AM
[09:00] => 09:00 AM
[10:00] => 10:00 AM
[11:00] => 11:00 AM
[12:00] => 12:00 PM
[13:00] => 01:00 PM
[14:00] => 02:00 PM
[15:00] => 03:00 PM
[16:00] => 04:00 PM
[17:00] => 05:00 PM
[18:00] => 06:00 PM
[19:00] => 07:00 PM
[20:00] => 08:00 PM
[21:00] => 09:00 PM
[22:00] => 10:00 PM
[23:00] => 11:00 PM
)
Here's a more flexible version that doesn't need DateTime (since we're already working with timestamps in seconds anyway). ;-)
function get_hours_range( $start = 0, $end = 86400, $step = 3600, $format = 'g:i a' ) {
$times = array();
foreach ( range( $start, $end, $step ) as $timestamp ) {
$hour_mins = gmdate( 'H:i', $timestamp );
if ( ! empty( $format ) )
$times[$hour_mins] = gmdate( $format, $timestamp );
else $times[$hour_mins] = $hour_mins;
}
return $times;
}
Added index value as hour:
Here is the code:
<?php
$time_slot= array();
if ( empty( $format ) ) {
$format = 'H:i';
}
$lower = 0; $upper = 86400; $step = 3600; $format = '';
$i = 0;
foreach ( range( $lower, $upper, $step ) as $increment ) {
$increment = gmdate( 'H:i', $increment );
$time_slot[$i] = $increment;
$i++;
}
print_r($time_slot);
?>
Here is the result:
Array
(
[0] => 00:00
[1] => 01:00
[2] => 02:00
[3] => 03:00
[4] => 04:00
[5] => 05:00
[6] => 06:00
[7] => 07:00
[8] => 08:00
[9] => 09:00
[10] => 10:00
[11] => 11:00
[12] => 12:00
[13] => 13:00
[14] => 14:00
[15] => 15:00
[16] => 16:00
[17] => 17:00
[18] => 18:00
[19] => 19:00
[20] => 20:00
[21] => 21:00
[22] => 22:00
[23] => 23:00
[24] => 00:00
)