TO_DAYS() Function Examples – MySQL

TO_DAYS() Function Examples – MySQL

In this MySQL TO_DAYS() tutorial, we would love to share with you how to use MySQL TO_DAYS() function with various examples. Today we are going to show you uses and syntax of TO_DAY() function with examples.

MySQL TO_DAYS() Function

Using the TO_DAYS () function in MySQL, to know how many days have elapsed since 0 days for a particular date. For e.g., you can give this function today’s date, and how many days it will return for 0 days.

Syntax

The basic syntax of MySQL to_days() is:

TO_DAYS(date)

Here is the date of use in date calculations.

Now we take a few examples for demonstration

Example-1

We take the first example of the to_day functions for demostration.

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

we take the second example with a curdate() of MySQL.

    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

TO_DAYS() v/s FROM_DAYS()

Contrary to the FROM_DAYS () function TO_DAYS (), which returns the number of days, given a date. Here’s an example to display the relationship between FROM_DAYS () and TO_DAYS () functions:

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

Output-3

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

So in this example, we use TO_DAYS () to return the number of days from the current date. Then I use FROM_DAYS () to return the date from that value (which, as expected, returns to the original value of CURDATE ()).

Example-4

we take another example with a now() function of MySQL. Before we use curdate() function of mysql with to_days function. Curdate() is used to return the current date and now() is used to return the current date with time.

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

Output-4

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

First of all, we 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

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

You may like

  1. WEEKOFYEAR() Function Examples – MySQL
  2. WEEKDAY() Function Examples – MySQL
  3. LAST_DAY MySQL Function Examples
  4. DAYOFYEAR() Function Examples – MySQL
  5. MySQL YEARWEEK() Function Examples – MySQL
  6. MySQL weekday function with example
  7. MySQL CURRENT TIMESTAMP Function Examples
  8. Get month name from date in mysql
  9. mysql get first day of the current month
  10. mysql last day of previous month
  11. Get month number from month name in mysql
  12. mysql get day of week number from date
  13. Mysql Get Data Of Current Date, Week, Month, YEAR
  14. Query For Get Data Of Last Day, Week, Month, YEAR – Mysql

If you have any questions or thoughts to share, use the comment form below to reach us.

One reply to TO_DAYS() Function Examples – MySQL

Leave a Reply

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