0

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);
?>

2 Answers 2

2

Change your first line,

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

to

$my_time = '9:30';

:)

1
  • 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
    Apr 3, 2010 at 17:37
0

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:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.