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.
Subtracting between timestamps
Moderator: General Moderators
-
AlbinoJellyfish
- Forum Commoner
- Posts: 76
- Joined: Sun Apr 04, 2004 7:39 pm
MySQL has a ton of powerful date and time functions
http://dev.mysql.com/doc/mysql/en/date- ... tions.html
http://dev.mysql.com/doc/mysql/en/date- ... tions.html
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!
`difference` is now the difference in seconds.
Code: Select all
SELECT
UNIX_TIMESTAMP(startTime) - UNIX_TIMESTAMP(endTime) as 'difference'
FROM
someTableReal programmers don't comment their code. If it was hard to write, it should be hard to understand.