#native_company# #native_desc#
#native_cta#

Timediff

By Johannes Meyer-Myklestad
on January 17, 2004

Version: 2

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: /* TIMEDIFFERENCE SCRIPT BY FRAGGALOT @ ATCC.NO, COPYRIGHT 2002
* GET HOW LONG AGO SINCE SOME TIMESTAMP
* syntax;
* timediff(timestamp[,how big[,special format]])
* timestamp: format of time()
* how big: ex. 2: 3hours 4minutes.
* another ex. 4: 1day 3hours 4minutes 34seconds
* special format: if set to true the timestamp var comes in hh/minmin/ss/mm/dd/yyyy format.
* */

<?
// timediff(timestamp[,how big])
	/**
	 * @param date 		UNIX_TIMESTAMP
	 * @param quantity 	INT, number of atts->out
	 * @return 		STR, agostr->out
	 **/

    function timediff($date,$quantity=2) {
	//get time difference
        $diff = time() - $date;
        
        //getting how long ago its all been
        $y_ago  = date("Y", $diff) -1970;
        $mo_ago = date("m", $diff) -1;
        $d_ago  = date("d", $diff) -1;
        $h_ago  = date("H", $diff);
        $mi_ago = date("i", $diff);
        $s_ago  = date("s", $diff);

        //create w_ago from d_ago
        if($d_ago >= 7) {
            $w_ago = floor($d_ago/7);
            $d_ago -= ($w_ago*7);
        } else { 
            $w_ago = 0; 
        }

        //create array containing ago_vars
        $foo_array = array ("y" => $y_ago, "mo" => $mo_ago, "w" => $w_ago, "d" => $d_ago, "h" => $h_ago, "m" => $mi_ago, "s" => $s_ago);

        $i = 0;
        foreach($foo_array as $key=>$value) {
            $value = ereg_replace("0+","0",$value);
            if($value != 0 && $i < $quantity) {
                $str .= (isset($str)) ? ", $key:$value" : "$key:$value";
                $i++;     
            }
        }

        //in $str there are now 1 or more ago_vars
        $str = (strpos($str,",") !== false) ? explode(",", $str) : array($str);
    
        $moo = array();
        foreach($str as $string) {
            preg_match("/(.*):(.*)$/", $string,$match);
            $string2 = trim($match[2])." ";
            $string2 = preg_replace("/^0(.+)/","1",$string2);
            $keyname = trim($match[1]);
            $string2 .= $keyname;
            array_push($moo, str_replace(" ","",$string2));
        }

        $final = ($moo[1] !== false) ? implode("",$moo) : $final;
        return $final; 
    }

//example stamp for testing purposes.
$stamp = 1066065981;
print '1: '.timediff($stamp,1).'<br />';
print '2: '.timediff($stamp).'<br />';
print '3: '.timediff($stamp,3).'<br />';
print '4: '.timediff($stamp,4).'<br />';
print '5: '.timediff($stamp,5).'<br />';
print '6: '.timediff($stamp,6).'<br />';
print '7: '.timediff($stamp,7).'<br />';
?>