PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

date_add> <Fonctions Date/Heure
Last updated: Fri, 26 Dec 2008

view this page in

checkdate

(PHP 4, PHP 5)

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 comptes.

year

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

Valeurs de retour

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

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



date_add> <Fonctions Date/Heure
Last updated: Fri, 26 Dec 2008
 
add a note add a note User Contributed Notes
checkdate
bmauser at gmail
16-Dec-2008 03:24
Here is function that checks date if matches given format and validity of the date.

<?php

/**
 * Checks date if matches given format and validity of the date.
 * Examples:
 * <code>
 * is_date('22.22.2222', 'mm.dd.yyyy'); // returns false
 * is_date('11/30/2008', 'mm/dd/yyyy'); // returns true
 * is_date('30-01-2008', 'dd-mm-yyyy'); // returns true
 * is_date('2008 01 30', 'yyyy mm dd'); // returns true
 * </code>
 * @param string $value the variable being evaluated.
 * @param string $format Format of the date. Any combination of <i>mm<i>, <i>dd<i>, <i>yyyy<i>
 * with single character separator between.
 */
function is_date($value, $format = 'mm.dd.yyyy'){
   
    if(
strlen($value) == 10 && strlen($format) == 10){
       
       
// find separator. Remove all other characters from $format
       
$separator_only = str_replace(array('m','d','y'),'', $format);
       
$separator = $separator_only[0]; // separator is first character
       
       
if($separator && strlen($separator_only) == 2){
           
// make regex
           
$regexp = str_replace('mm', '[0-1][0-9]', $value);
           
$regexp = str_replace('dd', '[0-3][0-9]', $value);
           
$regexp = str_replace('yyyy', '[0-9]{4}', $value);
           
$regexp = str_replace($separator, "\\" . $separator, $value);
           
            if(
$regexp != $value && preg_match('/'.$regexp.'/', $value)){

               
// check date
               
$day   = substr($value,strpos($format, 'd'),2);
               
$month = substr($value,strpos($format, 'm'),2);
               
$year  = substr($value,strpos($format, 'y'),4);
               
                if(@
checkdate($month, $day, $year))
                    return
true;
            }
        }
    }
    return
false;
}

?>
parris dot varney at gmail dot com
11-Dec-2008 04:24
I put together an is_date function using checkdate.  Works the same as is_numeric.

<?php
   
public static function is_date($date)
    {
       
$date = str_replace(array('\'', '-', '.', ','), '/', $date);
       
$date = explode('/', $date);

        if(   
count($date) == 1 // No tokens
           
and    is_numeric($date[0])
            and   
$date[0] < 20991231 and
            (   
checkdate(substr($date[0], 4, 2)
                        ,
substr($date[0], 6, 2)
                        ,
substr($date[0], 0, 4)))
        )
        {
            return
true;
        }
       
        if(   
count($date) == 3
           
and    is_numeric($date[0])
            and   
is_numeric($date[1])
            and
is_numeric($date[2]) and
            (   
checkdate($date[0], $date[1], $date[2]) //mmddyyyy
           
or    checkdate($date[1], $date[0], $date[2]) //ddmmyyyy
           
or    checkdate($date[1], $date[2], $date[0])) //yyyymmdd
       
)
        {
            return
true;
        }
       
        return
false;
    }
?>
doob_ at gmx dot de
26-Nov-2008 05:47
<?php

/*
** check a date
** dd.mm.yyyy || mm/dd/yyyy || dd-mm-yyyy || yyyy-mm-dd
*/

function check_date($date) {
    if(
strlen($date) == 10) {
       
$pattern = '/\.|\/|-/i';    // . or / or -
       
preg_match($pattern, $date, $char);
       
       
$array = preg_split($pattern, $date, -1, PREG_SPLIT_NO_EMPTY);
       
        if(
strlen($array[2]) == 4) {
           
// dd.mm.yyyy || dd-mm-yyyy
           
if($char[0] == "."|| $char[0] == "-") {
               
$month = $array[1];
               
$day = $array[0];
               
$year = $array[2];
            }
           
// mm/dd/yyyy    # Common U.S. writing
           
if($char[0] == "/") {
               
$month = $array[0];
               
$day = $array[1];
               
$year = $array[2];
            }
        }
       
// yyyy-mm-dd    # iso 8601
       
if(strlen($array[0]) == 4 && $char[0] == "-") {
           
$month = $array[1];
           
$day = $array[2];
           
$year = $array[0];
        }
        if(
checkdate($month, $day, $year)) {    //Validate Gregorian date
           
return TRUE;
       
        } else {
            return
FALSE;
        }
    }else {
        return
FALSE;    // more or less 10 chars
   
}
}

