FROM_DAYS() Examples – MySQL

In MySQL, the FROM_DAYS() function returns a date value based on the number of days provided as an argument.

This article contains examples to demonstrate.

Syntax

The syntax goes like this:

FROM_DAYS(N)

Where N is the number of days from day 0.

Example

Here’s an example to demonstrate.

SELECT FROM_DAYS(366);

Result:

+----------------+
| FROM_DAYS(366) |
+----------------+
| 0001-01-01     |
+----------------+

Although note that the MySQL documentation advises that this function is not intended for use with values that precede the advent of the Gregorian calendar (1582).

Here’s an example with a later date:

SELECT FROM_DAYS(645000);

Result:

+-------------------+
| FROM_DAYS(645000) |
+-------------------+
| 1765-12-13        |
+-------------------+

And a later date again:

SELECT FROM_DAYS(745000);

Result:

+-------------------+
| FROM_DAYS(745000) |
+-------------------+
| 2039-09-28        |
+-------------------+

 

FROM_DAYS() vs TO_DAYS()

The FROM_DAYS() function is the opposite of TO_DAYS(), which, given a date date, returns the day number. Here’s an example to demonstrate the relationship between these two functions:

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

Result:

+------------+--------------------+-------------------------------+
| CURDATE()  | TO_DAYS(CURDATE()) | FROM_DAYS(TO_DAYS(CURDATE())) |
+------------+--------------------+-------------------------------+
| 2018-06-26 |             737236 | 2018-06-26                    |
+------------+--------------------+-------------------------------+

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