tags:

views:

4598

answers:

4

Simple question but this is killing my time.

Any simple solution to add 30 minutes to current time in php with GMT+8?

+1  A: 

It looks like you are after the DateTime function add - use it like this:

$date = new DateTime();
date_add($date, new DateInterval("T30M"));

(Note: untested, but according to the docs, it should work)

a_m0d
http://us.php.net/manual/en/datetime.add.php
tschaible
That's a PHP >= 5.3.0 solution as the function ad the class DateInterval is not available in earlier versions.
Stefan Gehrig
Yes, but the title does specify Date Time, so I assume he is using the DateTime class
a_m0d
DateTime is part of PHP >= 5.2.0 but DateInterval as well as some DateTime methods are not available prior to 5.3.0.
Stefan Gehrig
+1  A: 
$timeIn30Minutes = mktime(idate("H"), idate("i") + 30);

or

$timeIn30Minutes = time() + 30*60; // 30 minutes * 60 seconds/minute

The result will be a UNIX timestamp of the current time plus 30 minutes.

Stefan Gehrig
+4  A: 

I think one of the best solutions and easiest is:

strtotime("+30 minutes")

Maybe it's not the most efficient but is one of the more understandable.

Khriz
why did this get downvoted?
orlandu63
Why the downvote, this is a valid method for adding a time interval?
Neil Aitken
+1  A: 

This is an old question that seems answered, but as someone pointed out above, if you use the DateTime class and PHP < 5.3.0, you can't use the add method, but you can use modify:

$date = new DateTime();
$date->modify("+30 minutes"); //or whatever value you want
mns