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
)
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
)
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
I have a select box with time interval of 15 mins starting from 9 to 8pm which is generated by using below code.
function hoursRange($lower = 9, $upper = 20, $step = .25, $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;
}
$timeArr = hoursRange();
$array = array_values($timeArr);
echo "<select>";
echo "<option value=''>--Select--</option>";
foreach($array as $key=>$value){
$disabled = '';
echo "<option value=".$key." $disabled>".$value."</option>";
}
echo "</select>";
It gives me an select box with options 9:00, 9:15 etc like below
Array
(
[0] => 9:00am
[1] => 9:15am
[2] => 9:30am
[3] => 9:45am
[4] => 10:00am
[5] => 10:15am
[6] => 10:30am
[7] => 10:45am
[8] => 11:00am
[9] => 11:15am
[10] => 11:30am
[11] => 11:45am
[12] => 12:00pm
[13] => 12:15pm
[14] => 12:30pm
[15] => 12:45pm
[16] => 1:00pm
[17] => 1:15pm
[18] => 1:30pm
[19] => 1:45pm
[20] => 2:00pm
[21] => 2:15pm
[22] => 2:30pm
[23] => 2:45pm
[24] => 3:00pm
[25] => 3:15pm
[26] => 3:30pm
[27] => 3:45pm
[28] => 4:00pm
[29] => 4:15pm
[30] => 4:30pm
[31] => 4:45pm
[32] => 5:00pm
[33] => 5:15pm
[34] => 5:30pm
[35] => 5:45pm
[36] => 6:00pm
[37] => 6:15pm
[38] => 6:30pm
[39] => 6:45pm
[40] => 7:00pm
[41] => 7:15pm
[42] => 7:30pm
[43] => 7:45pm
[44] => 8:00pm
)
Note: now my issue is that I have to disabled some options from select box by a condition
current time = 11 am => than all options earlier to this will be disabled
current time = 2pm => all options earlier to this should be disabled
current time = 3:30pm or 3:48pm then all options before 4 pm should be disabled.
For this I have tried this
date_default_timezone_set('Asia/Kolkata');
$curretHour = date("g");
echo "<select>";
foreach($array as $key=>$value){
$newArr = explode(':', $value);
if(($newArr[0] == $currentHour) || $newArr[0] <$currentHour){
$dis = 'disabled = disabled';
}else{
$dis = '';
}
echo "<option value=".$key." $dis>".$value."</option>";
}
echo "</select>";
but it's not working.
The best and most readable solution is to use DateTime class. I suggest to create current DateTime first. In the foreach loop you have to create DateTime foreach time and compare with the current one. Possible solution:
<?php
function hoursRange($lower = 9, $upper = 20, $step = .25, $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;
}
$timeArr = hoursRange();
$array = array_values($timeArr);
echo "<select>";
echo "<option value=''>--Select--</option>";
$currentDateTime = new \DateTime();
foreach($array as $key=>$value){
$dateTime = \DateTime::createFromFormat('g:ia', $value);
$disabled = $dateTime <= $currentDateTime;
echo "<option value=".$key." $disabled>".$value."</option>";
}
echo "</select>";
I find this as a soultion to my problem
--Selects--
11:00 am
11:15 am
11:30 am
11:45 am
12:00 pm
12:15 pm
12:30 pm
12:45 pm
01:00 pm
01:15 pm
01:30 pm
01:45 pm
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
-->
<?php
//date_default_timezone_set('Asia/Kolkata');
//echo date("g");
function hoursRange($lower = 9, $upper = 20, $step = .25, $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;
}
$currentHour = 9;
$array = $timeArr = hoursRange();
//$array = array_values($timeArr);
echo "<select>";
echo "<option value=''>--Select--</option>";
$i = 0;
foreach($array as $key=>$value){
$newArr = explode('.', $key);
if(($newArr[0] == $currentHour) || $newArr[0] <$currentHour){
$disabled = 'disabled = disabled';
}else{
$disabled = '';
}
//$disabled = '';
echo "<option value=".$i." $disabled>".$value."</option>";
$i++;
}
echo "</select>";
echo "<pre>";print_r($array);
///js
<select id="test" name="test">
<option value="1">9:00 am</option>
<option value="2">10:00 am</option>
<option value="3">11:00 am</option>
<option value="4">8:00 pm</option>
</select>
<script>
var selectbox = document.getElementById('test');
//var opt = ['9:00 am', '10:00 am','11:00 am','8:00 pm'];
//var opt = ['00:00'=>'12:00 am','10:00'=>'10:00 am'];
var opt = {}; // no need for an array
opt["900"] = '09:00 am';
opt["1000"] = '10:00 am';
opt["1100"] = '11:00 am';
opt["1200"] = '12:00 pm';
opt["1300"] = '01:00 pm';
opt["1400"] = '02:00 pm';
var curtime = '1400';
var str = curtime.split(':');
//console.log(opt);
selectbox.onchange = function(){
//alert('here');
var i=0;
var list = '';
for (var key in opt) {
console.log('key');
if(curtime <= key){
console.log('if');
var dis = 'disabled';
console.log(opt[key]);
}
list += '<option '+dis+' value='+i+'>'+opt[key]+'</option>';
//console.log(key);
i++;
}
selectbox.innerHTML = list;
};
</script>
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
)