MySQL TO_DAYS() Function
The TO_DAYS()
function in MySQL calculates the number of days between a specified date and the hypothetical date "0000-00-00" (year 0). This is useful for date-based calculations or when you need to represent dates as a numerical value.
TO_DAYS(): Definition and Usage
TO_DAYS()
converts a date into a day number. This number represents the number of days that have elapsed since the beginning of the Julian calendar (year 0). The output is an integer. It's important to note that TO_DAYS()
only works reliably with dates within the Gregorian calendar system.
Syntax
Syntax
TO_DAYS(date)
Parameter Values
Parameter | Description |
---|---|
date |
The date value. This is required. |
Related Function
The FROM_DAYS()
function performs the reverse operation, converting a day number back into a date.
Examples
Calculating Days Since Year 0
This example calculates the number of days between June 20th, 2017 and year 0.
Syntax
SELECT TO_DAYS("2017-06-20");
Output
736854
Calculating Days Since Year 0 from a DATETIME Value
This demonstrates using TO_DAYS()
with a datetime value. The time component is ignored; only the date part is used.
Syntax
SELECT TO_DAYS("2017-06-20 09:34:00");
Output
736854