function nicetime($fromDate, $toDate = NULL, $precision = -1, $separator = ', ', $divisors = NULL) {
if ( is_null( $toDate ) ) {
$toDate = $this->date_get('Asia/Jakarta');
}
// Determine the difference between the largest and smallest date
$dates = array(strtotime($fromDate), strtotime($toDate));
$difference = max($dates) - min($dates);
// Return the formatted interval
return $this->format_interval($difference, $precision, $separator, $divisors);
}
/**
* Formats any number of seconds into a readable string
*
* @param int Seconds to format
* @param string Seperator to use between divisors
* @param int Number of divisors to return, ie (3) gives '1 Year, 3 Days, 9 Hours' whereas (2) gives '1 Year, 3 Days'
* @param array Set of Name => Seconds pairs to use as divisors, ie array('Year' => 31536000)
* @return string Formatted interval
*/
function format_interval($seconds, $precision = -1, $separator = ', ', $divisors = NULL)
{
// Default set of divisors to use
if(!isset($divisors)) {
$divisors = Array(
'Year' => 31536000,
'Month' => 2628000,
'Day' => 86400,
'Hour' => 3600,
'Minute' => 60,
'Second' => 1);
}
arsort($divisors);
// Iterate over each divisor
foreach($divisors as $name => $divisor)
{
// If there is at least 1 of thie divisor's time period
if($value = floor($seconds / $divisor)) {
// Add the formatted value - divisor pair to the output array.
// Omits the plural for a singular value.
if($value == 1)
$out[] = "$value $name";
else
$out[] = "$value {$name}s";
// Stop looping if we've hit the precision limit
if(--$precision == 0)
break;
}
// Strip this divisor from the total seconds
$seconds %= $divisor;
}
// FIX
if (!isset($out)) {
$out[] = "0" . $name;
}
var_dump($out);
// Join the value - divisor pairs with $separator between each element
return implode($separator, $out);
}