I have a query that looks something like the following:
select timediff(time_end, time_begin) from tbl
time_end
and time_begin
are both of type datetime
Is there a trivial way I can convert the time there to return an integer (seconds)?
The TIME_TO_SEC() function converts a time value into seconds.
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59.
The SEC_TO_TIME() function returns a time value (in format HH:MM:SS) based on the specified seconds.
To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR . To get the difference in seconds as we have done here, choose SECOND .
Mysql TIME_TO_SEC is what you are looking for
SELECT TIME_TO_SEC(timediff(time_end, time_begin)) from tbl
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With