note 44441 deleted from function.time by tomsommer

From: Date: Thu, 05 Aug 2004 19:33:50 +0000
Subject: note 44441 deleted from function.time by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74077@lists.php.net to get a copy of this message
Note Submitter: webmaster@NO.ikrontik.SPAM.tk ---- 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"); } ?>

« previous php.notes (#74077) next »