MariaDB TO_SECONDS() Function

In MariaDB, TO_SECONDS() is a built-in function that returns the number of seconds since the 0 year (0000-00-00) for a given datetime.

TO_SECONDS() and FROM_DAYS() are similar.

MariaDB TO_SECONDS() Syntax

This is the syntax of the MariaDB TO_SECONDS() function:

TO_SECONDS(datetime)

Parameters

datetime

Required. A datetime value.

If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'TO_SECONDS'.

Return value

The MariaDB TO_SECONDS() function returns the number of seconds since the 0 year for the specified datetime.

If the argument is NULL, the TO_SECONDS() function will return NULL.

MariaDB TO_SECONDS() Examples

This statement returns the number of seconds since the 0 year of 2023-01-01:

SELECT TO_SECONDS('2023-01-01');

Output:

+--------------------------+
| TO_SECONDS('2023-01-01') |
+--------------------------+
|              63839750400 |
+--------------------------+

The MariaDB TO_SECONDS() function allow you to use a datetime value:

SELECT TO_SECONDS('2023-01-01 00:00:01');

Output:

+-----------------------------------+
| TO_SECONDS('2023-01-01 00:00:01') |
+-----------------------------------+
|                       63839750401 |
+-----------------------------------+

If you want to return the number of seconds since the 0 year of today, you can use the NOW() function:

SELECT
    NOW(),
    TO_SECONDS(NOW());

Output:

+---------------------+-------------------+
| NOW()               | TO_SECONDS(NOW()) |
+---------------------+-------------------+
| 2023-01-12 14:24:59 |       63840752699 |
+---------------------+-------------------+

Conclusion

In MariaDB, TO_SECONDS() is a built-in function that returns the number of seconds since the 0 year (0000-00-00) for a given datetime.