note 86479 added to function.time
| From: | jeff at osu1 dot php dot net | Date: | Tue, 21 Oct 2008 21:11:10 +0000 |
| Subject: | note 86479 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145546@lists.php.net to get a copy of this message | ||
I created a function to output human readable format from a timestamp
<?php
function readable_time($timestamp, $num_times = 2)
{
//this returns human readable time when it was uploaded (array in seconds)
$times = array(31536000 => 'year', 2592000 => 'month', 604800 =>
'week', 86400 => 'day', 3600 => 'hour', 60 =>
'minute', 1 => 'second');
$now = time();
$secs = $now - $timestamp;
$count = 0;
$time = '';
foreach ($times AS $key => $value)
{
if ($secs >= $key)
{
//time found
$s = '';
$time .= floor($secs / $key);
if ((floor($secs / $key) != 1))
$s = 's';
$time .= ' ' . $value . $s;
$count++;
$secs = $secs % $key;
if ($count > $num_times - 1 || $secs == 0)
break;
else
$time .= ', ';
}
}
return $time;
}
//the second paramater is the number of rounds to output. if you put in more than the time allows,
it will just show as much as it can
echo readable_time(time()-(60*60*24*8)-3681, 5) . ' ago<br />';
echo readable_time(time()-(60*60*24*8)-3681) . ' ago';
?>
outputs:
1 week, 1 day, 1 hour, 1 minute, 21 seconds ago
1 week, 1 day ago
----
Server IP: 209.41.74.194
Probable Submitter: 206.63.116.217
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/86479
Del: integrated -- https://master.php.net/note/delete/86479/integrated
Del: useless -- https://master.php.net/note/delete/86479/useless
Del: bad code -- https://master.php.net/note/delete/86479/bad+code
Del: spam -- https://master.php.net/note/delete/86479/spam
Del: non-english -- https://master.php.net/note/delete/86479/non-english
Del: in docs -- https://master.php.net/note/delete/86479/in+docs
Del: other reasons-- https://master.php.net/note/delete/86479
Reject -- https://master.php.net/note/reject/86479
Search -- https://master.php.net/manage/user-notes.php