Jump to content

Archived

This topic is now archived and is closed to further replies.

sasori

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

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 ?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

×
×
  • 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.