tags:

views:

204

answers:

2

Hi guys, hopefully you can help me here. I have some code (please see below) which takes the current time, then adds specific seconds to the time and re-displays the time 1 minute in the future.

Instead of the time being the current time, I want it to be a time which I set - say 9:30. Then I want to be able to add, for example 65 seconds and it shows me 9:31.

Please can you show me how to change it from current time, to a specific time I can set myself.

Thank you.

<?php

$my_time = date('h:i:s',time());
$seconds2add = 65;

$new_time= strtotime($my_time);
$new_time+=$seconds2add;

echo date('h:i:s',$new_time);
?>
+1  A: 

Change your first line,

 $my_time = date('h:i:s',time());

to

$my_time = '9:30';

:)

bobsoap
Ahh, thank for the correction, working perfectly now! I was doing this before: $my_time = date('h:i:s','9:30');.I am going to use this to work out what time a bus service begins, and by adding on the time it takes between each stop, I will be able to calculate what time the bus arrives at any stop.
Henry
A: 

As of PHP 5.2 you can use the DateTime class ( http://us.php.net/manual/en/class.datetime.php ).

$date = new DateTime(date('h:i:s'));
$date->modify('+65 seconds');
echo $date->format('h:i:s');

Also see:

Evan Byrne
That looks neat. I will make sure I'm running PHP5 and give that a try.
Henry
Yeah and if you have PHP 5.3 you can also use the diff() method, which totally rocks my socks: http://us.php.net/manual/en/datetime.diff.php
Evan Byrne