note 50652 deleted from function.time by aidan

From: Date: Sat, 08 Oct 2005 01:03:52 +0000
Subject: note 50652 deleted from function.time by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96611@lists.php.net to get a copy of this message
Note Submitter: webmaster at com1net dot com ---- 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.

« previous php.notes (#96611) next »