note 44441 added to function.time

From: Date: Thu, 05 Aug 2004 19:30:34 +0000
Subject: note 44441 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-74076@lists.php.net to get a copy of this message
I saw your time difference functions, but there's one flaw with the latter, posted by Nicolas Boumal. Although it is the best of the ones posted here, it will still return values that equal 0, such as "0 days". This is an updated version of his function: <?php // If you pass only the first parameter, it will calculate it // from the current time function calcElapsedTime($time, $curtime=FALSE) { if($curtime === FALSE) $curtime = time(); $diff = $curtime - $time; $weeksDiff = 0; $daysDiff = 0; $hrsDiff = 0; $minsDiff = 0; while($diff >= 604800) { $weeksDiff++; $diff -= 604800; } $weeksDiff = ($weeksDiff > 0) ? (($weeksDiff == 1) ? "1 week" : "$weeksDiff weeks, ") : ""; while($diff >= 86400) { $daysDiff++; $diff -= 86400; } $daysDiff = ($daysDiff > 0) ? (($daysDiff == 1) ? "1 day" : "$daysDiff days, ") : ""; while($diff >= 3600) { $hrsDiff++; $diff -= 3600; } $hrsDiff = ($hrsDiff > 0) ? (($hrsDiff == 1) ? "1 hour" : "$hrsDiff hours, ") : ""; while($diff >= 60) { $minsDiff++; $diff -= 60; } $minsDiff = ($minsDiff > 0) ? (($minsDiff == 1) ? "1 minute, " : "$minsDiff minutes, ") : ""; $diff = ($diff > 0) ? (($diff == 1) ? "1 second" : "$diff seconds") : "0 seconds"; return ("" . $weeksDiff . $daysDiff . $hrsDiff . $minsDiff . $diff . " ago"); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44441 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44441&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44441&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74076) next »