note 71375 deleted from function.time by nlopess
| From: | nlopess@php.net | Date: | Mon, 15 Jan 2007 18:41:05 +0000 |
| Subject: | note 71375 deleted from function.time by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121746@lists.php.net to get a copy of this message | ||
Note Submitter: pio at i64 dot pl
----
Function when($ts) takes timestamp on input and returns human readable time difference. Example
output:
- 3 minutes ago,
- 5 hours ago,
- 4 days ago,
- 2 weeks ago,
- 3 months ago,
- 4 years ago
function kiedy($ts) {
$ts=time()-$ts;
if ($ts<60)
// <1 minute
return $ts." seconds ago";
elseif ($ts<60*60)
// <1 hour
return floor($ts/60)." minutes ago";
elseif ($ts<60*60*2)
// <2 hour
return "1 hour ago";
elseif ($ts<60*60*24)
// <24 hours = 1 day
return floor($ts/60*60)." hours ago";
elseif ($ts<60*60*24*2)
// <2 days
return "1 day ago";
elseif ($ts<60*60*24*7)
// <7 days = 1 week
return floor($ts/60*60*24)." days ago";
elseif ($ts<60*60*24*30.5)
// <30.5 days ~ 1 month
return floor($ts/60*60*24*7)." weeks ago";
elseif ($ts<60*60*24*365)
// <365 days = 1 year
return floor($ts/60*60*24*30.5)." months ago";
else
// more than 1 year
return floor($ts/60*60*24*365)." years ago";
};
Polish users can find localized version (with correct conjugation) at http://blog.i64.pl/BlogPio/SU/kiedy-to-bylo
--
Pio
http://blog.i64.pl/BlogPio/