Split time into intervals

2019-06-18 15:33发布

I have three input

  1. Start time
  2. End time
  3. 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.

6条回答
看我几分像从前
2楼-- · 2019-06-18 15:56

Try this with DateTime and DateInterval:

<?php
$startTime = new DateTime('2012-07-13 01:00:00');
$endTime = new DateTime('2012-07-13 01:30:00');

while($startTime <= $endTime) {
    echo $startTime->format('H:i:s') . ' ';
    $startTime->add(new DateInterval('PT10M'));
}
?>

Eval-Demo

查看更多
Ridiculous、
3楼-- · 2019-06-18 16:02

Try this:

$startTime = new DateTime("2012-07-13 01:00:00");
$endTime = new DateTime("2012-07-13 01:30:00");

while ($startTime < $endTime) {
    echo $startTime->format('H:i:s') . ' - ';
    echo $startTime->modify('+10 minutes')->format('H:i:s') . "\n";
}

demo

查看更多
我命由我不由天
4楼-- · 2019-06-18 16:03

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:

$startTime=strtotime("2012-07-13 01:00:00");
$endTime=strtotime("2012-07-13 01:30:00");
$interval = "10";
$time=$startTime;
while ($time <= $endTime) {
    echo date('H:i', $time) . "<br>";
    $time = strtotime('+'.$interval.' minutes', $time);
}
查看更多
The star\"
5楼-- · 2019-06-18 16:03

I think the code works as expected... Maybe you could refactor it like this to have the exact output you are requiring:

<?php

$startTime=strtotime("2012-07-13 01:00:00");
$endTime=strtotime("2012-07-13 01:30:00");
$intervel="60";

$time=$startTime;
echo date('H:i', $time);
$time = strtotime('+'.$intervel.' minutes', $time);
while ($time <= $endTime) {
    echo "," . date('H:i', $time);
    $time = strtotime('+'.$intervel.' minutes', $time);
}

?>

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?

查看更多
老娘就宠你
6楼-- · 2019-06-18 16:03

You can achieve this with modify, DateInterval and DatePeriod.

$startTime = new DateTime('2012-07-13 01:00:00');
$endTime   = new DateTime('2012-07-13 01:30:00');
$endTime   = $endTime->modify( '+1 minute' );
$interval  = new DateInterval('PT10M');
$daterange = new DatePeriod($startTime, $interval ,$endTime);
foreach($daterange as $res) {
        echo $res->format('H:i') .", ";
}
查看更多
来,给爷笑一个
7楼-- · 2019-06-18 16:15
$start = DateTime::createFromFormat('Y-m-d H:i:s', '2012-07-13 01:00:00', new DateTimeZone('Europe/Berlin'));
$end = DateTime::createFromFormat('Y-m-d H:i:s', '2012-07-13 01:30:00', new DateTimeZone('Europe/Berlin'));
while($start <= $end) {
    echo $start->format('H:i:s'), "\n";
    $start->modify('+10 minutes');
}

You were missing the last possible output as that would yield the same value as your end-time. a < b should have been a <= b

Result: http://3v4l.org/8eK3O

查看更多
登录 后发表回答