Reputation: 7078
How can I add 30 seconds to this time?
$time = date("m/d/Y h:i:s a", time());
I wasn't sure how to do it because it is showing lots of different units of time, when I only want to add 30 seconds.
Upvotes: 39
Views: 94935
Reputation: 5621
What about using strtotime? The code would then be:
strtotime( '+30 second' );
Upvotes: 13
Reputation: 504
General :
$add_time=strtotime($old_date)+30;
$add_date= date('m/d/Y h:i:s a',$add_time);
Upvotes: 3
Reputation: 12443
$time = date("m/d/Y h:i:s a", time() + 30);
//or
$time = date("m/d/Y h:i:s a", strtotime("+30 seconds"));
Upvotes: 5
Reputation: 1637
$time = date("m/d/Y h:i:s", time());
$ts = strtotime($time);
$addtime = date("m/d/Y h:i:s", mktime(date("h", $ts),date("i", $ts),date("s", $ts)+30,date("Y", $ts),date("m", $ts),date("d", $ts));
Would be a more explained version of all of the above.
Upvotes: 0
Reputation: 15251
If you're using php 5.3+, check out the DateTime::add operations or modify
, really much easier than this.
For example:
$startTime = new DateTime("09:00:00");
$endTime = new DateTime("19:00:00");
while($startTime < $endTime) {
$startTime->modify('+30 minutes'); // can be seconds, hours.. etc
echo $startTime->format('H:i:s')."<br>";
break;
}
Upvotes: 17
Reputation: 18076
See mktime:
mktime (date("H"), date("i"), date("s") + 30)
http://www.php.net/manual/en/function.mktime.php
should do what you want.
Upvotes: 0