note 98463 deleted from function.time by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jun 2010 13:19:37 +0000 |
| Subject: | note 98463 deleted from function.time by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170200@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
I took andypsv at rcdrugs dot com function and made it better.
Added input check, future time and improved precision. Also fixed the trailing space and improved
readability.
<?php
function nicetime($timestamp,$recursive = false) {
if(empty($timestamp) || preg_match('/[0-9]/',$timestamp) === 0 || !is_bool($recursive))
{ return __METHOD__ .': Invalid input'; }
$cur_tm = time(); $dif = $cur_tm-$timestamp; $tense = 'ago';
if($dif < 0) { $tense = 'from now'; $dif = abs($dif); }
$pds =
array('second','minute','hour','day','week','month','year','decade');
$lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v]) < 1); $v--);
if($v < 0) { $v = 0; }
$_tm = $cur_tm-($dif%$lngh[$v]);
if(floor($no) <> 1) { $pds[$v] .='s'; }
$str = sprintf('%d %s',($recursive?$no:round($no)),$pds[$v]);
if(($recursive)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) { $str .= ', '.
nicetime($_tm); }
return $str .' '. $tense;
}
?>