Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] calculate time difference between present, past and future
To change between past, present, future, you need to do things only on the display side (no code patch needed).
The default function is:
timeago([referencedate], [timepointer], [measureby], [autotext])

$timepointer is from where to start calculating and is only needed if you are calculating the difference between 2 specific times. In other words, the present is not one of those times.
$referencedate is the time you're calculating to. This is required for all calculations.
$measureby is the unit to use
$autotext is whether to add "days from now" or "ago" to the string. Option only for present dates, otherwise, this variable is set to off.
For a standard present-past, use: timeago(pastdate);
It will automatically detect the time since in the appropriate units.
There is some coding error because rounding plays a huge roll. If it's 390 days from now, instead of saying "1 year, 25 days", the function will say "1 year". Post a better solution if you can.
Save 10,930 time
    function timeago($referencedate=0,$timepointer='',$measureby='', $autotext=true){
     
    if($timepointer == '') $timepointer = time();
    $Raw = $timepointer-$referencedate;
    $Clean = abs($Raw);
    $calcNum = array(array('s', 60),
                     array('m', 60*60),
                     array('h', 60*60*60),
                     array('d', 60*60*60*24),
                     array('y', 60*60*60*24*365));
    $calc = array('s' => array(1, 'second'),
                  'm' => array(60, 'minute'),
                  'h' => array(60*60, 'hour'),
                  'd' => array(60*60*24, 'day'),
                  'y' => array(60*60*24*365, 'year'));
    
    if($measureby == ''){
        $usemeasure = 's';
    
        for($i=0; $i<count($calcNum); $i++){
            if($Clean <= $calcNum[$i][1]){
                $usemeasure = $calcNum[$i][0];
                $i = count($calcNum);
            }       
        }
    }else{
        $usemeasure = $measureby;
    }
    
    $datedifference = floor($Clean/$calc[$usemeasure][0]);
    
    if($autotext==true && ($timepointer==time())){
        if($Raw < 0){
            $prospect = ' from now';
        }else{
            $prospect = ' ago';
        }
    }
    
    if($referencedate != 0){
        if($datedifference == 1){
            return $datedifference . ' ' . $calc[$usemeasure][1] . ' ' . $prospect;
        }else{
            return $datedifference . ' ' . $calc[$usemeasure][1] . 's ' . $prospect;
        }
    }else{
        return 'No input time referenced.';
    }
}