how to compare two dates

if i am using this in my model rules

[[‘end_date’], ‘compare’, ‘compareAttribute’=>‘start_date’, ‘operator’=>’>=’, ‘skipOnEmpty’=>true, ‘message’=>’{attribute} must be greater than {compareValue} '],

then it is only comparing with date not with month or year

if start_date is 01-05-2014 and end_date is 01-05-2015, it is giving error end_date must be greater than start_date

the i changed to ajax validation ,write my own function

public function validateDate($attribute, $params) {


  st= strtotime( $this->start_date);


  $et=strtotime( $this->end_date);


  if($et<$st){


    $this->addError($attribute, Yii::t('app', 'end date must be greater than start date'));


  }


}

with ajax it is working but i cant use ajax because i submitting my form with method get,so on get method ajax validation is not working.

You can’t compare dates directly with the compare validater, you can only do integers and strings per the docs.

The validation should happen before you submit the form (is what your wanting), therefore the form submit type (i.e. get/post) would not be your issue.

You need to set up your controller and form to handle the ajax validation for the custom validater.

Ajax validation doc.

Also, as a side note you might need to check you function because you are allowing the start and end date to be the same date and time however, this could be allowed in your situation if note, then change

[code]

$et<$st to $et<=$st

/code]

so at least this way the end date would have to be at least a second later (or day if you don’t do time) than the start date.

i have written ajax validation everything is working fine,but if form i m submitting is not submit by get method ,it works ,it call validation ,but if i m using get then whole form is calling in ajax validation

You should not be submitting or changing any data using GET. This breaks the HTTP model including the ability of caches to cache the responses to GET requests. "In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval"

GET is for getting, POST is for updating.

If you do not follow this, you will find all kinds of ways in which you will have to fight not just Yii but other parts of the web infrastructure too.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1

It looks like this is a validater for a search so it should be get…the ajax validation should still work.