I am trying to get the time passed between two datetime strings (including milliseconds)
example:
$pageTime = strtotime("2012-04-23T16:08:14.9-05:00");
$rowTime = strtotime("2012-04-23T16:08:16.1-05:00");
$timePassed = $rowTime - $pageTime;
echo $timePassed . "<br/><br/>";
What I want to see echoed is "1.2" but strtotime()
ignores the millisecond part of the string. Also, apparently microtime()
doesn't let you give it a datestring... Is there an alternative function for calculating this, or am I going to have to do some string parsing to extract the seconds and milliseconds and subtract?
Try it with DateTime instead.
This needs a bit of a workaround because
DateInterval
(which is returned byDateTime::diff()
) doesn't calculate the microseconds, so you need to this by handI always recommend
DateTime
because of its flexibility, you should look into itEDIT
For backwards compability to PHP 5.2 it takes the same approach as for the milliseconds:
Building on Dan Lee's answer, here's a universally working solution:
Complete explanations:
$uDiff
and convert the result in seconds by dividing by 1000 * 1000$uDiff
is important and has to be the same as in the $timePassed operation.DateTime::getTimestamp()
will give a correct answer even when the difference is greater than 60 seconds