PHPdatevalidation

我试图build立一个PHPdatevalidation(MM / DD / YYYY),但我有问题。 这里是我得到的一个样本:

$date_regex = '%\A(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d\z%'; $test_date = '03/22/2010'; if (preg_match($date_regex, $test_date,$_POST['birthday']) ==true) { $errors[] = 'user name most have no spaces';` 

你可以使用checkdate 。 例如,像这样的东西:

 $test_date = '03/22/2010'; $test_arr = explode('/', $test_date); if (checkdate($test_arr[0], $test_arr[1], $test_arr[2])) { // valid date ... } 

更偏执的做法,并不盲目地相信input:

 $test_date = '03/22/2010'; $test_arr = explode('/', $test_date); if (count($test_arr) == 3) { if (checkdate($test_arr[0], $test_arr[1], $test_arr[2])) { // valid date ... } else { // problem with dates ... } } else { // problem with input ... } 

你可以使用DateTime类的一些方法,这可能是方便的; 即DateTime::createFromFormat()DateTime::getLastErrors()一起使用。

 $test_date = '03/22/2010'; $date = DateTime::createFromFormat('m/d/Y', $test_date); $date_errors = DateTime::getLastErrors(); if ($date_errors['warning_count'] + $date_errors['error_count'] > 0) { $errors[] = 'Some useful error message goes here.'; } 

这甚至可以让我们看到究竟是什么导致dateparsing警告/错误(查看$date_errors中的warningserrors数组)。

虽然checkdate是好的,这似乎很简洁的函数来validation,也可以给格式。 [ 来源 ]

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

function是从这个答案或php.net复制

在date无效的情况下需要额外的->format() ,但是createFromFormat仍然设法创build一个DateTime对象。 例如:

 // Gives "2016-11-10 ..." because Thursday falls on Nov 10 DateTime::createFromFormat('DM j Y', 'Thu Nov 9 2016'); // false, Nov 9 is a Wednesday validateDate('Thu Nov 9 2016', 'DM j Y'); 

而不是庞大的DateTime对象..只是使用核心date()函数

 function isValidDate($date, $format= 'Ym-d'){ return $date == date($format, strtotime($date)); } 

REGEX应该是最后的手段。 PHP有几个函数可以为你validation。 在你的情况下,checkdate是最好的select。 http://php.net/manual/en/function.checkdate.php

Nicolas的解决scheme是最好的。 如果你想在正则expression式中,

尝试这个,

这将validation,01/01/1900至12/31/2099匹配无效的date,如2月31日接受破折号,空格,正斜线和点作为date分隔符

 (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2} 

用它:

 function validate_Date($mydate,$format = 'DD-MM-YYYY') { if ($format == 'YYYY-MM-DD') list($year, $month, $day) = explode('-', $mydate); if ($format == 'YYYY/MM/DD') list($year, $month, $day) = explode('/', $mydate); if ($format == 'YYYY.MM.DD') list($year, $month, $day) = explode('.', $mydate); if ($format == 'DD-MM-YYYY') list($day, $month, $year) = explode('-', $mydate); if ($format == 'DD/MM/YYYY') list($day, $month, $year) = explode('/', $mydate); if ($format == 'DD.MM.YYYY') list($day, $month, $year) = explode('.', $mydate); if ($format == 'MM-DD-YYYY') list($month, $day, $year) = explode('-', $mydate); if ($format == 'MM/DD/YYYY') list($month, $day, $year) = explode('/', $mydate); if ($format == 'MM.DD.YYYY') list($month, $day, $year) = explode('.', $mydate); if (is_numeric($year) && is_numeric($month) && is_numeric($day)) return checkdate($month,$day,$year); return false; } 

我知道这是一个较旧的post,但我已经开发了以下函数来validationdate:

 function IsDateTime($aDateTime) { try { $fTime = new DateTime($aDateTime); $fTime->format('m/d/YH:i:s'); return true; } catch (Exception $e) { return false; } } 

不知道这是否回答这个问题,或去帮助….

 $dt = '6/26/1970' ; // or // '6.26.1970' ; $dt = preg_replace("([.]+)", "/", $dt); $test_arr = explode('/', $dt); if (checkdate($test_arr[0], $test_arr[1], $test_arr[2]) && preg_match("/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/", $dt)) { echo(date('Ym-d', strtotime("$dt")) . "<br>"); } else { echo "no good...format must be in mm/dd/yyyy"; } 

我们可以使用简单的“date”inputtypes,如下所示:

 Birth date: <input type="date" name="userBirthDate" /><br /> 

然后我们可以将DateTime接口与内置函数'explode'链接起来:

 public function validateDate() { $validateFlag = true; $convertBirthDate = DateTime::createFromFormat('Ym-d', $this->birthDate); $birthDateErrors = DateTime::getLastErrors(); if ($birthDateErrors['warning_count'] + $birthDateErrors['error_count'] > 0) { $_SESSION['wrongDateFormat'] = "The date format is wrong."; } else { $testBirthDate = explode('-', $this->birthDate); if ($testBirthDate[0] < 1900) { $validateFlag = false; $_SESSION['wrongDateYear'] = "We suspect that you did not born before XX century."; } } return $validateFlag; } 

我在谷歌浏览器和IE浏览器上testing过,一切正常。 此外,Chrome显示简单的附加界面。 如果你在input中没有写入任何东西,或者写入的格式不正确(正确的是:'1919-12-23'),你会得到第一个语句。 如果你把所有的东西都写得很好,但是你输错了date(我认为没有人能在二十世纪之前出生),你的控制者会发出第二个陈述。

尝试这个

 /^(19[0-9]{2}|2[0-9]{3})\-(0[1-9]|1[0-2])\-(0[1-9]|1[0-9]|2[0-9]|3[0-1])((T|\s)(0[0-9]{1}|1[0-9]{1}|2[0-3]{1})\:(0[0-9]{1}|1[0-9]{1}|2[0-9]{1}|3[0-9]{1}|4[0-9]{1}|5[0-9]{1})\:(0[0-9]{1}|1[0-9]{1}|2[0-9]{1}|3[0-9]{1}|4[0-9]{1}|5[0-9]{1})((\+|\.)[\d+]{4,8})?)?$/ 

这个正则expression式适用于:

  • 2017-01-01T00:00:00 + 0000
  • 2017-01-01 00:00:00 + 00:00
  • 2017-01-01T00:00:00 + 00:00
  • 2017-01-01 00:00:00 + 0000
  • 2017年1月1日

请记住,这将覆盖( )字符的date和date时间的所有情况