2017-04-13 126 views
6
$date1 = "2017-04-13 09:09:80:300" 
$date2 = "2017-04-13 09:09:80:400" 

比较我怎么能检查date2或多或少是100毫秒,则在$date 1和虚假如果不是(101 - 或多或少)毫秒PHP

回答

4

你的问题,虽然看似出现简单,实际上相当难看,因为PHP的strtotime()函数从时间戳中截断毫秒。实际上,它甚至不会正确处理您的问题中的时间戳$date1$date2。一种解决方法是修剪时间戳的毫秒部分,使用strtotime()从纪元开始获取毫秒数,然后使用正则表达式获取并将毫秒部分添加到该数量。

$date1 = "2017-04-13 09:09:40:300"; 
$date2 = "2017-04-13 09:09:40:400"; 

preg_match('/^.+:(\d+)$/i', $date1, $matches); 
$millis1 = $matches[1]; 
$ts1 = strtotime(substr($date1, 0, 18))*1000 + $millis1; 
preg_match('/^.+:(\d+)$/i', $date2, $matches); 
$millis2 = $matches[1]; 
$ts2 = strtotime(substr($date2, 0, 18))*1000 + $millis2; 

if (abs($ts1 - $ts2) < 100) { 
    echo "within 100 millseconds"; 
} 
else { 
    echo "not within 100 millseconds"; 
} 

演示在这里:

Rextester

+0

否否否不能那么容易! +1 –

+0

@HankyPanky如果这是Postgres中的同一个问题,我已经在运行:-) –

+0

你试过了吗?这个不成立。毫秒被剥离到秒比较......如果差值为999,它将返回true – Peter

5

如果您将自己的这种格式的时间(我改变09:09:8009:09:40,因为它是不正确的格式)

$date1 = "2017-04-13 09:09:40:300" 
$date2 = "2017-04-13 09:09:40:400" 

创建自定义功能,因为strtotime不支持ms

function myDateToMs($str) { 
    list($ms, $date) = array_map('strrev', explode(":", strrev($str), 2)); 
    $ts = strtotime($date); 
    if ($ts === false) { 
     throw new \InvalidArgumentException("Wrong date format"); 
    } 
    return $ts * 1000 + $ms; 
} 

现在只是检查确实差小于100

$lessOrEqual100 = abs(myDateToMs($date1) - myDateToMs($date2)) <= 100; 
+1

你的答案比我的+1更清洁。 –

1

根据第二的php manual for strtotime馏分是允许的,但目前由strtotime函数忽略。

这意味着你可以表达你的日期,这样2017-04-13 09:00:20.100让他们通过的strtotime没有错误解析(让他们futureproofed),然后使用自定义功能进行比较公正的日期的毫秒部分。如果时间戳相同

如果日期在100毫秒内,下面的函数将返回true,否则返回false。你可以通过数量来比较它们作为参数。

<?php 
date_default_timezone_set ("UTC"); 

$date1 = "2017-04-13 09:00:20.100"; 
$date2 = "2017-04-13 09:00:20.300"; 

// pass date1, date2 and the amount to compare them by 
$res = compareMilliseconds($date1,$date2,100); 
var_dump($res); 

function compareMilliseconds($date1,$date2,$compare_amount){ 

    if(strtotime($date1) == strtotime($date2)){ 

     list($throw,$milliseond1) = explode('.',$date1); 
     list($throw,$milliseond2) = explode('.',$date2); 

     return (($milliseond2 - $milliseond1) < $compare_amount); 
    } 

} 


?> 
0

PHP 7.1,您可以用datetime对象做到这一点...

一定要与天的变化,以作为成功过程的真实指标测试所有其他的答案。

Demo

代码:

$dt1 = DateTime::createFromFormat('Y-m-d H:i:s:u e', "2017-04-14 0:00:00:000 UTC"); 
$dt2 = DateTime::createFromFormat('Y-m-d H:i:s:u e', "2017-04-13 23:59:59:999 UTC"); 
var_export($dt1->format('Y-m-d H:i:s:u')); 
echo "\n"; 
var_export($dt2->format('Y-m-d H:i:s:u')); 
echo "\n"; 
//var_export($dt1->diff($dt2)); 
echo "\n"; 
$diff=$dt1->diff($dt2); 
// cast $diff as an array so array_intersect_assoc() can be used 
if(sizeof(array_intersect_assoc(['y'=>0,'m'=>0,'d'=>0,'h'=>0,'i'=>0],(array)$diff))==5){ 
    // years, months, days, hours, and minutes are all 0 
    var_export($micro=round(abs($diff->s+$diff->f),3)); 
    // combine seconds with microseconds then test 
    echo "\n"; 
    if($micro>.1){ 
     echo "larger than .1"; 
    }else{ 
     echo "less than or equal to .1"; 
    } 
}else{ 
    echo "too large by units larger than seconds"; 
} 

输出:

'2017-04-14 00:00:00:000000' 
'2017-04-13 23:59:59:999000' 

0.001 
less than or equal to .1