Is there a way I can make a query in MySQL that will give me the difference between two timestamps in seconds, or would I need to do that in PHP? And if so, how would I go about doing that?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
How about "TIMESTAMPDIFF":
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff
If you want an unsigned difference, add an
ABS()
around the expression.Alternatively, you can use
TIMEDIFF(ts1, ts2)
and then convert the time result to seconds withTIME_TO_SEC()
.You could use the
TIMEDIFF()
and theTIME_TO_SEC()
functions as follows:You could also use the
UNIX_TIMESTAMP()
function as @Amber suggested in an other answer:If you are using the
TIMESTAMP
data type, I guess that theUNIX_TIMESTAMP()
solution would be slightly faster, sinceTIMESTAMP
values are already stored as an integer representing the number of seconds since the epoch (Source). Quoting the docs:Note that the
TIMEDIFF()
solution only works when thedatetimes
are less than 35 days apart!TIMEDIFF()
returns aTIME
datatype, and the max value for TIME is 838:59:59 hours (=34,96 days)