正则表达式生日日期验证来自;穆拉赫';s PHP“;
我想确保输入的生日不是在将来。如果我输入一个未来的日期,它会显示一条错误消息正则表达式生日日期验证来自;穆拉赫';s PHP“;,php,regex,Php,Regex,我想确保输入的生日不是在将来。如果我输入一个未来的日期,它会显示一条错误消息 public function birth($name, $value) { $datePattern = '/^[0-9]{2}\/[1-9]{2}\/[1-9]{4}?$/'; $match = preg_match($datePattern, $value); if ( $match === false ) { $field->setErrorMessage('Err
public function birth($name, $value) {
$datePattern = '/^[0-9]{2}\/[1-9]{2}\/[1-9]{4}?$/';
$match = preg_match($datePattern, $value);
if ( $match === false ) {
$field->setErrorMessage('Error testing field.');
return;
}
if ( $match != 1 ) {
$field->setErrorMessage('Invalid date format.');
return;
}
$dateParts = explode('/', $value);// i try to see if i could take everything after / to form an array
$month = $dateParts[0];
$day = $dateParts[1];
$year = $dateParts[2];
$dateString = $month . $day . $year;
$exp = new \DateTime($dateString);
$now = new \DateTime();
if ( $exp < $now ) {
$field->setErrorMessage('Cant be a date after the current date');
return;
}
$field->clearErrorMessage();
}
公共函数出生($name,$value){
$datePattern='/^[0-9]{2}\/[1-9]{2}\/[1-9]{4}?$/';
$match=preg_match($datePattern,$value);
如果($match==false){
$field->setErrorMessage('Error testing field');
返回;
}
如果($match!=1){
$field->setErrorMessage('日期格式无效');
返回;
}
$dateParts=explode(“/”,$value);//我试着看看是否可以将/之后的所有内容组成一个数组
$month=$dateParts[0];
$day=$dateParts[1];
$year=$dateParts[2];
$dateString=$month.$day.$year;
$exp=new\DateTime($dateString);
$now=new\DateTime();
如果($exp<$now){
$field->setErrorMessage('不能是当前日期之后的日期');
返回;
}
$field->clearErrorMessage();
}
但是它不能正常工作。我无法将
$dateString
与DateTime
进行比较,您不需要正则表达式,只需将其与当前日期进行比较即可
public function birth($name, $value) {
$timestamp = strtotime($value);
if ($timestamp === FALSE) {
// $value is not a valid date
}
if ($timestamp > time()) {
// date is in the future
}
}
但是,如果您想保留当前代码,问题就出在这一行
$dateString = $month . $day . $year;
这不是有效的日期表示形式。今天将是572014
-您需要在两者之间添加/
,它将起作用
$dateString = sprintf("%u/%u/%u", $month, $day, $year);
你不需要正则表达式,只需将它与当前日期进行比较即可
public function birth($name, $value) {
$timestamp = strtotime($value);
if ($timestamp === FALSE) {
// $value is not a valid date
}
if ($timestamp > time()) {
// date is in the future
}
}
但是,如果您想保留当前代码,问题就出在这一行
$dateString = $month . $day . $year;
这不是有效的日期表示形式。今天将是572014
-您需要在两者之间添加/
,它将起作用
$dateString = sprintf("%u/%u/%u", $month, $day, $year);
你不需要正则表达式,只需将它与当前日期进行比较即可
public function birth($name, $value) {
$timestamp = strtotime($value);
if ($timestamp === FALSE) {
// $value is not a valid date
}
if ($timestamp > time()) {
// date is in the future
}
}
但是,如果您想保留当前代码,问题就出在这一行
$dateString = $month . $day . $year;
这不是有效的日期表示形式。今天将是572014
-您需要在两者之间添加/
,它将起作用
$dateString = sprintf("%u/%u/%u", $month, $day, $year);
你不需要正则表达式,只需将它与当前日期进行比较即可
public function birth($name, $value) {
$timestamp = strtotime($value);
if ($timestamp === FALSE) {
// $value is not a valid date
}
if ($timestamp > time()) {
// date is in the future
}
}
但是,如果您想保留当前代码,问题就出在这一行
$dateString = $month . $day . $year;
这不是有效的日期表示形式。今天将是572014
-您需要在两者之间添加/
,它将起作用
$dateString = sprintf("%u/%u/%u", $month, $day, $year);
正则表达式是为了确保它们输入正确的格式。。。它与输入正确的日期/未来日期无关正则表达式是确保输入正确的格式。。。它与输入正确的日期/未来日期无关正则表达式是确保输入正确的格式。。。它与输入正确的日期/未来日期无关正则表达式是确保输入正确的格式。。。这与输入正确的日期/未来日期无关