note 50946 added to function.time

From: Date: Mon, 14 Mar 2005 16:47:06 +0000
Subject: note 50946 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-86427@lists.php.net to get a copy of this message
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]); } ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50946 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50946&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50946&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50946&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50946&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50946&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50946&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86427) next »