sasori Posted July 29, 2013 Share Posted July 29, 2013 (edited) i have 2 input forms, the "Date From" and "Date To" , both are using jquery's date picker My question is, how to limit the user to select only a range of one month using those two datepickers ? this is the snippet of the "Date From" date picker $this->widget('zii.widgets.jui.CJuiDatePicker', array( 'model' => $model, 'attribute' => 'STARTDATE', 'options' => array( 'dateFormat'=>'yy-mm-dd', 'showOn'=> 'both', 'buttonImage'=> Yii::app()->theme->baseUrl."/images/calendar.gif", 'buttonImageOnly' => 'true', 'dateFormat'=>'dd-mm-yy', 'changeMonth' => 'true', 'changeYear' => 'true', 'showButtonPanel' => 'true', 'constrainInput' => 'false', 'duration'=>'fast', 'showAnim' =>'slide', 'ampm' => 'true', 'onSelect' => 'js:function(selectedDate) {$( "#paymenttrans_TRANSDATETO" ).datepicker( "option", "minDate", selectedDate );}' ), 'flat'=>false, 'htmlOptions'=>array( 'readonly'=>'TRUE', 'size'=>'10', 'style'=>'margin-right: 5px;' ) ) ); the objective is like this, let's say the user selects date from January 1 to February 1, that's it,OR let's say the user selects January 5 to February 5.. if the user tries to select that has a range of more than 1 month , it should be prevented...how to do that? I have the idea of subtraction, but how? , what's the formula for detecting "if" the selected date range from the two datepicker is already exact 1 month ? Edited July 29, 2013 by sasori Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 29, 2013 Share Posted July 29, 2013 If the user is limited to only selecting a month, then why have two date pickers? Or, are you saying the user can select a range "up to" one month, but not more? If the user can only select a single month, then only give them a start date and programatically determine the month period for them. If the user can select a range "up to" one month, then you can determine if they exceeded a month using strtotime() with the "+1 month" modifier (you would use the same thing if you go with the single date picker). A few comments: 1) Although you definitely want to add validation server-side, you should do the same client side. Your date picker should have the ability to limit the available dates. So you can update t he second datepicker when a user makes a selection from the first. You would also need to handle when a user changes the first date picker to update the second. 2) You need to determine what to do about selections at the end of the month. For example, if a user selects January 31st and the first date, what is the max for the second date? Feb 28/29th? You will likely need to add specific logic to handle these situations. Quote Link to comment Share on other sites More sharing options...
Solution sasori Posted July 30, 2013 Author Solution Share Posted July 30, 2013 I've solved this problem by using the DateTime::diff and I just threw an error message if the user selected a range that is more than a month worth of days Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.