PHP Reference ยท Date and Time
PHP checkdate() function
Reference for PHP checkdate(): test whether a month, day and year form a real Gregorian date, with leap years, argument order and what it does not check.
checkdate() answers a simple question: do this month, day and year make a real date in the Gregorian calendar? It knows how many days each month has and handles leap years, so checkdate(2, 29, 2024) is true and checkdate(2, 29, 2025) is false.
It checks only the calendar date. It does not read text, does not check times, and does not use a time zone. That makes it a good second step after you have split user input into numbers.
<?php var_dump(checkdate(2, 30, 2025)); // bool(false)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
checkdate(int $month, int $day, int $year): bool
| Name | Description |
|---|---|
$month |
Month number, 1 to 12. |
$day |
Day of the month, at least 1 and at most the number of days in that month and year. |
$year |
Year, 1 to 32767. |
Return value: true if the combination is a valid Gregorian date, otherwise false. Passing non-integers that cannot be converted raises a TypeError.
Usage notes
- Argument order is month, day, year, like
mktime()after the time parts, and unlike the ISO order ofY-m-d. Swapping them is the usual bug. - It takes numbers, not strings. Split
"2025-02-30"yourself (withexplode()or a regex) and cast the parts toint. - The Gregorian calendar is applied to all years, including those before it was adopted in 1582 or later.
- No time or zone. For a full date-time check, parse with
DateTimeImmutable::createFromFormat()and compare the formatted result with the input, or inspectDateTimeImmutable::getLastErrors(). - Why not
strtotime()?strtotime("2025-02-30")returns a timestamp for March 2, notfalse, so it cannot validate by itself. - Leap years: divisible by 4, except centuries, unless the century is divisible by 400.