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/>';
}
Related
the format is 02.02.2018 12:00 EST and 02.02.2018 15:00 EST so then I can get 3 hours. I've tried several methods but no luck
$sTimeStr = strtotime('02.02.2018 12:00 EST');
$eTimeStr = strtotime('02.02.2018 15:00 EST');
$cur_time = strtotime( date( "m.d.Y h:i T", time() ));
if( $cur_time > $sTimeStr && $cur_time < $eTimeStr ){
//code to display
}
strtotime returns time in unix timestamp format so you can just subtract the two numbers to get the difference.
$t1 = "02.02.2018 12:00 EST";
$t2 = "02.02.2018 15:00 EST";
$time1 = strtotime($t1);
$time2 = strtotime($t2);
$diff = ($time2 - $time1);
$diff_in_hours = ($diff/60)/60;// $diff_in_hours = 3
$sysdate = date( 'm.d.Y', time() );
$systime = get_date_from_gmt( date( 'H:i', time() ), 'H:i' );
I ended up splitting date and time for better control
have 2 times $dayFrom = 10:00:00; and $dayTo = 12:00:00 i want to divide it in 15 minutes time duration.
Expected Result
10:00:00, 10:15:00, 10:30:00, 10:45:00 and so on
Looking for help
$timeArray = array();
$startTime = new \DateTime("2010-01-01 10:00:00");
$endTime = new \DateTime("2010-01-01 12:00:00");
while($startTime < $endTime) {
$timeArray[] = $startTime->format('H:i:s');
$startTime->add(new \DateInterval('PT 15 M'));
}
echo implode(",",$timeArray);
Here You can go with this code. It will be helpful to you :)
You can also try this -
$dayFrom = strtotime('10:00:00');
$dayTo = strtotime('12:00:00');
while($dayFrom <= $dayTo) {
echo date('H:i:s', $dayFrom);
$dayFrom= strtotime('+ 15 MINUTES', $dayFrom);
}
Output
10:00:00
10:15:00
10:30:00
10:45:00
11:00:00
11:15:00
11:30:00
11:45:00
12:00:00
strtotime()
Another variation on a theme - using DateInterval.
$df='H:i';
$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('PT15M');
$ts=date( 'Y-m-d H:i:s', strtotime('10.00am') );
$tf=date( 'Y-m-d H:i:s', strtotime('12.00pm') );
$start=new DateTime( $ts, $timezone );
$end=new DateTime( $tf, $timezone );
while( $start->add( $interval ) <= $end ){
echo $start->format( $df ).'<br />';
}
$dayFrom = "10:00:00";
$dayTo = "12:00:00";
while($endTime<$dayTo){
$endTime = strtotime($dayFrom) + 900;
echo date('h:i:s', $endTime);
}
Try this.
I need to divide time period for example from:
2015-11-22 11:22:33 to 2015-11-24 02:02:04
into something like this:
2015-11-22 11:22:33 - 2015-11-22 23:59:59
2015-11-23 00:00:00 - 2015-11-23 23:59:59
2015-11-24 00:00:00 - 2015-11-24 02:02:04.
It has to work also for periods shorter than 24h, so for
2015-11-22 11:22:33 to 2015-11-23 02:02:04
I need this:
2015-11-22 11:22:33 - 2015-11-22 23:59:59
2015-11-23 00:00:00 - 2015-11-23 02:02:04.
I found almost perfect piece of code, but it only works for periods longer than 24h and I don't know how to tune it.
<?php
$start_date = '27:04:2013';
$start_time = '16:30';
$end_date = '29:04:2013';
$end_time = '22:30';
// Date input strings and generate a suitable DatePeriod
$start = DateTime::createFromFormat("d:m:Y H:i", "$start_date $start_time");
$end = DateTime::createFromFormat("d:m:Y H:i", "$end_date $end_time");
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
// Get midnight at start of current day
$date_start = clone $date;
$date_start->modify('midnight');
// Get 23:59:59, end of current day
// (moving to midnight of next day might be good too)
$date_end = clone $date;
$date_end->modify('23:59:59');
// Take care of partial days
$date_start = max($start, $date_start);
$date_end = min($end, $date_end);
// Here you would construct your array of
// DateTime pairs, or DateIntervals, as you want.
printf(
"%s -> %s \n",
$date_start->format('Y-m-d H:i'),
$date_end->format('Y-m-d H:i')
);
}
?>
Try this:
$date1 = '2015-11-22 11:22:33';
$date2 = '2015-11-23 12:22:34';
$f1 = strtotime($date1);
$f2 = strtotime(substr($date1, 0, 10) . " 23:59:59");
while($f2 < strtotime($date2)) {
print(date('Y-m-d H:i:s',$f1) .' - ' .date('Y-m-d H:i:s',$f2).'<br>');
$f1 = strtotime(date('Y-m-d H:i:s', $f2) .' +1 second');
$f2 = strtotime(date('Y-m-d H:i:s', $f2) .' +1 day');
}
print(date('Y-m-d H:i:s',$f1) .' - ' .$date2.'<br>');
See here: http://sandbox.onlinephpfunctions.com/code/77eaae15fdd7c0d2ca1f02a2d225c17199218819
$datetime1 = new DateTime('2015-11-22 11:22:33');
$datetime2 = new DateTime('2015-11-23 02:02:04');
$interval = $datetime1->diff($datetime2);
var_dump($interval->format('%y-%m-%d %h:%i:%s'));
// RESULT: string(14) "0-0-0 14:39:31"
References:
http://php.net/manual/de/datetime.diff.php
http://php.net/manual/de/class.dateinterval.php
http://php.net/manual/de/dateinterval.format.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
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