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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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