MySQL TIME_TO_SEC() Function Examples

MySQL TIME_TO_SEC() Function Examples

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

ParameterDescription
timeIt 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 |
 +--------------+---------+

Recommended Tutorial

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *