When working with PHP, you may need to validate whether month, day and year describe a real Gregorian date. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
checkdate: Returns true only when the supplied calendar parts form an actual date.
What Is checkdate in PHP?
Validate whether month, day and year describe a real Gregorian date. Returns true only when the supplied calendar parts form an actual date.
Syntax:
checkdate(int $month, int $day, int $year): bool
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: Argument order is month, day, year. It does not parse user-entered date strings.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
var_dump(checkdate(2, 29, 2024));
Output:
bool(true)
The code focuses on the main operation. Returns true only when the supplied calendar parts form an actual date.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
var_dump(checkdate(2, 29, 2025));
Output:
bool(false)
What changes? Argument order is month, day, year. It does not parse user-entered date strings.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$submitted = ["day" => 31, "month" => 4, "year" => 2026];
if (!checkdate($submitted["month"], $submitted["day"], $submitted["year"])) {
echo "Invalid date";
}
Output:
Invalid date
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Returns true only when the supplied calendar parts form an actual date.
Important Behavior and Edge Cases
- Core behavior: Returns true only when the supplied calendar parts form an actual date.
- Inputs and results: Argument order is month, day, year. It does not parse user-entered date strings.
- Alternatives: date_create_from_format() parses strings while checkdate() validates numeric parts.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
checkdate | Validate whether month, day and year describe a real gregorian date. |
| Related PHP approach | date_create_from_format() parses strings while checkdate() validates numeric parts. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Argument order is month, day, year. It does not parse user-entered date strings. |
| Confusing this API with a related operation | date_create_from_format() parses strings while checkdate() validates numeric parts. |
| Assuming output is identical across environments | Check return values and required extensions or versions. Use explicit locale, timezone or encoding settings when they affect the result. |
Key Takeaways
- checkdate helps you validate whether month, day and year describe a real Gregorian date.
- Returns true only when the supplied calendar parts form an actual date.
- Argument order is month, day, year. It does not parse user-entered date strings.
Use checkdate when you need to validate whether month, day and year describe a real Gregorian date. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.