note 50652 added to function.time
| From: | webmasteratcom1netdotcom at osu1 dot php dot net | Date: | Sun, 06 Mar 2005 02:31:52 +0000 |
| Subject: | note 50652 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85983@lists.php.net to get a copy of this message | ||
In response to kirua at coder-studio spammy dot com's post, I liked his time function a lot,
and so I made it more user friendly. I added a years feature, and it also automatically adds or
removes the 's' on the end of year(s), day(s), hour(s) minute(s) second(s) based on the
number thats output. . .
<?
function calcElapsedTime($time)
{
// calculate elapsed time (in seconds!)
$diff = time()-$time;
$yearsDiff = floor($diff/60/60/24/365);
$diff -= $yearsDiff*60*60*24*365;
$daysDiff = floor($diff/60/60/24);
$diff -= $daysDiff*60*60*24;
$hrsDiff = floor($diff/60/60);
$diff -= $hrsDiff*60*60;
$minsDiff = floor($diff/60);
$diff -= $minsDiff*60;
$secsDiff = $diff;
return ($yearsDiff.' year'.(($yearsDiff <> 1) ? "s" :
"").', '.$daysDiff.' day'.(($daysDiff <> 1) ? "s" :
"").', '.$hrsDiff.' hour'.(($hrsDiff <> 1) ? "s" :
"").', '.$minsDiff.' minute'.(($minsDiff <> 1) ? "s" :
"").', '.$secsDiff.' second'.(($secsDiff <> 1) ? "s" :
""));
}
?>
To use simply feed it something like:
$operation = calcElapsedTime(mktime(0, 0, 0, 6, 20, 2000)); // time elapsed since 6/20/2000.
echo "This website has been in operation $operation.<br>";
Output:
This website has been in operation 4 years, 259 days, 18 hours, 1 minute, 12 seconds.
-or-
echo "This page was last updated
".calcElapsedTime(filectime('duration.php')).".<br>"; // time elapsed
since duration.php was last updated.
Output:
This page was last updated 0 years, 0 days, 1 hour, 12 minutes, 55 seconds.
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50652
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50652&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50652&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50652&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50652&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50652&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50652&report=yes
Search -- http://master.php.net/manage/user-notes.php