Sam
Sam

Reputation: 7078

Add 30 seconds to the time with PHP

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

Answers (7)

Martijn
Martijn

Reputation: 5621

What about using strtotime? The code would then be:

strtotime( '+30 second' );

Upvotes: 13

Ivoglent Nguyen
Ivoglent Nguyen

Reputation: 504

General :

$add_time=strtotime($old_date)+30;
$add_date= date('m/d/Y h:i:s a',$add_time);

Upvotes: 3

Alex
Alex

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

Digital Human
Digital Human

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

danp
danp

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

Bob Fincheimer
Bob Fincheimer

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

Artefacto
Artefacto

Reputation: 97845

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

Upvotes: 80

Related Questions