checkdate

(PHP 3, PHP 4, PHP 5)

checkdate -- 验证一个格里高里日期

说明

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

如果给出的日期有效则返回 TRUE,否则返回 FALSE。检查由参数构成的日期的合法性。日期在以下情况下被认为有效:

  • year 的值是从 1 到 32767

  • month 的值是从 1 到 12

  • Day 的值在给定的 month 所应该具有的天数范围之内,闰年已经考虑进去了。

例子 1. checkdate() 例子

<?php
var_dump
(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
?>

上例将输出:

bool(true)
bool(false)

参见 mktime()strtotime()


add a note add a note User Contributed Notes
moklet
20-Mar-2006 09:34
ANd now it accepts date formats like yyyy/mm/dd as well

<?php

/**
* Checks for a valid date
*
* @param string  Date in the format given by the format parameter.
* @param integer Disallow years more than $yearepsilon from now (in future as well as in past)
* @param string  Formatting string. Has to be one of 'dmy', 'dym', 'mdy', 'myd', 'ydm' or 'ymd'. (Default is 'ymd' for ISO 8601 compability)
* @return array [ year, month, day ]
* @since 1.0
*/

function datecheck($date, $yearepsilon=5000, $format='ymd') {
 
$date=str_replace("/", "-", $date);
 
$format = strtolower($format);
  if (
count($datebits=explode('-',$date))!=3) return false;
 
$year = intval($datebits[strpos($format, 'y')]);
 
$month = intval($datebits[strpos($format, 'm')]);
 
$day = intval($datebits[strpos($format, 'd')]);

  if ((
abs($year-date('Y'))>$yearepsilon) || // year outside given range
    
($month<1) || ($month>12) || ($day<1) ||
     ((
$month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
     (
$day>30+(($month>7)^($month&1)))) return false; // date out of range

 
return array(
  
'year' => $year,
  
'month' => $month,
  
'day' => $day
 
);
}

?>
</Life>.org
05-Mar-2006 05:07
Taking the comments of Hightechguy into account, here is a version with variable date formatting:

<?php

/**
* Checks for a valid date
*
* @param string  Date in the format given by the format parameter.
* @param integer Disallow years more than $yearepsilon from now (in future as well as in past)
* @param string  Formatting string. Has to be one of 'dmy', 'dym', 'mdy', 'myd', 'ydm' or 'ymd'. (Default is 'ymd' for ISO 8601 compability)
* @return array [ year, month, day ]
* @since 1.0
*/

function datecheck($date, $yearepsilon=5000, $format='ymd') {
 
$format = strtolower($format);
  if (
count($datebits=explode('-',$date))!=3) return false;
 
$year = intval($datebits[strpos($format, 'y')]);
 
$month = intval($datebits[strpos($format, 'm')]);
 
$day = intval($datebits[strpos($format, 'd')]);

  if ((
abs($year-date('Y'))>$yearepsilon) || // year outside given range
    
($month<1) || ($month>12) || ($day<1) ||
     ((
$month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
     (
$day>30+(($month>7)^($month&1)))) return false; // date out of range

 
return array(
  
'year' => $year,
  
'month' => $month,
  
'day' => $day
 
);
}

?>
Hightechguy
01-Feb-2006 06:17
As an addition to the function contributed by </Life>.org:
You may want to use different date format "YYYY-MM-DD" like "2006-02-01", because
a) it's default for mySQL and
b) relatively easily retrieved from MSSQL if you use CONVERT function with style=ODBC canonical, which gives yyyy-mm-dd hh:mi:ss(24h) style

Finally function looks like:
<?php

  
function dateCheck($date, $yearepsilon=5000) { // inputs format is "YYYY-MM-DD" ONLY !
      
if (count($datebits=explode('-',$date))!=3) return false;
      
$year = intval($datebits[0]);
      
$month = intval($datebits[1]);
      
$day = intval($datebits[2]);

       if ((
abs($year-date('Y'))>$yearepsilon) || // year outside given range
      
($month<1) || ($month>12) || ($day<1) ||
       ((
$month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
       (
$day>30+(($month>7)^($month&1)))) return false; // date out of range

      
return checkdate($month,$day,$year );
   }
?>
</Life>.org
21-Aug-2005 12:35
The function of phil@philmo contains a few errors...
I corrected them as far as I found them and tried to improve the code.

<?php

/**
* Checks for a valid date
*
* @param string  Date in the format "MM-DD-YYYY"
* @param integer Disallow years more than $yearepsilon from now (in future as well as in past)
* @return array [ month, day, year ]
* @since 1.0
*/

/* fixed:
** - leap years
** - december now is allowed
** - removed unnecessary checking
** if you find any more mistakes, please correct them. :)
*/

function datecheck($date, $yearepsilon=5000) {
  if (
count($datebits=explode('-',$date))!=3) return false;
 
$month = intval($datebits[0]);
 
$day = intval($datebits[1]);
 
$year = intval($datebits[2]);

  if ((
abs($year-date('Y'))>$yearepsilon) || // year outside given range
    
($month<1) || ($month>12) || ($day<1) ||
     ((
$month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
     (
$day>30+(($month>7)^($month&1)))) return false; // date out of range

 
return array(
  
'year' => $year,
  
'month' => $month,
  
'day' => $day
 
);
}

?>
Matt Browne
22-Jul-2005 01:58
Here's a date validation snippet I wrote, since I wasn't able to find one that was flexible enough. It accepts dates in m-d-y format (2- or 4-digit year) and both dashes and slashes as separators.

$strDate = $yourDate;
$isValid = false;

if (ereg('^([0-9]{1,2})[-,/]([0-9]{1,2})[-,/](([0-9]{2})|([0-9]{4}))$', $strDate)) {
   $dateArr = split('[-,/]', $param->value);
   $m=$dateArr[0]; $d=$dateArr[1]; $y=$dateArr[2];
   $isValid = checkdate($m, $d, $y);
}

if (!$isValid) {
   // take appropriate action
}

// then, to obtain the timestamp
$date = mktime(null,null,null,$m,$d,$y);
Zoe Blade
20-Jul-2005 09:31
If you need to check a MySQL format date, here's a function to do it:

/*
 * Check a MySQL (ISO 8601 based) format date
 *
 * The first part splits the date up into its component parts, checking that
 * they appear to be roughly OK. The regex specifically looks for:
 * four digits between 1000 and 9999, -, two digits between 01 and 12, -,
 * two digits between 01 and 31).
 *
 * The second part makes sure that the date is definitely valid (taking into
 * account leap years).
 *
 * See http://dev.mysql.com/doc/mysql/en/datetime.html for details on the MySQL
 * date format.
 */

function checkmysqldate($isodate)
{
  if (preg_match("/^([123456789][[:digit:]]{3})-(0[1-9]|1[012])-
(0[1-9]|[12][[:digit:]]|3[01])$/", $isodate, $date_part) && checkdate($date_part[2], $date_part[3], $date_part[1]))
  {
   return true;
  }
  else
  {
   return false;
  }
}

(The form made me insert a character return between the hyphen and open bracket in the regex line due to wrapping. Just join those two lines together to get the code to work!)
martin a pavel
19-Jul-2005 05:14
<?php

/**
 * Number of days in selected Month of selected Year
 * @return int
 */
function DaysInMonth ( $Year, $MonthInYear )
{
   if (
in_array ( $MonthInYear, array ( 1, 3, 5, 7, 8, 10, 12 ) ) )
       return
31;

   if (
in_array ( $MonthInYear, array ( 4, 6, 9, 11 ) ) )
       return
30;

   if (
$MonthInYear == 2 )
       return (
checkdate ( 2, 29, $Year ) ) ? 29 : 28;
  
   return
false;
}

?>
cristianDOTzuddas [SPAM] gmail DOT com
09-Jul-2005 05:38
This function returns TRUE if the passed string contains a valid MySQL date.

The allowed format is YYYY-MM-DD and the supported range is from '1000-01-01' to '9999-12-31' (MySQL limitations).

Here is the code:
http://www.codeflower.com/index.php?a=showCode&id=36

Ex.:
is_valid_mysql_date('2005-05-21');  <--  TRUE
is_valid_mysql_date('2005-5-30');  <--  FALSE