61

Simple question but this is killing my time.

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

13 Answers 13

143

I think one of the best solutions and easiest is:

date("Y-m-d H:i:s", strtotime("+30 minutes"))

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

1
  • If you want to use strtotime relatively to an existing datetime you can do code strtotime('2011-11-01 + 2 days'); Commented Nov 3, 2011 at 12:13
63

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
1
  • 3
    You could also use it without modify() function like $date = new DateTime('+30 minutes'); Commented Jan 22, 2020 at 9:07
13

Time 30 minutes later

$newTime = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +30 minutes"))
11
$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.

5
echo $date = date('H:i:s', strtotime('13:00:00 + 30 minutes') );

13:00:00 - any inputted time

30 minutes - any interval you wish (20 hours, 10 minutes, 1 seconds etc...)

3

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)

4
  • 1
    That's a PHP >= 5.3.0 solution as the function ad the class DateInterval is not available in earlier versions. Commented Jun 12, 2009 at 10:03
  • Yes, but the title does specify Date Time, so I assume he is using the DateTime class Commented Jun 12, 2009 at 10:14
  • 1
    DateTime is part of PHP >= 5.2.0 but DateInterval as well as some DateTime methods are not available prior to 5.3.0. Commented Jun 12, 2009 at 10:45
  • @a_m0d @tokam Doc says it must start with a "P" for "Period", so, this is the correct way to add 30 minutes: new DateInterval('PT30M') (Tested! :)) Commented Mar 9, 2012 at 21:20
3
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata')); 
echo $dateTime->modify("+10 minutes")->format("H:i:s A");
1
  • 2
    While this code may solve the question, including an explanation of how and why this solves the problem (and ideally how it improves upon other answers) would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. Commented Mar 27, 2020 at 19:55
2
$ck=2016-09-13 14:12:33;
$endtime = date('H-i-s', strtotime("+05 minutes", strtotime($ck)));  
1

In addition to Khriz's answer.

If you need to add 5 minutes to the current time in Mysql format you can do:

$cur_time=date("Y-m-d H:i:s");
$duration='+5 minutes';
echo date('Y-m-d H:i:s', strtotime($duration, strtotime($cur_time)));
0

time after 30 min, this easiest solution in php

date('Y-m-d H:i:s', strtotime("+30 minutes"));

for DateTime class (PHP 5 >= 5.2.0, PHP 7)

$dateobj = new DateTime();
$dateobj ->modify("+30 minutes"); 
0

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') ?>
0
new DateTime('+30minutes')

As simple as the accepted solution but gives you a DateTime object instead of a Unix timestamp.

-1
$time = strtotime(date('2016-02-03 12:00:00'));
        echo date("H:i:s",strtotime("-30 minutes", $time));
1
  • The question was "Any simple solution to add 30 minutes to current time". Commented Feb 4, 2016 at 0:50

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.