Comparing milliseconds in php

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

how can I check if date2more or less than 100 milliseconds, then $date 1in and false if not (101 - more or less)

+6
source share
4 answers

If you get your time in this format (I changed 09:09:80to 09:09:40because it was the wrong format)

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

create a custom function because it strtotimedoes not support 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;
}

now just check that the difference is less than 100

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

, , , , 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

+4

php manual strtotime , strtotime.

, : 2017-04-13 09:00:20.100, strtotime ( ), ,

The following function will return true if the dates are within 100 milliseconds, otherwise false. You can pass the sum to compare them as an argument.

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

}


?>
+1
source

PHP 7.1 allows you to do this with DateTime objects ...

Be sure to check all the other answers with a change in the day as a true indicator of a successful process.

Demo

code:

$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";
}

Outputs:

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

0.001
less than or equal to .1
0
source

Source: https://habr.com/ru/post/1016521/


All Articles