1

Can somebody help me compare two times and found out which one is less than other. by less I mean which one has older time than other.

Times are retrieved from database :

$time1 = strtotime($row[0]);
$time2 = strtotime($row2[0]);
Mike Ezzati
  • 2,968
  • 1
  • 23
  • 34

6 Answers6

3

If you only want to know which date is earlier, you can simply compare them.

Here is an example from the Php manual :

$d1 = new DateTime('1492-01-01');

$d2 = new DateTime('1492-12-31');


var_dump($d1 < $d2);

var_dump($d1 > $d2);

var_dump($d1 == $d2);

?>

Results :

bool(true)

bool(false)

bool(false)
Theox
  • 1,363
  • 9
  • 20
0

You have to convert your input dates into timestamps, then just subtract one from the other.

$timestampA = 1221672010;
$timestampB = 1221671010;
$diff       = $timestampA - $timestampB;

if ( $diff > 0 ) {
  // $timestampA date is after $timestampB date
} else if ( $diff < 0 ) {
  // $timestampA date is before $timestampB date
} else {
  // dates are equal
}

To convert string date into timestamp you can use strtotime function.

hsz
  • 148,279
  • 62
  • 259
  • 315
0

I believe that by 2 times you mean 2 dates. You can always convert them to timestamp and them compare the two numbers.

for that you can use strtotime function

Paul Moldovan
  • 208
  • 1
  • 2
  • 8
0

If you mean time() for times data use min function: http://fr.php.net/min

Otherwise, http://php.net/manual/en/datetime.diff.php

$time1 = time();
$time2 = time()-3600;
var_dump(min($time1, $time2));

Or

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days')
GoT
  • 156
  • 4
0

you can use the time() function witch returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

or if you have the date-time in string format you can use strtotime() to convert it before comparaison.

The smallest number is the oldest time !

0x1gene
  • 3,349
  • 4
  • 29
  • 48
0
$time = strtotime(min(array($row[0], $row2[0])));

Gives you the earliest time.

powtac
  • 40,542
  • 28
  • 115
  • 170