MySQL TO_SECONDS Function

MySQL TO_SECONDS is one of the Date Functions, which returns the number of seconds from year 0 to the user given date or datetime. The syntax of the TO_SECONDS() Function in MySQL is as shown below:

TO_SECONDS(date or expression);

MySQL TO_SECONDS function Example

The following examples help you understand the use of the To seconds function. Here, we are returning the number of seconds from 0 to different values.

SELECT TO_SECONDS('2018-01-02');

SELECT TO_SECONDS('2018-12-31');

SELECT TO_SECONDS('2018-12-31 10:22:30');
MySQL TO_SECONDS Function 1

MySQL TO_SECONDS Example 2

In this Date method query, we are using NOW, CURDATE, and UTC_TIMESTAMP along with some addition operation inside this TO_SECONDS function. It means we are using the Datetime value in MySQL String format.

SELECT TO_SECONDS(NOW()), TO_SECONDS(NOW() + 25);

SELECT TO_SECONDS(CURDATE()), TO_SECONDS(CURDATE() + 2);

SELECT TO_SECONDS(UTC_TIMESTAMP());
MySQL TO_SECONDS Function 2

Let us see another example of the MySQL To_Seconds function. This time, we are using the YYYYMMDD and YYMMDD formats as the date argument. From the below, 95 means 1995

SELECT TO_SECONDS('20190225');

SELECT TO_SECONDS(750225);

SELECT TO_SECONDS(951231);
MySQL TO_SECONDS Function 3

The to seconds method throw an error if it finds the Wrong Date Or Zero. This example shows you the same. Within the second statement, 65 is a wrong day value.

SELECT TO_SECONDS('0000-00-00');

SELECT TO_SECONDS('2019-12-65');

SELECT TO_SECONDS('2019-12-44 10:23:98');
MySQL TO_SECONDS Function 4