Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding minutes to date time in PHP

Tags:

date

php

time

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.

The date time format I have is:

2011-11-17 05:05: year-month-day hour:minute

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?

like image 236
Luke B Avatar asked Nov 17 '11 14:11

Luke B


People also ask

How to add hours and minutes in php?

php $hour_one = "01:20:20"; $hour_two = "05:50:20"; $h = strtotime($hour_one); $h2 = strtotime($hour_two); $minute = date("i", $h2); $second = date("s", $h2); $hour = date("H", $h2); echo "<br>"; $convert = strtotime("+$minute minutes", $h); $convert = strtotime("+$second seconds", $convert); $convert = strtotime("+$ ...

How to add date time in php?

php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("next Sunday"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date) .

How do you add 30 minutes to a timestamp?

To add minutes to a datetime you can use DATE_ADD() function from MySQL. In PHP, you can use strtotime(). select date_add(yourColumnName,interval 30 minute) from yourTableName; To use the above syntax, let us create a table.

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.


1 Answers

$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'); 

The ISO 8601 standard for duration is a string in the form of P{y}Y{m1}M{d}DT{h}H{m2}M{s}S where the {*} parts are replaced by a number value indicating how long the duration is.

For example, P1Y2DT5S means 1 year, 2 days, and 5 seconds.

In the example above, we are providing PT5M (or 5 minutes) to the DateInterval constructor.

like image 171
Tim Cooper Avatar answered Sep 24 '22 03:09

Tim Cooper