note 70974 added to function.date
| From: | Bandit at osu1 dot php dot net | Date: | Tue, 07 Nov 2006 01:06:14 +0000 |
| Subject: | note 70974 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119448@lists.php.net to get a copy of this message | ||
So I wanted a little function to output an easy to read but inaccurate date. I came up with the
following (probably very inefficient) little function;
<?php
function ezDate($d) {
$ts = time() - strtotime(str_replace("-","/",$d));
if($ts>31536000) $val = round($ts/31536000,0).' year';
else if($ts>2419200) $val = round($ts/2419200,0).' month';
else if($ts>604800) $val = round($ts/604800,0).' week';
else if($ts>86400) $val = round($ts/86400,0).' day';
else if($ts>3600) $val = round($ts/3600,0).' hour';
else if($ts>60) $val = round($ts/60,0).' minute';
else $val = $ts.' second';
if($val>1) $val .= 's';
return $val;
}
?>
Then I use it as follows;
<?php
echo ucwords(ezDate('2006-09-07 18:42:00')).' Ago';
?>
Which would output;
2 Months Ago
I add an acronym tag around the output also, so my users can mouse-over for the exact date.
Hope it helps someone!
----
Server IP: 202.89.36.242
Probable Submitter: 210.86.115.205
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/70974
Del: integrated -- https://master.php.net/note/delete/70974/integrated
Del: useless -- https://master.php.net/note/delete/70974/useless
Del: bad code -- https://master.php.net/note/delete/70974/bad+code
Del: spam -- https://master.php.net/note/delete/70974/spam
Del: non-english -- https://master.php.net/note/delete/70974/non-english
Del: in docs -- https://master.php.net/note/delete/70974/in+docs
Del: other reasons-- https://master.php.net/note/delete/70974
Reject -- https://master.php.net/note/reject/70974
Search -- https://master.php.net/manage/user-notes.php