Calculate time period list between 2 time in php? - 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

Related

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/>';
}

how can I create a list of times in 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

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

Display Open/Closed based on day and time and location

I have ran through a few test examples I have found and tried to combined some answer but have been unsuccessful.
This is what I have so far & I will actually need to add another one for a second locations hours.
<?php
date_default_timezone_set('America/New_York');
$date = new DateTime;
echo date("D m/d/y h:i:s",time())
?>
<?php
$times = array(
'mon' => '7:30 AM - 12:00 PM, 12:30 PM - 4:30 PM',
'tue' => '7:30 AM - 12:00 PM, 12:30 PM - 6:30 PM',
'wed' => '7:30 AM - 12:00 PM, 3:30 PM - 4:30 PM',
'thu' => '7:30 AM - 12:00 PM, 12:30 PM - 6:30 PM',
'fri' => '7:30 AM - 4:30 PM',
'sat' => '7:30 AM - 12:00 PM',
'sun' => 'closed'
);
function compileHours($times, $timestamp) {
$times = $times[strtolower(date('D',$timestamp))];
if(!strpos($times, '-')) return array();
$hours = explode(",", $times);
$hours = array_map('explode', array_pad(array(),count($hours),'-'), $hours);
$hours = array_map('array_map', array_pad(array(),count($hours),'strtotime'), $hours, array_pad(array(),count($hours),array_pad(array(),2,$timestamp)));
end($hours);
if ($hours[key($hours)][0] > $hours[key($hours)][1]) $hours[key($hours)][1] = strtotime('+1 day', $hours[key($hours)][1]);
return $hours;
}
function isOpen($now, $times) {
$open = 0; // time until closing in seconds or 0 if closed
// merge opening hours of today and the day before
$hours = array_merge(compileHours($times, strtotime('yesterday',$now)),compileHours($times, $now));
foreach ($hours as $h) {
if ($now >= $h[0] and $now < $h[1]) {
$open = $h[1] - $now;
return $open;
}
}
return $open;
}
$now = strtotime('12:00am');
$open = isOpen($now, $times);
if ($open == 0) {
echo "Closed";
} else {
echo "Open";
}
?>
Here is the link to the test page I am mess with http://www.sullivanscrap.com/testtime.php Any suggestions would be greatly appreciated.
I believe your problem is that you are setting $now to midnight of today's morning, then checking that against your hour ranges. This will always return closed because none of your hour ranges contain that time. Changing this to $now = time() seems to yield the expected results.

loop with time math is incorrectly showing AM/PM

In this case hours_start will be 08:00:00 and hours_end will be 14:00:00
here's the code i'm using to generate a list of 30 minute time slots between start and end
while($row = $q->fetch()){
$hours = $row['hours_end'] - $row['hours_start']; //amount of hours working in day
for($i = 0; $i < $hours * 2; $i++){ // double hours for 30 minute increments
$minutes_to_add = 1800 * $i; // add 30 - 60 - 90 etc.
$timeslot = date('h:i:s', strtotime($row['hours_start'])+$minutes_to_add);
echo "
<tr>
<td>" . date('h:i A', strtotime($timeslot)) . "</td>
</tr>";
}
}
this is producing:
08:00 AM
08:30 AM
09:00 AM
09:30 AM
10:00 AM
10:30 AM
11:00 AM
11:30 AM
12:00 PM
12:30 PM
01:00 AM
01:30 AM
as you can see, it is functioning as (i) expected until it gets to (what should be) 1 PM then it switches back to AM. not sure whats going on here.
Its because you are setting $timeslot using h which is only a 12 hour format without appending am or pm. Then taking that 12 hour format and running it through strtotime which expects 24 hour format if am or pm is not present. Hence anything after 12 becomes am again.
You need to use:
$timeslot = date('H:i:s', strtotime($row['hours_start'])+$minutes_to_add);
OR
$timeslot = date('h:i:s a', strtotime($row['hours_start'])+$minutes_to_add);
Your date format is using h instead of H. The lowercase h is 12 hour format
Use a capital H instead for 24 hours.
date('H:i A', strtotime($timeslot))

Categories