Php yii比较模型中的日期和时间

Php yii比较模型中的日期和时间,php,yii,Php,Yii,我有一个有规则的模型 public function rules() { return array( array('user_id, name, start_date, timezones, start_hours, return_date, return_hours, location_start, location_end, trip_activity, trip_mean, status', 'required'), arra

我有一个有规则的模型

public function rules() {
        return array(
            array('user_id, name, start_date, timezones, start_hours, return_date, return_hours, location_start, location_end, trip_activity, trip_mean, status', 'required'),
            array('user_id, return_hours, sign_off, saved, advise, time_notification, status', 'numerical', 'integerOnly'=>true),
            array('name, going_with, start_hours, location_start, location_end, location_return, trip_activity, trip_mean', 'length', 'max'=>255),
            array('created, modified, advise, notify_best_friend, notify_now, notify_returning, send_notification, sign_off', 'safe'),
            array('going_with, location_return, time_notification, created, modified', 'default', 'setOnEmpty' => true, 'value' => null),
            array('id, user_id, name, going_with, start_date, start_hours, return_date, return_hours, location_start, location_end, location_return, trip_activity, trip_mean, saved, advise, time_notification, status, created, modified', 'safe', 'on'=>'search'),
            array(
                  'return_date',
                  'compare',
                  'compareAttribute'=>'start_date',
                  'operator'=>'>=', 
                  'allowEmpty'=>false , 
                  'message'=>'{attribute} must be greater than "{compareValue}".'
                ),

            array(
                  'return_hours',
                  'compare',
                  'compareAttribute'=>'start_hours',
                  'operator'=>'>', 
                  'allowEmpty'=>false , 
                  'message'=>'{attribute} must be greater than "{compareValue}".'
                ),
            );
    }
我想比较一下日期和时间。。。 如果“return\u date”>=“start\u date”。。。没关系。 如果“return\u date”==“start\u date”,那么“return\u hours>start\u hours”,但是根据上面的规则,我无法比较它们。
什么是解决方案?谢谢

您可以使用自定义验证器来实现这一点

public function comparedates($attribute,$params)
        {
            $message=Yii::t('yii','{attribute} must be greater than "{compareValue}".');
            if(CDateTimeParser::parse($this->return_date, "yyyy-MM-dd") < CDateTimeParser::parse($this->start_date, "yyyy-MM-dd"))
            {
                $params=array('{attribute}'=>$this->getAttributeLabel('return_date'),'{compareValue}'=>$this->getAttributeLabel('start_date'));
                $this->addError($attribute,strtr($message,$params));
            }
            else if(CDateTimeParser::parse($this->return_date, "yyyy-MM-dd") === CDateTimeParser::parse($this->start_date, "yyyy-MM-dd"))
            {
                if(CDateTimeParser::parse($this->return_hours, "hh") < CDateTimeParser::parse($this->start_hours, "hh")//change hh according to the format of return_hours and start_hours
                {   
                    $params=array('{attribute}'=>$this->getAttributeLabel('return_hours'),'{compareValue}'=>$this->getAttributeLabel('start_hours'));
                    $this->addError('return_hours',strtr($message,$params));                
                }
            }
        }
}


请记住,根据返回时间和开始时间的格式更改from hh的pattern参数。您可以使用自定义验证器实现这一点

public function comparedates($attribute,$params)
        {
            $message=Yii::t('yii','{attribute} must be greater than "{compareValue}".');
            if(CDateTimeParser::parse($this->return_date, "yyyy-MM-dd") < CDateTimeParser::parse($this->start_date, "yyyy-MM-dd"))
            {
                $params=array('{attribute}'=>$this->getAttributeLabel('return_date'),'{compareValue}'=>$this->getAttributeLabel('start_date'));
                $this->addError($attribute,strtr($message,$params));
            }
            else if(CDateTimeParser::parse($this->return_date, "yyyy-MM-dd") === CDateTimeParser::parse($this->start_date, "yyyy-MM-dd"))
            {
                if(CDateTimeParser::parse($this->return_hours, "hh") < CDateTimeParser::parse($this->start_hours, "hh")//change hh according to the format of return_hours and start_hours
                {   
                    $params=array('{attribute}'=>$this->getAttributeLabel('return_hours'),'{compareValue}'=>$this->getAttributeLabel('start_hours'));
                    $this->addError('return_hours',strtr($message,$params));                
                }
            }
        }
}


记住根据return\u hours和start\u hours的格式更改from hh的pattern参数

return\u date类型是什么?return\u date和start\u date类型是'date'。如果date是一个自定义类,因为PHP中没有此类类,那么我假设是您自己编写的,您描述了比较规则并编写了自己的Yii验证器。return\u date类型是什么?return\u date和start\u date类型是“date”。如果date是一个自定义类,因为PHP中没有此类类,所以我假设您是自己编写的,那么您描述了比较规则并编写了自己的Yii验证器。非常感谢您。。。这是工作,但有个问题。。。我不能使用“返回时间必须大于开始时间”的多语言…非常感谢。。。这是工作,但有个问题。。。我不能使用“返回时间必须大于开始时间”的多语言。。。