note 70974 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 14 Dec 2014 11:32:16 +0000 |
| Subject: | note 70974 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201160@lists.php.net to get a copy of this message | ||
Note Submitter: Bandit
----
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!