php get microtime from date string

岁酱吖の 提交于 2019-11-26 22:03:02

问题


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?


回答1:


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);
}



回答2:


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:

  • We store the signed microseconds difference between both dates in $uDiff and convert the result in seconds by dividing by 1000 * 1000
  • The order of the operands in $uDiff is important and has to be the same as in the $timePassed operation.
  • We compute the Unix timestamp (in full seconds) difference between both dates and we add the microseconds difference to get the wanted result
  • Using DateTime::getTimestamp() will give a correct answer even when the difference is greater than 60 seconds


来源:https://stackoverflow.com/questions/10289160/php-get-microtime-from-date-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!