MySQL TIME_TO_SEC() function; In this tutorial, we would love to share with you how to use TIME_TO_SEC() function with the help of examples.
MySQL TIME_TO_SEC() function
Definition:- The TIME_TO_SEC() function is pre-built MySQL function, which is used to convert / change given time value into seconds value.
Syntax
TIME_TO_SEC(time)
Parameters of time_to_sec() function
Parameter | Description |
---|---|
time | It accept single parameters and required. This is a time value. |
Example 1 – Basic Usage
Here, we will take an example to demonstrate. This example convert a given time to second value.
SELECT TIME_TO_SEC('00:05:00');
The output of the above query is the following:
+-------------------------+ | TIME_TO_SEC('00:05:00') | +-------------------------+ | 300 | +-------------------------+
In the above example, we have used the %r format specifier, which is used to formats the time value in 12-hour (hh:mm:ss with AM or PM).
Example 2 – With Hour Value
Here’s, take the next example. Now we will convert hours value to second value using time_to_sec() function:
SELECT TIME_TO_SEC('01:00:00');
The output of the above query is the following:
+-------------------------+ | TIME_TO_SEC('01:00:00') | +-------------------------+ | 3600 | +-------------------------+
Example 3 – Current Time to Second
Here, take a next example with current time. We will convert current time to seconds. You can see the example below:
SELECT CURTIME() AS 'CurrentTime', TIME_TO_SEC(CURTIME()) AS 'Seconds';
The output of the above query is the following:
+--------------+---------+ | CurrentTime | Seconds | +--------------+---------+ | 08:22:25 | 30145 | +--------------+---------+