Page 1 of 1

Subtracting between timestamps

Posted: Mon Jul 11, 2005 2:02 pm
by AlbinoJellyfish
I have a script where I need to figure out the time between two timestamps in a database and I don't know how I should go about doing this.
So lets say:
$startTime = "2004-05-19 16:03:40" and $endTime = "2004-05-19 16:03:44"
How would I subtract startTime from endTime to get the total number of seconds in between.
It wont always be 4 seconds, it might be a few minutes too.

Posted: Mon Jul 11, 2005 2:05 pm
by hawleyjr
MySQL has a ton of powerful date and time functions

http://dev.mysql.com/doc/mysql/en/date- ... tions.html

Posted: Mon Jul 11, 2005 2:11 pm
by pickle
I take it you've stored your dates as MySQL timestamps? You're in luck! MySQL has the ability to convert it's timestamps into UNIX timestamps. You can then subtract the one from the other and voila!

Code: Select all

SELECT
  UNIX_TIMESTAMP(startTime) - UNIX_TIMESTAMP(endTime) as 'difference'
FROM
  someTable
`difference` is now the difference in seconds.