Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:41:43+00:00 2026-06-10T04:41:43+00:00

I have model with rules public function rules() { return array( array(‘user_id, name, start_date,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:41:45+00:00Added an answer on June 10, 2026 at 4:41 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a validation rules in my model: public function rules() { return array(){
I have a model with validation rule like this public function rules() { return
Let's say I have an ActiveRecord with the following validation rules: public function rules()
I have model public class UploadOptionModel { public UploadOptionModel() { OutputFormat = outputFormatList.Select(i =>
I have model: [XmlRoot(ElementName = event, IsNullable=true)] public class Event { public int id
I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
In my controller I have model operations that can return empty results. I've setup
I have a model with a number of validation rules, all of which are
I have a model that extends CActiveRecord that has the following rules() method: /**
I have an Xtext grammar that does something like this: Model: (names += Name)*

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.