note 42887 deleted from function.time by aidan
| From: | aidan@php.net | Date: | Sat, 08 Oct 2005 01:06:17 +0000 |
| Subject: | note 42887 deleted from function.time by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96631@lists.php.net to get a copy of this message | ||
Note Submitter: kirua at coder-studio spammy dot com
----
Hi all,
in response to a message from jcarroll at nospam theworld dot com, 18-Sep-2003 05:58 :
To calculate the elapsed time since a certain time(), I'd rather do this:
function calcElapsedTime($time)
{
// calculate elapsed time (in seconds!)
$diff = time()-$time;
$daysDiff = 0; $hrsDiff = 0; $minsDiff = 0; $secsDiff = 0;
$sec_in_a_day = 60*60*24;
while($diff >= $sec_in_a_day){$daysDiff++; $diff -= $sec_in_a_day;}
$sec_in_an_hour = 60*60;
while($diff >= $sec_in_an_hour){$hrsDiff++; $diff -= $sec_in_an_hour;}
$sec_in_a_min = 60;
while($diff >= $sec_in_a_min){$minsDiff++; $diff -= $sec_in_a_min;}
$secsDiff = $diff;
return ('(elapsed time '.$daysDiff.'d '.$hrsDiff.'h
'.$minsDiff.'m '.$secsDiff.'s)');
}
that way, you avoid using lots of divisions, you avoid using floor() and you don't need to
update $diff cause it's used as a counter, so it's always up to date.
have a nice day,
Nicolas Boumal