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?
The microtime() function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds. The $get_as_float is sent as a parameter to the microtime() function and it returns the string microsec sec by default.
By default, microtime() returns a string in the form "msec sec", where sec is the number of seconds since the Unix epoch (0:00:00 January 1,1970 GMT), and msec measures microseconds that have elapsed since sec and is also expressed in seconds as a decimal fraction.
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.
: a very short interval of time (as 0.01 millionth of a second) microtime photography.
Try it with DateTime instead.
This needs a bit of a workaround because DateInterval
(which is returned by DateTime::diff()
) doesn't calculate the microseconds, so you need to this by hand
$pageTime = new DateTime("2012-04-23T16:08:14.1 - 5 hours");
$rowTime = new DateTime("2012-04-23T16:08:16.9 - 5 hours");
// the difference through one million to get micro seconds
$uDiff = abs($pageTime->format('u')-$rowTime->format('u')) / (1000 * 1000);
$diff = $pageTime->diff($rowTime);
echo $diff->format('%s')-$uDiff;
I always recommend DateTime
because of its flexibility, you should look into it
EDIT
For backwards compability to PHP 5.2 it takes the same approach as for the milliseconds:
$pageTime = new DateTime("2012-04-23T16:08:14.1 - 5 hours");
$rowTime = new DateTime("2012-04-23T16:08:16.9 - 5 hours");
// the difference through one million to get micro seconds
$uDiff = abs($pageTime->format('u')-$rowTime->format('u')) / (1000 * 1000);
$pageTimeSeconds = $pageTime->format('s');
$rowTimeSeconds = $rowTime->format('s');
if ($pageTimeSeconds + $rowTimeSeconds > 60) {
$sDiff = ($rowTimeSeconds + $pageTimeSeconds)-60;
} else {
$sDiff = $pageTimeSeconds - $rowTimeSeconds;
}
if ($sDiff < 0) {
echo abs($sDiff) + $uDiff;
} else {
// for the edge(?) case if $dt2 was smaller than $dt
echo abs($sDiff - $uDiff);
}
Building on Dan Lee's answer, here's a universally working solution:
$pageTime = new DateTime("2012-04-23T16:08:14.9-05:00");
$rowTime = new DateTime("2012-04-23T16:08:16.1-05:00");
$uDiff = ($rowTime->format('u') - $pageTime->format('u')) / (1000 * 1000);
$timePassed = $rowTime->getTimestamp() - $pageTime->getTimestamp() + $uDiff;
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 secondsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With