MySQL TO_DAYS() Function

MySQL TO_DAYS() Function; Through this tutorial, i am going to show you MySQL TO_DAYS() function with the help of examples.

MySQL TO_DAYS() Function

MySQL TO_DAYS function is the Date Function, which is used to return the total number of days from year 0 to user given date.

Syntax of MySQL TO_DAYS() Function

The basic syntax of MySQL to_days() is:

TO_DAYS(date)

Here is the date of use in date calculations.

Example 1 – MySQL TO_DAYS() Function

Take an first example using the to_day function; as follows:

SELECT TO_DAYS('2000-12-31');

Output-1

+-----------------------+
| TO_DAYS('2000-12-31') |
+-----------------------+
|                730850 |
+-----------------------+

Note: Note however that the MySQL documentation advises that this function is not for use with those values ​​that were before the arrival of the Gregorian calendar (1582).

Example 2 – MySQL TO_DAYS() Function

Let’s take an second example using curdate() and to_days() of MySQL; as follows:

    SELECT 
    CURDATE(),
    TO_DAYS(CURDATE());

Output-2

+------------+--------------------+
| CURDATE()  | TO_DAYS(CURDATE()) |
+------------+--------------------+
| 2019-07-16 |             737621 |
+------------+--------------------+

First of all, we use the MySQL CURDATE () function to return the current date, after that, we pass that function to the TO_DAYS () function to return the number of days that day 0 is.

Example 3 – MySQL TO_DAYS() Function

Let’s take an third example using the msyql now() with to_days() function; as follows:

    SELECT 
    NOW(),
    TO_DAYS(NOW());

Output-3

+------------------------+--------------------+
| NOW()                  | TO_DAYS(CURDATE()) |
+------------------------+--------------------+
| 2019-07-16 18:10:45    |             737621 |
+------------+--------------------------------+

First of all, use the MySQL NOW() function to return the current date and time value, after that, we pass that function to the TO_DAYS () function to return the number of days that day 0 is.

Conclusion

MySQL TO_DAYS() Function , you have learned how to use MySQL TO_DAYS() function. You have also learned different FROM_DAYS () and TO_DAYS () function of MySQL.

Leave a Comment