Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Milliseconds compare in php

Tags:

date

php

time

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

how can I check if the date2 is more or less 100 milliseconds then $date 1 in and false if not (101 - more or less)

like image 474
Scopi Avatar asked Apr 13 '17 10:04

Scopi


3 Answers

Your question, while deceptively appearing simple, is actually fairly ugly, because it is the case that PHP's strtotime() function truncates milliseconds from a timestamp. Actually, it won't even correctly process the timestamps $date1 and $date2 which you have in your question. One workaround is to trim off the millisecond portion of the timestamp, use strtotime() to get milliseconds since the epoch, then use a regex to obtain and add the millisecond portion to this amount.

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

Demo here:

Rextester

like image 158
Tim Biegeleisen Avatar answered Oct 23 '22 16:10

Tim Biegeleisen


If you get your time in such format (I changed 09:09:80 to 09:09:40 as it was incorrect format)

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

create custom function since strtotime doesn't 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 does difference is less than 100

$lessOrEqual100 = abs(myDateToMs($date1) - myDateToMs($date2)) <= 100;
like image 5
Peter Avatar answered Oct 23 '22 14:10

Peter


According to the php manual for strtotime fractions of a second are allowed, although currently ignored by the strtotime function.

This means you could express your dates like this 2017-04-13 09:00:20.100 to have them parsed by strtotime without error (keeping them futureproofed) and then use a custom function to compare just the millisecond portion of the dates if the timestamps are the same

The below function will return true if the dates are within 100 milliseconds, false otherwise. You can pass in the amount to compare them by 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);
  }

}


?>
like image 2
GreensterRox Avatar answered Oct 23 '22 15:10

GreensterRox