View Single Post
  #2 (permalink)  
Old 06-08-09, 06:20 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

function date_calc_diff($timestamp_past$timestamp_future$years true$months true$days true$hours true$mins true$secs true)

{
    
$diff $timestamp_future $timestamp_past;
    
$calc_times = array();
    
$timeleft   = array();
    
    
// Prepare array, depending on the output we want to get.
    
if ($years)  $calc_times[] = array('Year',   'Years',   31104000);
    if (
$months$calc_times[] = array('Month',  'Months',  2592000);
    if (
$days)   $calc_times[] = array('Day',    'Days',    86400);
    if (
$hours)  $calc_times[] = array('Hour',   'Hours',   3600);
    if (
$mins)   $calc_times[] = array('Minute''Minutes'60);
    if (
$secs)   $calc_times[] = array('Second''Seconds'1);

    foreach (
$calc_times AS $timedata)
    {
        list(
$time_sing$time_plur$offset) = $timedata;
        
        if (
$diff >= $offset)
        {
            
$left floor($diff $offset);
            
$diff -= ($left $offset);
            
$timeleft[] = "{$left} " . ($left == $time_sing $time_plur);
        }
    }
    
    return 
$timeleft ? ($timestamp_future $timestamp_past null '-') . implode(', '$timeleft) : 0;


Usage example:
PHP Code:

$diff date_calc_diff($opened$closed);


echo 
$diff// 4 Days, 2 Hours, 29 Minutes, 14 Seconds 
Reply With Quote