tags:

views:

88

answers:

8

Hey all, just wondering how I can add 30 seconds on to this?

$time = date("m/d/Y h:i:s a", time());

Thankyou, again.


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.

+9  A: 
$time = date("m/d/Y h:i:s a", time() + 30);
Artefacto
A: 

$time = date("m/d/Y h:i:s a", time()+30);

Mark Baker
A: 
$time = date("m/d/Y h:i:s a", time() + 30);
Palantir
A: 

See mktime:

mktime (date("H"), date("i"), date("s") + 30)

http://www.php.net/manual/en/function.mktime.php

should do what you want.

Bob Fincheimer
why a down vote with no comment?
reuscam
Because it ugly. If `date("s")` for example returns 50, than it will be 50 + 30 = 80, what is not a valid number of seconds in a 'time'. Better just use time() + 30 (it is more logical).
VDVLeon
"Because it ugly" is a reason for the down vote, not a reason for down voting with no comment. Thank you for the explanation.
reuscam
and make time will handle the 80 and convert it up....that's it's job.You could put 8,000,000 as the seconds and it would convert it to a proper date, mktime does that.
Bob Fincheimer
+2  A: 

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;
}
danp
A: 
$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.

Digital Human
+1  A: 

What about using strtotime? The code would then be:

strtotime( '+30 second' );

Martijn Dwars
A: 
$time = date("m/d/Y h:i:s a", time() + 30);

//or

$time = date("m/d/Y h:i:s a", strtotime("+30 seconds"));
Alex