Jaydeep Mor
Jaydeep Mor

Reputation: 1703

Dynamic set increment time by 15 minutes

I have trouble to increment time by 15 minutes to end time.

I tried date("H:i:s", strtotime('+15 minutes', strtotime($startTime)));. But it is not dynamic.

Here i have starttime and endtime.

$startTime = '09:00:00';
$endTime = '11:00:00';

And want to output like,

09:00:00 09:15:00 09:30:00 09:45:00 10:00:00 10:15:00 10:30:00 10:45:00

Thanks.

Upvotes: 4

Views: 640

Answers (4)

Mohammad Hamedani
Mohammad Hamedani

Reputation: 3354

You can use following codes:

<?php

$startTime = '09:00:00';
$endTime = '11:00:00';

$times = array();
$last_inc = $startTime;
while($last_inc < $endTime) {
    $times[] = $last_inc;
    $last_inc = date("H:i:s", strtotime("+15 minutes $last_inc"));
}

print_r($times);

Ouput:

Array
(
    [0] => 09:00:00
    [1] => 09:15:00
    [2] => 09:30:00
    [3] => 09:45:00
    [4] => 10:00:00
    [5] => 10:15:00
    [6] => 10:30:00
    [7] => 10:45:00
)

Upvotes: 1

Rushit Solanki
Rushit Solanki

Reputation: 111

Please try below code

$startTime='09:00:00';
$endTime='11:00:00';
$Times=array();
$interval=15;

while(strtotime($startTime) < strtotime($endTime))
{   
    $Times[]=$startTime;
    $startTime=strtotime("+".$interval." minutes",strtotime($startTime));
    $startTime=date('h:i:s',$startTime);    
}

Output

Array
(
   [0] => 09:00:00
   [1] => 09:15:00
   [2] => 09:30:00
   [3] => 09:45:00
   [4] => 10:00:00
   [5] => 10:15:00
   [6] => 10:30:00
   [7] => 10:45:00
)

Upvotes: 1

B. Desai
B. Desai

Reputation: 16436

You have to loop over time to generate data you want

$startTime = '09:00:00';
$endTime = '11:00:00';
$new_Time = $startTime;
while($new_Time < $endTime){
    $new_Time = date("H:i:s", strtotime('+15 minutes', strtotime($new_Time)));
    echo $new_Time;
    echo "<br>";
}

o/p:

09:15:00
09:30:00
09:45:00
10:00:00
10:15:00
10:30:00
10:45:00
11:00:00

Upvotes: 1

Nidhi
Nidhi

Reputation: 1539

I think using do...while you can do this

use this code:

$startTime = '09:00:00';
$endTime = '11:00:00';
$inc = "";
do {
    $inc = date("H:i:s", strtotime('+15 minutes', strtotime($startTime)));
    $startTime = $inc;
    echo $inc."   ";
}while($inc < $endTime);

output:

09:15:00 09:30:00 09:45:00 10:00:00 10:15:00 10:30:00 10:45:00 11:00:00

Upvotes: 1

Related Questions