Manish Raghani
Manish Raghani

Reputation: 51

how to divide 2 times with specific time duration

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

Upvotes: 1

Views: 137

Answers (4)

Red Bottle
Red Bottle

Reputation: 3080

$dayFrom = "10:00:00";
$dayTo = "12:00:00";
while($endTime<$dayTo){   
$endTime = strtotime($dayFrom) + 900;
echo date('h:i:s', $endTime);
}

Try this.

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33813

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

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31739

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()

Upvotes: 1

Nirav Joshi
Nirav Joshi

Reputation: 2950

$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 :)

Upvotes: 1

Related Questions