tags:

views:

48

answers:

1

I have a 24 hour time string (ie 16:30) and would like to add x time blocks of 30 mins. For example if x = 4, then 16:30 + 4(30) = 18:30. Is there any easy way to do this with out exploding the string and doing if statements for mins/hours.. etc? Also this is on a php page would it be easier to do this in php then echo it to the javascript?

Thanks for the help!

+3  A: 

You could add to a timestamp and then format it to HH:MM

var x = 1;
var d = new Date(Date.parse("1/1/2010 16:30") + x * 30 * 60 * 1000);
var newTime = d.getHours() + ":" + d.getMinutes();

You'll want to pad zero's to the hours and minutes.

You can do it in PHP pretty much the same way:

$x = 1;
$newTime = date('H:i', strtotime('1/1/2010 16:30') + $x * 30 * 60);

The new time will already be formatted with leading zero's.

webbiedave