Simple question but this is killing my time.
Any simple solution to add 30 minutes to current time in php with GMT+8?
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
The question is a little old, but I come back to it often ;p
Another way, which is also a one liner:
<?= date_create('2111-11-11 00:00:00')->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns Y-m-d h:i:s:
<?= date_create('@'.time())->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns timestamp:
<?= date_create('@'.time())->modify("+30 minutes")->format('U') ?>
Time 30 minutes later
$newTime = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +30 minutes"))
$time = strtotime(date('2016-02-03 12:00:00'));
echo date("H:i:s",strtotime("-30 minutes", $time));
It looks like you are after the DateTime function add - use it like this:
$date = new DateTime();
date_add($date, new DateInterval("PT30M"));
(Note: untested, but according to the docs, it should work)
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.