Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Date Time Current Time Add Minutes

Tags:

php

Simple question but this is killing my time.

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

like image 919
Ervin Ter Avatar asked Jun 12 '09 09:06

Ervin Ter


People also ask

How to add minutes in DateTime in php?

Show activity on this post. $minutes_to_add = 5; $time = new DateTime('2011-11-17 05:05'); $time->add(new DateInterval('PT' . $minutes_to_add . 'M')); $stamp = $time->format('Y-m-d H:i');

How to add time to 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.

What is Strtotime 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.

What does Date () do in php?

The date() function formats a local date and time, and returns the formatted date string.


2 Answers

I think one of the best solutions and easiest is:

date("Y-m-d", strtotime("+30 minutes")) 

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

like image 93
Khriz Avatar answered Sep 21 '22 11:09

Khriz


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 
like image 27
mns Avatar answered Sep 21 '22 11:09

mns