0

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.

aksu
  • 5,221
  • 5
  • 24
  • 39
Martin Varga
  • 105
  • 2
  • 7
  • **[MICROTIME](http://www.php.net/manual/de/function.microtime.php)** may help ? like $novy = strtotime($input1,microtime()); – Dwza Feb 20 '14 at 15:51

4 Answers4

1

If you use date_create_from_format you can specify the exact date format for php to convert the string representations to:

<?php
$input1 = '00:02:14.31';
$input2 = '00:02:14.32';
$novy = date_create_from_format('H:i:s.u', $input1);
$stary = date_create_from_format('H:i:s.u',$input2);
if ($novy < $stary) {
    echo "1 shorter\n";
} else {
    echo "2 longer\n";
}

Recommended reading: http://ie2.php.net/datetime.createfromformat

kguest
  • 3,804
  • 3
  • 29
  • 31
  • i dont know why but i dont get not only hundreths but also seconds. here is var_dump of `$input2` `object(DateTime)[2] public 'date' => string '2014-02-21 00:02:14' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'UTC' (length=3)` – Martin Varga Feb 21 '14 at 14:38
  • True, you would. There's no mention that you didn't want seconds, and that the problem was that hundredths of seconds was being ignored - this solution addresses that problem perfectly. – kguest Feb 21 '14 at 15:19
0

You could write some conditional logic to test if HH::MM::SS are identical, then simply compare XX, else use the strtotime() function that you are already using

Jason Fingar
  • 3,358
  • 1
  • 21
  • 27
0

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

Carlos Campderrós
  • 22,354
  • 11
  • 51
  • 57
  • This works fine, but i m not sure that it will work always. I m not sure if is best idea to compare numeric information as string. – Martin Varga Feb 21 '14 at 14:48
  • if all your inputs really have the same format then there is no problem. It's usually done when comparing dates with YYYY-MM-DD format. You probably would face problems if you did not write leading 0s though. – Carlos Campderrós Feb 21 '14 at 15:49
0

You are working with durations, not dates. PHP's date and time functions aren't really of any help here. You should parse the string yourself to get a fully numeric duration:

$time = '00:02:14.31';

sscanf($time, '%d:%d:%d.%d', $hours, $minutes, $seconds, $centiseconds);
$total = $centiseconds
       + $seconds * 100
       + $minutes * 60 * 100
       + $hours * 60 * 60 * 100;

var_dump($total);

The total is in centiseconds (100th of a second, the scale of your original input). Multiply/divide by other factors to get in others scales, as needed.

deceze
  • 510,633
  • 85
  • 743
  • 889