3
we had a similar problem when developing xhelp - our helpdesk application. We created a custom function to take the difference between two times and break it down into units:
The function is used like this:
//Assumes that original time is stored in $time2
//Get Current TimeStamp
$time1 = time();
//Get Number of elapsed seconds
$elapsed = $time1 - $time2;
$elapsed = xhelpGetElaspedTime($elapsed);
//Now $elapsed is an array:
// $elasped['years'] contains the number of years
// $elapsed['weeks'] contains the number of weeks
// $elapsed['days'] contains the number of days
// $elapsed['hours'] contains the number of hours
// $elapsed['minutes'] contains the number of minutes
// $elapsed['seconds'] contains the number of seconds
And the function is defined like so:
function xhelpGetElapsedTime($time)
{
//Define the units of measure
$units = array('years' => (365*60*60*24) /*Value of Unit expressed in seconds*/,
'weeks' => (7*60*60*24),
'days' => (60*60*24),
'hours' => (60*60),
'minutes' => 60,
'seconds' => 1);
$local_time = $time;
$elapsed_time = array();
//Calculate the total for each unit measure
foreach($units as $key=>$single_unit) {
$elapsed_time[$key] = floor($local_time / $single_unit);
$local_time -= ($elapsed_time[$key] * $single_unit);
}
return $elapsed_time;
}