note 102671 deleted from function.time by danbrown

[email protected] Mon, 28 Feb 2011 06:40:00 -0800
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: patrick at pjmccoy dot com 

----

I took yasmary at gmail dot com's function and updated it a little bit to give it a true facebook style output (yesterday if 1 day ago, the day of the week if within the last week, and then the month day and year (if necessary) for times greater than a week ago).  I also tweaked it to adjust the time according to a valid timezone string (DateTimeZone).

<?php 
function time_ago($date,$timezone='America/Denver') {
    if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day");
    $lengths         = array("60","60","24","7");
    
    $now             = time();
    $unix_date       = $date;
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // state the tense
    $tense         = "ago";
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    $return = "$difference $periods[$j] {$tense}";
    
    /* If the return is days, we do some special processing */
    if ($j == 3) {
    	// set the timezone using DateTime
    	$timezone = new DateTimeZone($timezone);
	$dateTime = new DateTime();
	$dateTime->setTimestamp($unix_date);
	$dateTime->setTimezone($timezone);
	    
	$nowDateTime = new DateTime();
	$nowDateTime->setTimestamp($now);
	$nowDateTime->setTimezone($timezone);
	    
	    
    	if ($difference == 1) {
    		$return = 'Yesterday';
    	} else if ($difference <= 7) {
    		//$return = strftime('%A',$unix_date);
    		$return = $dateTime->format('l');
    	} else {
    		//if (strftime('%Y',$unix_time) == strftime('%Y',$now)) {
    		if ($dateTime->format('Y') == $nowDateTime->format('Y')) {
    			//$return = strftime('%B %e',$unix_date);
    			$return = $dateTime->format('F j');
    		} else {
    			//$return = strftime('%B %e, %Y',$unix_date);
    			$return = $dateTime->format('F j, Y');
    		}
    		
    	}
    	//$return .= ' at '.strftime('%l:%M%P',$unix_date);
    	$return .= ' at '.$dateTime->format('g:ia');
    }
    
    return $return;
}

?>

And if you don't care about the timezone: 
<?php 
function time_ago($date) {
    if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day");
    $lengths         = array("60","60","24","7");
    
    $now             = time();
    $unix_date       = $date;
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // state the tense
    $tense         = "ago";
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    $return = "$difference $periods[$j] {$tense}";
    
    /* If the return is days, we do some special processing */
    if ($j == 3) {
    	if ($difference == 1) {
    		$return = 'Yesterday';
    	} else if ($difference <= 7) {
    		$return = strftime('%A',$unix_date);
    	} else {
    		if (strftime('%Y',$unix_date) == strftime('%Y',$now)) {
    			$return = strftime('%B %e',$unix_date);
    		} else {
    			$return = strftime('%B %e, %Y',$unix_date);;
    		}
    		
    	}
    	$return .= ' at '.strftime('%l:%M%P',$unix_date);
    }
    
    return $return;
}
?>

Returns look like this: 
33 minutes ago
1 hour ago
Yesterday at 11:06am
Tuesday at 2:39pm
February 18 at 8:53pm
December 19, 2010 at 4:46pm