How to add time with another time in php?
I'm really stuck with adding X minutes to a datetime, after doing lots of google'ing and PHP manual reading, I don't seem to be getting anywhere. Show The date time format I have is:
Minutes to add will just be a number between 0 and 59 I would like the output to be the same as the input format with the minutes added. Could someone give me a working code example, as my attempts don't seem to be getting me anywhere?
Tim Cooper 154k37 gold badges321 silver badges273 bronze badges asked Nov 17, 2011 at 14:51
The ISO 8601 standard for duration is a string in the form of For example, In the example above, we are providing
Daniel 7,7875 gold badges57 silver badges80 bronze badges answered Nov 17, 2011 at 14:54
Tim CooperTim Cooper 154k37 gold badges321 silver badges273 bronze badges 10 PHP's DateTime class has a useful modify method which takes in easy-to-understand text.
You could also use string interpolation or concatenation to parameterize it:
answered Sep 30, 2014 at 23:50
DanielDaniel 7,7875 gold badges57 silver badges80 bronze badges 1
result is
Live demo is If you are no familiar with
answered Nov 17, 2011 at 14:55
NemodenNemoden 8,5266 gold badges39 silver badges65 bronze badges 3 You can do this with native functions easily:
I'd recommend the DateTime class method though, just posted by Tim. answered Nov 17, 2011 at 14:54
BradBrad 155k48 gold badges341 silver badges511 bronze badges I don't know why the approach set as solution didn't work for me. So I'm posting here what worked for me in hope it can help anybody:
answered Jun 19, 2019 at 10:42
user3361395user3361395 1412 silver badges4 bronze badges I thought this would help some when dealing with time zones too. My modified solution is based off of @Tim Cooper's solution, the correct answer above.
The bold line, line 3, is the addition. I hope this helps some folks as well. answered Sep 21, 2016 at 18:54
acaritoacarito 6247 silver badges15 bronze badges A bit of a late answer, but the method I would use is:
Or in PHP >= 5.4
answered Oct 26, 2015 at 8:42
PeterPeter 8,3896 gold badges55 silver badges92 bronze badges If you want to give a variable that contains the minutes. Then I think this is a great way to achieve this.
answered Jan 16, 2015 at 16:27
Use Example:
answered Jun 16, 2017 at 3:10
DeathRsDeathRs 1,06017 silver badges22 bronze badges As noted by Brad and Nemoden in their answers above, strtotime() is a great function. Personally, I found the standard DateTime Object to be overly complicated for many use cases. I just wanted to add 5 minutes to the current time, for example. I wrote a function that returns a date as a string with some optional parameters: Obviously, this is not a fully featured method. Just a quick and simple function for modifying/formatting the current date.
answered Apr 30, 2016 at 19:01
One more example of a function to do this: (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval): (I've also written an alternate form of the below function.)
answered Apr 4, 2017 at 20:26
one line mysql datetime format
answered Dec 18, 2018 at 14:40
Sjaak WishSjaak Wish 3652 silver badges8 bronze badges Without using a variable:
With using a variable:
answered Jan 13, 2018 at 12:04
Wajid khanWajid khan 7948 silver badges18 bronze badges 2 How can I add minutes and minutes in 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 can add hours minutes and seconds in PHP?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 can I insert current time in PHP?Note that the PHP date() function will return the current date/time of the server!
How do you add 1 minute to a datetime?Use the timedelta() class from the datetime module to add minutes to datetime, e.g. result = dt + timedelta(minutes=10) . The timedelta class can be passed a minutes argument and adds the specified number of minutes to the datetime.
|