Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 5 minutes to current datetime on php < 5.3

Tags:

php

datetime

I want to add 5 minutes to this date: 2011-04-8 08:29:49

$date = '2011-04-8 08:29:49';

When I use strtotime I am always getting 1970-01-01 08:33:31

How do I add correctly 5 minutes to 2011-04-8 08:29:49?

like image 358
beginner Avatar asked Apr 10 '11 00:04

beginner


People also ask

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.

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 do you add 30 minutes to a timestamp?

To add 30 minutes to the Date Object first, we get the current time by using the Date. getTime( ) method and then add 30 minute's milliseconds value (30 * 60 * 1000) to it and pass the added value to the Date Object.

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


2 Answers

$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);

Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate

Enjoy! :)

like image 196
Frantisek Avatar answered Oct 13 '22 02:10

Frantisek


Try this:

echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
like image 37
Blender Avatar answered Oct 13 '22 02:10

Blender