checkdate

(PHP 4, PHP 5, PHP 7)

checkdateValide une date grégorienne

Description

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

Vérifie la validité d'une date formée par les arguments. Une date est considérée comme valide si chaque paramètre est défini correctement.

Liste de paramètres

month

Le mois doit être compris entre 1 et 12.

day

Le jour doit être un jour autorisé par le mois donné. Les années bissextiles sont prises en compte.

year

L'année est comprise entre 1 et 32767 inclus.

Valeurs de retour

Retourne TRUE si la date fournie est valide, FALSE sinon.

Exemples

Exemple #1 Exemple avec checkdate()

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

L'exemple ci-dessus va afficher :

bool(true)
bool(false)

Voir aussi

  • mktime() - Retourne le timestamp UNIX d'une date
  • strtotime() - Transforme un texte anglais en timestamp

add a note add a note

User Contributed Notes 3 notes

up
414
glavic at gmail dot com
3 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
Daniel
1 month ago
@whitej71

In the example you gave, it is correctly returning false. The function example is comparing a string to a known date format. I suggest reading the documentation on the date format to see why you're making the wrong assumption.
up
-15
whitej71 at msu dot edu
9 months ago
The below suggested date validation (from another note) does NOT work for ALL formats and date inputs.

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

A couple of examples should suffice as a warning to test carefully and particularly not to use code that allows passing in of just any date format string:

var_dump(validateDate('7/01/16', 'n/j/y')); # false but should be true
var_dump(validateDate('7/1/16', 'm/d/y')); # false but should be true

Sorry, I don't have a suggested correction.
To Top