dongxia527680 2014-09-03 13:47
浏览 33

PHP有效的DOB输入/不准确的日期比较

I am using Yii framework, set some rules for model validation when data is being modified. One of them which is to compare the valid DOB input. Criteria would be DOB should not be

  1. Greater than today
  2. Between 100 years as from today.

This is how I implement the rule:

/**
 * Checks the valid input for DOB
 *
 * @return boolean
 * @throws Error Message
 */
public function validDOB($attribute,$params){
    if (($bdate = $this->bdate) != null){               
        $now = new DateTime("now");
        $dob = DateTime::createFromFormat('d/m/Y', $bdate);
        $interval = $now->diff($dob);

        $curdate=strtotime($now->format('d/m/Y'));
        $largedate=strtotime($dob->format('d/m/Y'));

        if ($interval->y > 100){
            $this->addError('bdate', 'Wow! Are you sure you are ' . $interval->y .' years old?');
        }elseif ($curdate > $largedate){
            $this->addError('bdate', 'Psst.. Hey... You are not a time traveller...');
        }
    }
}

So the problem is, the validator suppose to valid and throws me error when an invalid date is selected, but, say that today's date is 03/09/2014 (d/m/Y) and the selected date is 04/09/2014 (d/m/Y) suppose the system should prompt the error message but instead it returns as a valid input. I would have to select dates until about 13/09/2014 (d/m/Y) then only the system shows invalid input...

Date greater than today

Same goes to year selection. Suppose that date is 13/09/2014 (d/m/Y), so between 100 years which means that if a user selects anything before 13/09/1914 (d/m/Y) the system would prompt invalid... But it also causes a bug like above..

Additionally I also realized that when I use strtotime() to compare future dates, it somehow causes those years between 100 year became invalid as well...

What have I done wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?