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
a34 at yahoo dot com
09-Jul-2007 12: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.

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 03: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;
}
?>