note 98463 added to function.time
| From: | Anonymous at osu1 dot php dot net | Date: | Thu, 17 Jun 2010 12:11:58 +0000 |
| Subject: | note 98463 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170197@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 92.246.4.127
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/98463
Del: integrated -- https://master.php.net/note/delete/98463/integrated
Del: useless -- https://master.php.net/note/delete/98463/useless
Del: bad code -- https://master.php.net/note/delete/98463/bad+code
Del: spam -- https://master.php.net/note/delete/98463/spam
Del: non-english -- https://master.php.net/note/delete/98463/non-english
Del: in docs -- https://master.php.net/note/delete/98463/in+docs
Del: other reasons-- https://master.php.net/note/delete/98463
Reject -- https://master.php.net/note/reject/98463
Search -- https://master.php.net/manage/user-notes.php