PHP 8.0.12 Released!

checkdate

(PHP 4, PHP 5, PHP 7, PHP 8)

checkdateValidate a Gregorian date

Description

checkdate(int $month, int $day, int $year): bool

Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined.

Parameters

month

The month is between 1 and 12 inclusive.

day

The day is within the allowed number of days for the given month. Leap years are taken into consideration.

year

The year is between 1 and 32767 inclusive.

Return Values

Returns true if the date given is valid; otherwise returns false.

Examples

Example #1 checkdate() example

<?php
var_dump
(checkdate(12312000));
var_dump(checkdate(2292001));
?>

The above example will output:

bool(true)
bool(false)

See Also

  • mktime() - Get Unix timestamp for a date
  • strtotime() - Parse about any English textual datetime description into a Unix timestamp

add a note add a note

User Contributed Notes 4 notes

up
601
glavic at gmail dot com
8 years ago
With DateTime you can make the shortest date&time validator for all formats.

<?php

function validateDate($date, $format = 'Y-m-d H:i:s')
{
   
$d = DateTime::createFromFormat($format, $date);
    return
$d && $d->format($format) == $date;
}

var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # true

var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
# or
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true

var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
# or
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false
# ...
up
1
hugo dot REMOVE dot voerman at priva dot NOTNEEDED dot com
16 days ago
I use a slight variation of the validateDate() above, to also allow validating dates like: '2021-10-05T13:55:01Z' with DateTime::ATOM like:

<?php
function validateDate($date, $format = 'Y-m-d H:i:s')
{
   
// replace a 'Z' at the end by '+00:00'
   
$date = preg_replace('/(.*)Z$/', '${1}+00:00', $date);

   
$d = DateTime::createFromFormat($format, $date);
    return
$d && $d->format($format) == $date;
}
?>

This means a call like:
<?php
var_dump
(validateDate('2021-10-05T13:55:01Z', DateTime::ATOM));
?>
will output true

I use this in a filter_input wrapper like:

<?php
function fi_validateDate($format)
{
    return function(
$value = null) use ($format) {
        return
validateDate($value, $format) ? $value : null;
    };
}
?>

so I can do stuff like:

<?php
$dateFrom
= filter_input(INPUT_GET, 'dateFrom', FILTER_CALLBACK, ['options' => fi_validateDate(DateTime::ATOM)]);
?>
up
-7
Anonymous
8 months ago
based on :
https://www.php.net/manual/es/function.checkdate.php#113205

there is a HOF php8 refactor

        $isValidFormatDateHOF = fn (string $format) =>  static fn (string $date) => ($dateTimeInmutable = \DateTimeImmutable::createFromFormat( $format, $date)) && $dateTimeInmutable->format( $format) === $date;

$isAtom = $isvalidFormatDateHOF(DateTimeInterface::ATOM)

$isAtom('2020-02-02')  //false
$isAtom('2021-02-15T19:17:01+00:00') //true
up
-58
MarkAgius at markagius dot co dot uk
1 year ago
Note:
checkdate() will return true and not false with the non existing dates 3/09/1752 to 13/09/1752
To Top