Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql: convert timediff() to seconds

Tags:

sql

mysql

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)?

like image 983
randombits Avatar asked Apr 07 '16 20:04

randombits


People also ask

How do I convert time to seconds in MySQL?

The TIME_TO_SEC() function converts a time value into seconds.

How to get seconds from date in MySQL?

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.

How do you convert seconds to HH MM SS in MySQL?

The SEC_TO_TIME() function returns a time value (in format HH:MM:SS) based on the specified seconds.

How does MySQL calculate duration?

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 .


1 Answers

Mysql TIME_TO_SEC is what you are looking for

SELECT TIME_TO_SEC(timediff(time_end, time_begin)) from tbl
like image 192
Seva Kalashnikov Avatar answered Nov 14 '22 22:11

Seva Kalashnikov