Search code examples
phpstrtotimemilliseconds

compare times with hundredths in php


I try to compare two swim times in php. They are like HH:MM:SS.XX (XX are hundreths). I get them as string and i want to find out which swimmer is faster. I tryed to convert them using strtotime(). It works with hours, minutes and seconds but it ignores hundreths. Here is my code for better explanation:

$novy = strtotime($input1); 
$stary = strtotime($input2);
if($novy < $stary){
   //change old(stary) to new(novy)
}

If $input1 is 00:02:14.31 and $input2 is 00:02:14.32 both $novy and $stary are 1392850934. I read some solution to similar problem in javascript but I can`t use it, this must be server-side. Thank you for help.


Solution

  • If the format is really HH:MM:SS.XX (ie: with leading 0's), you can just sort them alphabetically:

    <?php
    $input1 = '00:02:14.31';
    $input2 = '00:02:14.32';
    if ($input1 < $input2) {
        echo "1 faster\n";
    } else {
        echo "2 faster\n";
    }
    

    It prints 1 faster