note 71375 added to function.time
| From: | pio at i64 dot pl | Date: | Sat, 25 Nov 2006 00:28:39 +0000 |
| Subject: | note 71375 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120065@lists.php.net to get a copy of this message | ||
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/
----
Server IP: 195.136.184.34
Probable Submitter: 83.27.93.53
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/71375
Del: integrated -- https://master.php.net/note/delete/71375/integrated
Del: useless -- https://master.php.net/note/delete/71375/useless
Del: bad code -- https://master.php.net/note/delete/71375/bad+code
Del: spam -- https://master.php.net/note/delete/71375/spam
Del: non-english -- https://master.php.net/note/delete/71375/non-english
Del: in docs -- https://master.php.net/note/delete/71375/in+docs
Del: other reasons-- https://master.php.net/note/delete/71375
Reject -- https://master.php.net/note/reject/71375
Search -- https://master.php.net/manage/user-notes.php