how can I create a list of times in php? - php

I have x.
x is minutes.
I have a string start_time like: 7:00
And I have a string end_time like: 14:00
how can print a list of time with increase x minutes for each loop?
I want print something like this:
if x = 30
7:00 - 7:30
7:30 - 8:00
...
13:30 - 14:00
I try do it with math functions in php like this:
$time = '7:00';
$mm = $hh = 0;
$str = explode(":",$time);
if(($str[1]+ $x) > 60)
{
...
}
but it there a more simple method? can date function in php do it?

You can use DatePeriod together with DateInterval to achieve this.
Create two DateTime intervals with your start and end times, and a DateInterval instance with the number of minutes you need. Then, create a DatePeriod with this information, and iterate over it to show the resulting times:
<?php
$minutes = 15;
$start = "07:00";
$end = "14:00";
$startDate = DateTime::createFromFormat("H:i", $start);
$endDate = DateTime::createFromFormat("H:i", $end);
$interval = new DateInterval("PT".$minutes."M");
$dateRange = new DatePeriod($startDate, $interval, $endDate);
foreach ($dateRange as $date) {
echo $date->format("H:i")."<br>";
}
Demo
Result
07:00
07:15
07:30
07:45
08:00
08:15
08:30
08:45
09:00
09:15
09:30
09:45
10:00
10:15
// etc

You can use the DatePeriod object along with a desired DateInterval object.
Example: https://3v4l.org/lg8QW
$start = new \DateTime('07:00');
$end = new \DateTime('14:00');
$interval = new \DateInterval('PT1M'); //change to desired interval
$periods = new \DatePeriod($start, $interval, $end);
foreach ($periods as $period) {
echo $period->format('H:i') ;
}
Result:
07:00
07:01
07:02
07:03
07:04
07:05
07:06
07:07
07:08
07:09
07:10
07:11
07:12
07:13
07:14
07:15
07:16
07:17
07:18
07:19
07:20
//...
13:59

You can use DateTime object.
just like this:
<?php
$start_time = '7:00';
$date1 = DateTime::createFromFormat('H:i', $start_time);
$old = $start_time;
while (true) {
$date1->modify('+30 min');
echo $old . '-' . $date1->format('H:i').PHP_EOL;
$old = $date1->format('H:i');
if ($old == '14:00') {
break;
}
}
Output:
7:00-07:30
07:30-08:00
08:00-08:30
...
13:30-14:00
More info just see the manual here: DateTime

Related

DatePeriod does not show 00:00

I create an attendance system for employees working in a company and I need to check the time intervals while adding records. So I have to create an array of hours. With the DatePeriod, I output hours in a certain time interval. It works but does not show anything when 00:00 is in two different time intervals.
Normally it should output the following range:
00:00
01:00
02:00
03:00
04:00
But shows nothing.
<?php
$a = '23:00';
$b = '05:00';
$period = new DatePeriod(
new DateTime($a),
new DateInterval('PT1H'),
new DateTime($b),
DatePeriod::EXCLUDE_START_DATE
);
foreach ($period as $date) {
echo $date->format("H:i\n");
}
?>
Thank you
If you dump dates you pass to DatePeriod you will see they are both today so your start date is after the end date. And really you mean in this case: give me hours between today 23:00 and tomorrow 05:00 - so if this happen just add 1 day to your end date.
<?php
$a = '23:00';
$b = '05:00';
$aDate = new DateTime($a);
$bDate = new DateTime($b);
if ($aDate > $bDate) {
$bDate->add(new DateInterval('P1D'));
}
$period = new DatePeriod(
$aDate,
new DateInterval('PT1H'),
$bDate,
DatePeriod::EXCLUDE_START_DATE
);
foreach ($period as $date) {
echo $date->format("H:i\n");
}
Output:
00:00
01:00
02:00
03:00
04:00

PHP Display Hour range today and tomorrow

I've got the tutorial from this post.
The PHP code:
$start = '20:00:00';
$end = '07:59:59'; //need to show until tomorrow time.
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
Now I need to show the time from 20:00:00 until tomorrow at 07:59:59.
Current code when I'm trying to run it got no result (empty).
The problem with your code is that it's just comparing the times not dates so obviously in your example the start time is greater than the end time.
Try the following example it works perfectly.
<?php
$TodayDate = date("Y-m-d");
$start = $TodayDate.' 20:00:00';
$TomorrowDate = new DateTime('tomorrow');
$TomorrowDate = $TomorrowDate->format('Y-m-d');
$end = $TomorrowDate.' 07:59:59'; //need to show until tomorrow time.
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
?>
20:00 - 20:30
20:30 - 21:00
21:00 - 21:30
21:30 - 22:00
22:00 - 22:30
22:30 - 23:00
23:00 - 23:30
23:30 - 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
You could use the DateTime class with it's associated methods - add ~ this would perhaps simplify the task? It is simple to modify the above to work with a pre-defined start time / end time
$now = new DateTime();
$end = new DateTime( date( DATE_ATOM, strtotime( 'now + 1day' ) ) );
while( $now->add( new DateInterval('PT30M') ) < $end ){
echo $now->format( 'H:i:s' ) . '<br />';
}
To use the actual desired start/end times
$start = date( DATE_ATOM, strtotime( 'today 8pm' ) );
$finish = date( DATE_ATOM, strtotime( sprintf( '%s + 1day',$start ) ) );
$now = new DateTime( $start);
$end = new DateTime( $finish );
while( $now->add( new DateInterval('PT30M') ) < $end ){
echo $now->format( 'H:i:s' ) . '<br />';
}
Just add some date.
Search for 08:00:00 of which day?
$start = '01/01/2019 20:00:00';
$end = '01/02/2019 08:00:00';
$time = strtotime($start);
$timeStop = strtotime($end);
while ($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}

