duanchuang1935 2012-08-24 04:26
浏览 192
已采纳

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

I have model with rules

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}".'
                ),
            );
    }

I want to compare date and time...
In case: 'return_date' >= 'start_date'... it's ok.
In case 'return_date' == 'start_date', so 'return_hours > 'start_hours' but with rules above I can't compare them.
What's solution? thanks!

  • 写回答

1条回答 默认 最新

  • dpo69086 2012-08-24 08:35
    关注

    You can achieve this using a custom validator.

    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));                
                    }
                }
            }
    

    And in your rules

    public function rules() {
    return array(
     //your previous rules 
         array('return_date','comparedates'),
        );
    

    }

    Remember change the the patternparameter of CDateTimeParser::parse() from hh according to the format of your return_hours and start_hours

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP