1

I have 2 dates that I would like to get the difference of using the date_diff function.

This is the code I am running:

$starttime=DateTime::createFromFormat("Y-m-d\TH:i:sP",'2014-10-08T22:03:51.579+02:00');

$stoptime=DateTime::createFromFormat("Y-m-d\TH:i:sP",'2014-10-08T22:04:59.811+02:00');

$interval = date_diff($starttime, $stoptime);

$interval->format("%i %s");

This should return a string from the date calculation with '01:08'

If it was calculating correctly but something seems to be wrong with the creation of the dates I think, the var_dump of the $starttime and $stoptime return FALSE which according to the doc means that the datetime object can't be created. Am I specifying the wrong format for my date/time string?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Sevren
  • 301
  • 4
  • 10

1 Answers1

2

You made it more complex than it needs to be. DateTime() can handle that date format so skip using createFromFormat():

$starttime= new DateTime('2014-10-08T22:03:51.579+02:00');
$stoptime= new DateTime('2014-10-08T22:04:59.811+02:00');
$interval = date_diff($starttime, $stoptime);
echo $interval->format("%i:%S");

Demo

Also, use %S to get leading zeros for your seconds.

John Conde
  • 217,595
  • 99
  • 455
  • 496