4

I want to add 3 minutes to a date/time variable I have, but I'm not sure how to do this. I made the variable from a string like this: (which is in the RFC 2822 date format btw)

$date = 2011-10-18T19:56:00+0200

I converted that string into date using this command:

$time = date_format(DateTime::createFromFormat("Y-m-d\TH:i:sO", $date), "G:i")

Now, I'd like to add 3 minutes to that variable, but I I'm not sure how. I've used the following command in my script before, but that applies to the current date/time, so I'm not sure how to use that for my time variable:

$currenttime = date('G:i', strtotime('+2 hours'));

So, how can I add three minutes to the $time variable?

laarsk
  • 862
  • 6
  • 17
  • 36

5 Answers5

9
echo $idate="2013-09-25 09:29:44";

$effectiveDate = strtotime("+40 minutes", strtotime($idate));

echo date("Y-m-d h:i:s",$effectiveDate);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
saikat
  • 111
  • 1
  • 6
5

Since you're using the DateTime object already, stick with it:

$time = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date);
$three_minutes = $time->add(new DateInterval('P2H'));
                                               ^^--two (2) hours (H)
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • An even better solution, as I do not want the current time to be modified, thanks a lot! – laarsk Oct 18 '11 at 18:27
  • Unfortunatly, I don't get it to work... :S Ive got it like this now,: `echo 'Arrival: '.date_format($time, 'G:i').'
    '."\n";` `echo 'Departure: ';` `echo ( $time->add(new DateInterval('P2H')) );` `echo '

    '."\n";` but the page stops at the Departure echo...
    – laarsk Oct 18 '11 at 18:32
  • 3
    the string format is incorrect. You need use $time->add(new DateInterval('PT2H')); – Marcos Bergamo May 15 '14 at 18:48
4

Use the second parameter of strtotime to provide a reference time:

$date_rfc2822 = '2011-10-18T19:56:00+0200';
$dateTime = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date_rfc2822);
echo date('G:i', strtotime('+2 hours', $dateTime->getTimestamp()));
phihag
  • 278,196
  • 72
  • 453
  • 469
  • This doesnt do what I want, I just figured out, as it takes the current time and adds 3 minutes, it doesnt get its time from the $datetime variable... am i correct? – laarsk Oct 18 '11 at 18:36
  • @laarks No, the current time isn't used anywhere. Note that this exact demo program (when prefixed with ` – phihag Oct 18 '11 at 20:01
  • Hmmm, I guess you're right then. But I already got another fix: i used strtotime twice :) which gets the date just fine aswell [from: http://stackoverflow.com/questions/7812112/php-add-3-minutes-to-date-variable] – laarsk Oct 18 '11 at 20:05
1

The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.

http://www.php.net/manual/en/dateinterval.construct.php

That being said my solution to a similar problem is this:

// pretend that $date is what you got from mysql
// which is like 2013-02-12 23:08:17
echo "<br>";
echo $date;
$time = DateTime::createFromFormat("Y-m-d H:i:s", $date);
echo "<br>";
echo $time->format('H-i-s');
$time->add(new DateInterval('PT2H'));
echo "<br>";
echo $time->format('H-i-s');
// Outputs:
// 2013-02-12 23:08:17
// 23-08-17
// 01-08-17

The thing is to add P when using DateInterval class, and T before time entries. For your case you need to go with PT3M for 3 minute addition. I was trying to add 2 hours and what I did was $time->add(new DateInterval('PT2H'));.

If you would look at the interval specs:

Y   years
M   months
D   days
W   weeks. These get converted into days, so can not be combined with D.
H   hours
M   minutes
S   seconds

M for months and M for minutes. That is why there is a T in front of time.

At least that's what I want to believe... 'O_O

Logan
  • 10,649
  • 13
  • 41
  • 54
1
//set the date and time

$start = '2017-01-01 14:00:00';

//display the converted time you want to add for ex. 1 hour and 20 minutes 

echo date('Y-m-d H:i:s',strtotime('+1 hour +20 minutes',strtotime($start)));
Ahmed Bermawy
  • 2,290
  • 4
  • 35
  • 42