Re: finding the difference

From: Date: Thu, 25 Jan 2001 11:19:58 +0000
Subject: Re: finding the difference
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-36599@lists.php.net to get a copy of this message
At 3:16 AM -0500 1/25/01, Website4S@aol.com wrote:
I have a date and time stored in the following format: day:month:year:hour:minutes Example 27:01:2001:08:13 I want to work out the difference between that and the current time on my server, but I can`t find any math functions in the PHP manual that allows you to work out the diference. In the end I hope to be able to display '2 days 23 hours and 55 minutes' type results, similar to the way in which eLance.com shows the time remaining on a project. Anyone have any suggestion if it is at all possible? Thanks Ade
Take a look under date/time functions: http://www.php.net/manual/en/ref.datetime.php You want to convert your stored time into a Unix timestamp (that is, seconds since midnight 1 jan 1970), subtract the unix timestamp of the current time, and then display the result in your desired format: $Deadline = ' 27:01:2001:08:13'; $Bits = explode(':', $Deadline); $TimeDiff = mktime($Bits[3],$Bits[4],$Bits[5],$Bits[1],$Bits[0],$Bits[2]) - time(); # $TimeDiff is now the time difference, expressed in seconds. # Now, use modulo operator repeatedly to get day, hour & minute values. # BTW, there are 86400 seconds/day, 3600 seconds/hour. $NDays = $TimeDiff % 86400; $R = $TimeDiff - ($NDays * 86400); $NHours = $R % 3600; $R = $R - ($NHours * 3600); $NMinutes = round($R/60); echo "You have $NDays days, $NHours hours and $NMinutes left!"; If you're feeling obfuscatory, you could compact the calculations into (I think): $NHours = ($R = $TimeDiff - (($NDays = $TimeDiff % 86400) * 86400)) % 3600; $NMinutes = round(($R - ($NHours * 3600))/60); Why you would _want_ to do this, I don't know. I suppose if you're an ex-FORTRAN programmer frustrated by the lack of assigned and computed GOTOs and want to take it out on others, maybe. OK, now I'm rambling. And showing my age... - steve Usual untested, off-top-of-head, no-warranty code caveats apply. -- +--- "They've got a cherry pie there, that'll kill ya" ------------------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +-------------------------------------- FBI Special Agent Dale Cooper ---+

« previous php.general (#36599) next »