I have three input
- Start time
- End time
- intervel time.
Example
start time - 01:00
end time - 01:30
intervel-time - 10 min
I need the output like 01:00, 01:10, 01:20, 01:30
i have tried this below code, its not working .
<?php
$startTime=strtotime("2012-07-13 01:00:00");
$endTime=strtotime("2012-07-13 01:30:00");
$time=$startTime;
while ($time < $endTime) {
echo date('H:i:s', $time) . " - ";
$time = strtotime('+10 minutes', $time);
echo date('H:i:s', $time) . "<br>";
}
?>
When i try with start and end time interval is 60min, the above code is work.
I am new in PHP.
Any one please help me.
Try this with DateTime and DateInterval:
Eval-Demo
Try this:
demo
Your code works just fine, i've just added a variable which idicates the interval and reformatted the time in order to be the same as
I need the output like 01:00, 01:10, 01:20, 01:30
and the last thing is that you should first print the time the increment it so.Here is the code:
I think the code works as expected... Maybe you could refactor it like this to have the exact output you are requiring:
If you change from 10 to 60 the interval, then it will only print the start time because the start_time+60 is higher than the end time; is this what you'd expect?
You can achieve this with modify, DateInterval and DatePeriod.
You were missing the last possible output as that would yield the same value as your end-time.
a < b
should have beena <= b
Result: http://3v4l.org/8eK3O