note 105419 added to function.time

From: Date: Tue, 16 Aug 2011 14:32:08 +0000
Subject: note 105419 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-182512@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.197 Probable Submitter: 188.77.187.81 ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- https://master.php.net/note/edit/105419 Del: integrated -- https://master.php.net/note/delete/105419/integrated Del: useless -- https://master.php.net/note/delete/105419/useless Del: bad code -- https://master.php.net/note/delete/105419/bad+code Del: spam -- https://master.php.net/note/delete/105419/spam Del: non-english -- https://master.php.net/note/delete/105419/non-english Del: in docs -- https://master.php.net/note/delete/105419/in+docs Del: other reasons-- https://master.php.net/note/delete/105419 Reject -- https://master.php.net/note/reject/105419 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#182512) next »