check_date('21.02.1983');
check_date('21-02-1983');
check_date('02/21/1983'); // Common U.S. writing
check_date('1983-02-21'); // iso 8601

?>
saturn at ax dot com dot tw
26-Aug-2008 08:06
I wrote a simple function to converter datetime to UNIX timestamp. If the input time with error format, the function will return current timestamp.

<?php
function datetime2timestamp($datetime)
{
   
$datetime = str_replace('-', ' ', $datetime);
   
$datetime = str_replace('/', ' ', $datetime);
   
$datetime = str_replace(':', ' ', $datetime);
   
$array = explode(' ', $datetime);

   
$year   = $array[0];
   
$month  = $array[1];
   
$day    = $array[2];
   
$array[3] ? $hour   = $array[3] : $hour   = '00';
   
$array[4] ? $minute = $array[4] : $minute = '00';
   
$array[5] ? $second = $array[5] : $second = '00';
   
    if (
preg_match("/^(\d{4}) (\d{2}) (\d{2}) ([01][0-9]|2[0-3]) ([0-5][0-9]) ([0-5][0-9])$/", "$year $month $day $hour $minute $second", $matches)) {
        if (
checkdate($matches[2], $matches[3], $matches[1])) {
        return
mktime(intval($hour), intval($minute), intval($second), intval($month), intval($day), intval($year));
        } else {
        return
time();
        }       
    } else {
    return
time();
    }
}
?>
el dot vartauy__ at t__gmail dot com
29-Feb-2008 07:18
for funny leap year detection:
<?php
function is_leap($year=NULL) {
    return
checkdate(2, 29, ($year==NULL)? date('Y'):$year); // true if is a leap year
}
?>
wasile_ro[at]yahoo[dot]com
08-Oct-2007 04:30
here's a cool function to validate a mysql datetime:

<?php
function isValidDateTime($dateTime)
{
    if (
preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
        if (
checkdate($matches[2], $matches[3], $matches[1])) {
            return
true;
        }
    }

    return
false;
}
?>
jens wittmann
28-Aug-2007 05:29
for checking the rime use this:

<?php
function checktime($hour, $minute) {
    if (
$hour > -1 && $hour < 24 && $minute > -1 && $minute < 60) {
        return
true;
    }
}
?>
brenig code
14-Aug-2007 08:21
<?php

/**
* check a date combo of the 2
*/
function checkData($date)
{
    if (!isset(
$date) || $date=="")
    {
        return
false;
    }
  
    list(
$dd,$mm,$yy)=explode("/",$date);
    if (
$dd!="" && $mm!="" && $yy!="")
    {
    if (
is_numeric($yy) && is_numeric($mm) && is_numeric($dd))
        {
            return
checkdate($mm,$dd,$yy);

        }
    }  
    return
false;

}
?>
a34 at yahoo dot com
09-Jul-2007 02:21
checkData function posted below does not consider a date entered such as 03/27c/2000.   The c will cause it to crash.  Here is the fix.

<?php
function checkData($mydate) {
      
    list(
$yy,$mm,$dd)=explode("-",$mydate);
    if (
is_numeric($yy) && is_numeric($mm) && is_numeric($dd))
    {
        return
checkdate($mm,$dd,$yy);
    }
    return
false;           
}
?>
manuel84**at**mp4**dot**it
04-Dec-2006 04:49
If you have a date like this gg/mm/aaaa and you'd like to verify that it is in the Italian Format you can use a function like this.
For other date format you can take this code and simply modify the list and explode line
<?php
/**
* check a date in the Italian format
*/
function checkData($date)
{
    if (!isset(
$date) || $date=="")
    {
        return
false;
    }
   
    list(
$dd,$mm,$yy)=explode("/",$date);
    if (
$dd!="" && $mm!="" && $yy!="")
    {
        return
checkdate($mm,$dd,$yy);
    }
   
    return
false;
}
?>

date_add> <Fonctions Date/Heure
Last updated: Fri, 26 Dec 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites