PHP日历类Ajax Calendar控件,日期切换为dec到jan并再次返回到dec

时间:2013-08-21 09:46:09

标签: php jquery ajax date calendar

好的,所以这里的交易我已经制作了一个PHP日历类,如此处http://pastebin.com/QERgmn8y我还有一个ajax脚本,如果满足某些条件,将切换日历日期和年份{{3 }}

因为我还没有足够的报告,所以这里是PasteBin上最终脚本的ID,ID是 JKdi6H8f 这个脚本让它全部发生

现在到目前为止我已尝试过多种条件,例如

if(prevMon > mon ){//set year to -1};

if(nextMon < mon){//set year to +1};

和许多其他条件,但那些似乎最好的工作到目前为止唯一的问题是,当我将日期切换到12月然后回去它上升一年,或如果我到12月然后切换到1月和返回到今年12月将是2014年,而不是回到过去。

如果有人能找到解决这个问题的方法,我会很高兴。

这里还有相关代码的片段

 //next month is clicked
        $(document).on('click',"#next_month", function(){
                var selected =$(this).attr('class');
                date.setMonth(selected-1);
                var mon = date.getMonth();
                var prevMon = date.getMonth()-1;
                if(prevMon < 0){
                        prevMon = 11;
                }
                var nextMon = date.getMonth()+1;
                if(nextMon > 11){
                        nextMon = 0;
                }
                var remain = 11 - mon;
                var currY = date.getFullYear();
                console.log(selected+"current m: "+mon+" prev m: "+prevMon+" next m: "+nextMon+" current y: "+currY+" remainder: "+remain);

                if(remain == 0){
                        date.setFullYear(currY+1);
                        console.log(currY);
                }
                $.ajax({
                        type: "POST",
                        url: "calendar/calendar.php",
                        data: {nMonth: selected, year: currY},
                        success: function(data){
                                var d = data.split("▼");
                                $("#cal_wrap").html(d[1]);
                                $("#cal_header_wrap").html(d[0]);
                        }
                });
        });

        //previous month is clicked
        $(document).on('click',"#prev_month", function(){
                var selected =$(this).attr('class');
                date.setMonth(selected-1);
                var mon = date.getMonth();
                var prevMon = date.getMonth()+1;
                if(prevMon > 11){
                        prevMon = 0;
                }
                var nextMon = date.getMonth()-1;
                if(nextMon < 0){
                        nextMon = 11;
                }
                var remain = 11 - mon;
                var currY = date.getFullYear();
                console.log(selected+"current m: "+mon+" prev m: "+prevMon+" next m: "+nextMon+" current y: "+currY+" remainder: "+remain);

                if(remain == 11){
                        date.setFullYear(currY-1);
                        console.log(currY);
                }
                $.ajax({
                        type: "POST",
                        url: "calendar/calendar.php",
                        data: {pMonth: selected, year: currY},
                        success: function(data){
                                var d = data.split("▼");
                                $("#cal_wrap").html(d[1]);
                                $("#cal_header_wrap").html(d[0]);
                        }
                });
        });

和PHP脚本

if(isset($nMonth)){
                //sets the year to what javascript returns
                $cal->setY($year);
                $nm = mktime(0,0,0,$nMonth,1,$cal->getY(),0);
                $month = date("m",$nm);
                $cal->setM($month);
                $cal->showCalHeader();
                echo "▼";
                $cal->showWeek();
                $cal->showCalendar();
        }else if(isset($pMonth)){
                //sets the year to what javascript returns
                $cal->setY($year);
                //sets the month to previous month
                $pm = mktime(0,0,0,$pMonth,1,2013,0);
                $month = date("m",$pm);
                $cal->setM($month);
                $cal->showCalHeader();
                echo "▼";
                $cal->showWeek();
                $cal->showCalendar();  
        }

0 个答案:

没有答案