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]);
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]);
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)
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.
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
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')
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 !
$time = strtotime(min(array($row[0], $row2[0])));
Gives you the earliest time.