Adding 30 minutes to time formatted as H:i in PHP

bateman_ap picture bateman_ap · May 4, 2010 · Viewed 149.9k times · Source

Having a nightmare at the moment and just can't see why it isn't working

I have a value in the form H:i (ie 10:00, 13:30) etc called $time

What I want to do is create two new values, $startTime which is 30 mins before $time and $endTime which is 30 mins after $time

I have tried the following but just doesn't seem to want to work

$startTime = date("H:i",strtotime('-30 minutes',$time));
$endTime = date("H:i",strtotime('+30 minutes',$time));

If I pass through 10:00 as $time and echo out both $startTime and $endTime I get:

$startTime = 00:30
$startTime = 01:30        

Answer

webbiedave picture webbiedave · May 4, 2010
$time = strtotime('10:00');
$startTime = date("H:i", strtotime('-30 minutes', $time));
$endTime = date("H:i", strtotime('+30 minutes', $time));