php - can't add time in a while loop -


i'm trying increment time in while loop failing. need start time increment 20 minutes every loop long less end time. results echo in button choice user.

$sam start time stored in mysql database 07:00:00
$eam end time stored in mysql database 10:00:00

while(strtotime($sam) < strtotime($eam)){     $sam = new datetime($sam);     $sam = $sam->format('g:i');     $samend = new datetime($sam);     $samend->modify('+20 minutes');     $samendf = $samend->format('g:i');     $sam = new datetime(strtotime($samend));     echo '<button style="margin:5px 0px" class="btn btn-primary btn-block">'.$sam.' - '.$samendf.'</button>'; } 

you're mixing bit on objects , strings, in addition strtotime() cannot take object. seem doing bit more complicated need. if understand correctly, you're looking for.

you can compare datetimes directly, $sam < $eam.

$sam = new datetime('07:00:00'); $eam = new datetime('10:00:00');  while ($sam < $eam) {     $start = $sam->format('g:i');      // start value equal ending of last 1 in previous loop     $sam->modify('+20 minutes');       // increase 20 minutes each loop     $samend = $sam->format('g:i');     // increased 20 minutes in relation $start     echo '<button style="margin:5px 0px" class="btn btn-primary btn-block">'.$start.' - '.$samend.'</button> '; } 

this create output buttons

[7:00 - 7:20] [7:20 - 7:40] [7:40 - 8:00]
[8:00 - 8:20] [8:20 - 8:40] [8:40 - 9:00]
[9:00 - 9:20] [9:20 - 9:40] [9:40 - 10:00]


Comments