How to use PHP Time function to set a time variable of '09:30', add a specific amount of seconds and then echo the new time
Hopefully you c开发者_高级运维an 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);
?>
Change your first line,
$my_time = date('h:i:s',time());
to
$my_time = '9:30';
:)
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:
- http://us.php.net/manual/en/datetime.modify.php
- http://us.php.net/manual/en/datetime.format.php
精彩评论