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 example. Also, we will explain TIME_TO_SEC() function definition, syntax, and various 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

  1. MySQL Tutorial With Examples
  2. PHP Date and Time functions tutorial
  3. MySQL: DATE() And TIME() Functions 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, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, 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 *