MySQL FROM_DAYS() Function
MySQL FROM_DAYS(): Convert a Day Number Back to a Date
The FROM_DAYS() function takes an integer day count N (representing days elapsed since year 0) and converts it back into a DATE value ('YYYY-MM-DD'). It acts as the exact inverse of TO_DAYS().
Data pipelines use FROM_DAYS() after performing integer arithmetic on TO_DAYS() values, such as computing average dates or bucketing numeric day offsets.
Syntax
Pass a single integer day count to FROM_DAYS():
FROM_DAYS(N)
N: An integer representing days since'0000-00-00'. For reference,'0001-01-01'equals day366, and'2026-05-12'equals day740100.
Values of N below 366 return '0000-00-00'.
Practical Example
Suppose your analytics team computes each customer's midpoint order date in demo_orders by averaging TO_DAYS(order_date) and converting the integer back with FROM_DAYS().
SELECT
customer_id,
MIN(order_date) AS first_order,
MAX(order_date) AS latest_order,
FROM_DAYS(ROUND(AVG(TO_DAYS(order_date)))) AS average_order_date
FROM demo_orders
WHERE customer_id IN (14, 29)
GROUP BY customer_id;
| customer_id | first_order | latest_order | average_order_date |
|---|---|---|---|
| 14 | 2026-01-01 | 2026-01-31 | 2026-01-16 |
| 29 | 2026-03-01 | 2026-05-01 | 2026-03-31 |
Pairing TO_DAYS() with AVG() and FROM_DAYS() gives you an exact calendar midpoint because AVG() cannot operate directly on DATE values cleanly.
Common Mistakes & NULL Behavior
Keep these constraints in mind when calling FROM_DAYS():
- Not for pre-Gregorian dates: MySQL documents that
FROM_DAYS()is not meant for dates before the Gregorian calendar adoption on October 15, 1582 (N < 578103), because it assumes Gregorian leap rules backward to year 0. - Do not pass Unix epoch days or timestamps:
FROM_DAYS(19855)returns'0054-05-12', not a 2024 date! Unix timestamps must be converted withFROM_UNIXTIME(), notFROM_DAYS(). NULLor out-of-range values returnNULL: PassingNULL, non-numeric strings, or numbers exceeding'9999-12-31'(N > 3652424) makesFROM_DAYS()returnNULL.- Negative numbers or values under
366: PassingFROM_DAYS(100)orFROM_DAYS(-5)returns'0000-00-00', notNULL.
Frequently Asked Questions
What is the inverse function of FROM_DAYS()?
TO_DAYS(date) is the inverse. FROM_DAYS(TO_DAYS('2026-05-12')) returns '2026-05-12'.
Why did FROM_DAYS(0) return '0000-00-00' instead of NULL?
MySQL treats any day number under 366 as year zero and returns '0000-00-00'. Only NULL, unparseable strings, or numbers past '9999-12-31' return NULL.
Should I use FROM_DAYS() for adding days to a date?
No. ADDDATE(order_date, 10) or order_date + INTERVAL 10 DAY is cleaner and easier to read than FROM_DAYS(TO_DAYS(order_date) + 10).
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.