MySQL TO_DAYS() Function Example

MySQL TO_DAYS() Function Example

MySQL TO_DAYS() Function; Through this tutorial, we would love to share with you how to use MySQL TO_DAYS() function with the help of examples. T

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 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.

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

Next, 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.

Recommended MySQL Tutorials

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

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, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL 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 *