Get time interval between two time using php with given duraion using php?

I have start time and end time in php.
If i give duration it should display all the time intervals.
Startime = 2014-07-28 07:00:00
End Time = 2014-07-28 11:00:00
duration = 30 min
i need output with 30 min difference between start and end time.
Output should look like :
07:00, 07:30 , 08:00 , 08:30 ..... 10:00 , 10:30 , 11:00
try
$s = strtotime("2014-07-28 07:00:00");
$e = strtotime("2014-07-28 11:00:00");
while($s != $e) {
$s = strtotime('+30 minutes', $s);
echo date('H:i', $s);
}
output :- 07:30 08:00 08:30 09:00 09:30 10:00 10:30 11:00
for comma seperated:-
while($s != $e) {
$s = strtotime('+30 minutes', $s);
$arr[] = date('H:i', $s);
}
echo implode(',', $arr);
output:- 07:30,08:00,08:30,09:00,09:30,10:00,10:30,11:00
Use the DatePeriod class for this:
$start = new DateTime('2014-07-28 07:00:00');
$end = new DateTime('2014-07-28 11:00:00');
$interval = new DateInterval('PT30M');
$period = new DatePeriod($start, $interval, $end);
foreach($period as $time) {
echo $time->format('Y-m-d H:i:s') . PHP_EOL;
}
Output:
2014-07-28 07:00:00
2014-07-28 07:30:00
2014-07-28 08:00:00
2014-07-28 08:30:00
2014-07-28 09:00:00
2014-07-28 09:30:00
2014-07-28 10:00:00
2014-07-28 10:30:00
you can try
$start = "2014-07-28 07:00:00";
$end = "2014-07-28 11:00:00";
$start_time = strtotime($start);
$end_time = strtotime($end);
$time_diff = 30 * 60;
for($i=$start_time; $i<=$end_time; $i+=$time_diff)
{
echo date("H:i", $i).", ";
}
See WORKING DEMO

PHP Create Timeslots with break timing using DatePeriod

I want to create time slots with start,end time & also break timing.
public function getServiceScheduleSlots($duration,$break, $stTime,$enTime)
{
$start = new DateTime($stTime);
$end = new DateTime($enTime);
$interval = new DateInterval("PT" . $duration. "M");
$period = new DatePeriod($start, $duration, $end);
foreach ($period as $dt) {
$periods[] = $dt->format('H:iA');
}
return $periods;
}
For ex.,
My service start time 10:00 AM , End Time 12:00 PM.
Condition: each service time 30 min & 15 min break.
Above method returns like,
10:00 AM - 10:30 AM
10:30 AM - 11:00 AM
11:00 AM - 11:30 AM
11:30 AM - 12:00 PM
Expected results as,
10:00 AM - 10:30 AM
10:45 AM - 11:15 AM
11:30 AM - 12:00 PM
I want to add break time when each period starts.
Thanks in advance.
How about this....
function getServiceScheduleSlots($duration,$break, $stTime,$enTime)
{
$start = new DateTime($stTime);
$end = new DateTime($enTime);
$interval = new DateInterval("PT" . $duration. "M");
$breakInterval = new DateInterval("PT" . $break. "M");
for ($intStart = $start;
$intStart < $end;
$intStart->add($interval)->add($breakInterval)) {
$endPeriod = clone $intStart;
$endPeriod->add($interval);
if ($endPeriod > $end) {
$endPeriod=$end;
}
$periods[] = $intStart->format('H:iA') .
' - ' .
$endPeriod->format('H:iA');
}
return $periods;
}

Calculate time period list between 2 time in php?

I want to generate 3 time-period-list column between two times.
if
$start_time = '08:00 AM';
$end_time = '10:00: PM';
Time Period List As:
Morning ----- Noon ----- Eve
8:00 AM 1:00 PM 6:00 PM
8:30 AM 1:30 PM 6:30 PM
9:00 AM 2:.0 PM 7:00 PM
to to to
... ... ...
12:00 PM 5:00 PM 10:00 PM
I have calculated times between $start_time and $end_time as:
$time = time();
$rounded_time = $time % 900 > 450 ? $time += (900 - $time % 900): $time -= $time % 900;
$start = strtotime('08:00 AM');
$end = strtotime('10:00 PM');
for( $i = $start; $i <= $end; $i += 1800)
{
echo "<input name='start_time' type='radio' value='".date('g:i A', $i)."' />"." ".date('g:i A', $i)."<br>";
}
Remaining work to divide these times in three column as mention above
Thanks in advance to all my mates.
You can use strtotime method to add and substract time period from given time. For example:
$time = strtotime('10:00');
$halfAnHourBefore = date("H:i", strtotime('-30 minutes', $time));
$halfAnHourAfter = date("H:i", strtotime('+30 minutes', $time));
would give $halfAnHourBefore as 09:30 and $halfAnHourAfter as 10:30

Categories