Php add 30 minutes to datetime

So what I need to do is add 30 minutes to the following

date("Ymdhis");

I tried this

+strtotime("+30 minutes");

however it does not seem to like it. I wondering what the correct why to do this is.

asked Mar 22, 2012 at 22:47

RussellHarrowerRussellHarrower

6,39121 gold badges94 silver badges188 bronze badges

1

Your method of using strtotime should work.


Output

2012/03/22 10:55:45
2012/03/22 11:25:45 // 30 minutes later

However your method of adding time probably isn't correct. The above will work to add 30 minutes to the current time. Suppose you want to add 30 minutes from a given time, $t, then use strtotime's second parameter, which is used as a base for the calculation of relative dates.

date("Y/m/d H:i:s", strtotime("+30 minutes", $t));

http://codepad.org/Z5yquF55

answered Mar 22, 2012 at 22:56

I tested this code but it doesn't work for me:

 $t = date();  
 date("Y/m/d h:i:s", strtotime("+30 minutes", $t));

Here's my solution

 //This is where you put the date, but I use the current date for this example
 $date = date("Y-m-d H:i:s");

 //Convert the variable date using strtotime and 30 minutes then format it again on the desired date format
 $add_min = date("Y-m-d H:i:s", strtotime($date . "+30 minutes"));
 echo  $date . "
"; //current date or whatever date you want to put in here echo $add_min; //add 30 minutes

answered Mar 23, 2012 at 5:38

SuperNoobSuperNoob

3723 silver badges6 bronze badges

strtotime() accepts a second parameter which is its starting point.

If you have date("Ymdhis", $somedate) and wanted to add 30 minutes to it, you can do date("Ymdhis", strtotime("+30 minutes", $someddate))

answered Mar 22, 2012 at 22:57

staticsanstaticsan

29.4k4 gold badges58 silver badges73 bronze badges

Try something like.

$Start = "12:00:00";
$Minutes = 30;

$To = date("H:i:s", strtotime($Start)+($Minutes*60));

answered Mar 22, 2012 at 22:51

Php add 30 minutes to datetime

AdamAdam

3,5336 gold badges30 silver badges50 bronze badges

Use this function:

date("Ymdhis", strtotime("+30 minutes"))

Php add 30 minutes to datetime

Somnath Muluk

52.5k34 gold badges216 silver badges224 bronze badges

answered Mar 22, 2012 at 22:57

sikandersikander

2,31316 silver badges23 bronze badges

Not sure what your entire code looks like, but:

date("Ymdhis");

is returning a string. So it doesn't make sense to add the result of

strtotime("+30 minutes");

(which is an integer) to that string.

You either want

strtotime("+30 minutes");

by itself, or

date("Ymdhis", strtotime("+30 minutes"));

to get the formatted string.

answered Mar 22, 2012 at 22:56

mattmatt

1,9281 gold badge20 silver badges28 bronze badges

1

Do you mean date("Ymdhis", strtotime("+30 minutes"));? This will represent the date that is 30 minutes in the future.

answered Mar 22, 2012 at 22:57

mvdnesmvdnes

3831 silver badge5 bronze badges


answered Mar 22, 2012 at 23:05

How to add minutes in DateTime using php?

For this, you can use the strtotime() method. $anyVariableName= strtotime('anyDateValue + X minute'); You can put the integer value in place of X.

How to add time in time in php?

PHP | DateTime add() Function The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object.

How to add 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; ?>

What is the use of Strtotime function in php?

The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.