note 50946 deleted from function.time by aidan
| From: | aidan@php.net | Date: | Sat, 08 Oct 2005 01:03:45 +0000 |
| Subject: | note 50946 deleted from function.time by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96609@lists.php.net to get a copy of this message | ||
Note Submitter: mlseuk at hotmail dot com
----
Here's a very simple and easily extensible way of calculating elapsed times. It automatically
takes care of leap years and the like and doesn't require that you do any complicated modulo
arithmetic at all! It accepts a timestring in a format that is compatible with GNU time formats
(i.e. anything that strtotime will accept) and returns the elapsed time as an associative array.
Its output has 1 second accuracy. Probably needs a bit off fiddling to get it working perfectly.
function elapsed_time($timestring)
{
//Get the time difference.
$x = split(",", date("Y,m,d,H,i,s", time() - strtotime($timestring)));
//Return an associative array. Subtract 01.01.1970 because
//time() and strtotime() return time values WRT the Unix Epoch.
return array("years" => $x[0] - 1970,
"months" => $x[1] - 1,
"days" => $x[2] - 1,
"hours" => $x[3],
"minutes" => $x[4],
"seconds" => $x[5]);
}