note 105419 deleted from function.time by danbrown

From: Date: Wed, 17 Aug 2011 15:35:14 +0000
Subject: note 105419 deleted from function.time by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182532@lists.php.net to get a copy of this message
Note Submitter: bsegura AT redaccenir DOT com ---- Get difference between two days in words. Ie. "2 months and 1 week ago" <?php /** * Get difference between two days in words. Ie. "2 months and 1 week ago" * * @param date Unix timestamp */ public static function getReadableTimeToDate( $pastDate ) { $timeAgo = time() - $pastDate; $dateRanges = array ( 31536000 => array( 'year', 'years' ), 2419200 => array( 'month', 'months' ), 604800 => array( 'week', 'weeks' ), 86400 => array( 'day', 'days' ), 3600 => array( 'hour', 'hours' ), 60 => array( 'minute', 'minutes' ) ); // max no. of ranges to add to date. Example values: // 1: "2 years ago" // 2: "2 years and 1 week ago" or "2 years and 3 days ago" // 3: "2 years and 1 week and 4 days ago" $nMaxRangesInDate = 2; // First value (year in this case) is a must. But second value is not always necessary, especially if you don't need an accurate date. Ie: // "1 year and 2 days" those 2 days many times doesn't change anything // This variable will set how big the number should be to be included into date. Ie: // If your real date is "1 year and 3 months ago". If you use: // $nMinTimeToAdd = 2; // result is: "1 year ago" // $nMinTimeToAdd = 3; // result is: "1 year and 3 months ago" $nMinTimeToAdd = 3; $finalDate = array(); foreach( $dateRanges as $seconds => $sufix ) { $t = floor($timeAgo / $seconds); if ( $t > 0 ) { if( count($finalDate) == 0 || $t >= $nMinTimeToAdd ) { $finalDate[] = "$t {$sufix[$t>1?1:0]}"; $timeAgo -= $seconds*$t; } --$nMaxRangesInDate; } if( $nMaxRangesInDate == 0 ) break; } return implode(" and ", $finalDate). " ago"; } ?> Modification from original found at: http://blog.mayflower.de/archives/317-Put-out-the-age-of-a-date-in-words.html#c158624

« previous php.notes (#182532) next »