Php UTC时间(以7位小数计算)
我需要在PHP7.3中计算2个UTC时间值和7位小数之间的差值 我是否可以简单地执行以下操作:Php UTC时间(以7位小数计算),php,unix,utc,epoch,seconds,Php,Unix,Utc,Epoch,Seconds,我需要在PHP7.3中计算2个UTC时间值和7位小数之间的差值 我是否可以简单地执行以下操作: val1 = 20200205120415.6513380; //first timestamp val2 = 20200205120415.6535670; //second timestamp $diff = $val2 - $val1; //should be difference between the 2 timestamps $start = 20200205120415.6513380
val1 = 20200205120415.6513380; //first timestamp
val2 = 20200205120415.6535670; //second timestamp
$diff = $val2 - $val1; //should be difference between the 2 timestamps
$start = 20200205120415.6513380;
$end = 20200205120415.6535670;
//get value left of . and then create datetime object to later convert to seconds
list($datetime, $usecStart) = explode(".", $start);
$startTime = date_create_from_format("YmdHis", $datetime);
list($datetime, $usecEnd) = explode(".", $end);
$endTime = date_create_from_format("YmdHis", $datetime);
//get timestamp in seconds and add franction or microseconds back
$start = $startTime->getTimestamp().".".$usecStart;
$end = $endTime->getTimestamp().".".$usecEnd;
//get difference in seconds and fraction or microseconds
echo $end - $start;
上述计算值为0.00229。如果我做得正确,那么这个值是以秒或微秒为单位的,我能把它转换成UNIX历元时间戳吗?我强烈怀疑上面的时间不是简单的数字;他们的BCD二进制编码十进制为2020-02-05-12:04:15.6513380。您无法对这些进行简单的计算,您需要解析它们以转换为unix时间戳
根据您的语言,将它们转换为字符串,并将前四个字符作为年份,后两个字符作为月份,等等,这可能是解析这些字符的最简单方法。这是我目前的完整性解决方案 属性右侧的值。实际上是分数秒。因此,在PHP中,为了获得差异,我执行了以下操作:
val1 = 20200205120415.6513380; //first timestamp
val2 = 20200205120415.6535670; //second timestamp
$diff = $val2 - $val1; //should be difference between the 2 timestamps
$start = 20200205120415.6513380;
$end = 20200205120415.6535670;
//get value left of . and then create datetime object to later convert to seconds
list($datetime, $usecStart) = explode(".", $start);
$startTime = date_create_from_format("YmdHis", $datetime);
list($datetime, $usecEnd) = explode(".", $end);
$endTime = date_create_from_format("YmdHis", $datetime);
//get timestamp in seconds and add franction or microseconds back
$start = $startTime->getTimestamp().".".$usecStart;
$end = $endTime->getTimestamp().".".$usecEnd;
//get difference in seconds and fraction or microseconds
echo $end - $start;
下面是使用datetime->diff函数的另一种方法:
$start = new DateTime('2020-02-05T12:04:15.6513380Z');
$end = new DateTime('2020-02-05T12:04:15.6535670Z');
$diff = $start->diff($end);
echo $diff->format('%h:%i:%s.%F');