Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 30 seconds to the time with PHP

Tags:

php

time

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.

like image 957
Sam Avatar asked Jun 16 '10 11:06

Sam


People also ask

How can add hours minutes and seconds in PHP?

php $time = "01:30:00"; list ($hr, $min, $sec) = explode(':',$time); $time = 0; $time = (((int)$hr) * 60 * 60) + (((int)$min) * 60) + ((int)$sec); echo $time; ?>

How do you add 30 minutes to a timestamp?

To add minutes to a datetime you can use DATE_ADD() function from MySQL. In PHP, you can use strtotime(). select date_add(yourColumnName,interval 30 minute) from yourTableName; To use the above syntax, let us create a table.

How can I timestamp in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How can I get 30 Day date in PHP?

php $next_due_date = date('y-m-d',strtotime('+30 days',strtotime('echo $userRow3["due_date"]'))) .


3 Answers

$time = date("m/d/Y h:i:s a", time() + 30);
like image 89
Artefacto Avatar answered Oct 18 '22 18:10

Artefacto


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;
}
like image 17
dmp Avatar answered Oct 18 '22 19:10

dmp


What about using strtotime? The code would then be:

strtotime( '+30 second' );
like image 12
Martijn Avatar answered Oct 18 '22 19:10

Martijn