Jump to content

how to know if the chosen date range is exactly 1 month


sasori
Go to solution Solved by sasori,

Recommended Posts

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 by sasori
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.