Given two dates (e.g., '2008-06-30' and '2008-06-01') I'm wondering the easiest way to calculate the number of days that have elapsed. I wrote this function but can't help wondering if there's like a one-line piece of PHP kung-fu that might be shorter and more reliable. I have a weird feeling this one might break in 2038 or whatever:
/**
* This routine can take two strings representing dates
* and return the number of days that have elapsed between
* the two. By default, it rounds to the nearest day.
* NOTE: It relies on the strtotime() function to parse
* the date strings
* @param string $time1 The starting time
* @param string $time2 The ending time
* @param int $precision Round to this number of decimal points
*/
function get_days_elapsed($time1, $time2, $precision=0) {
$t1 = strtotime($time1);
if ($t1 === FALSE) return FALSE;
$t2 = strtotime($time2);
if ($t2 === FALSE) return FALSE;
$elapsed_seconds = $t2 - $t1;
$elapsed_days = round(($elapsed_seconds) / (60*60*24), $precision);
return $elapsed_days;
}