1

好的,这就是我做了一个 PHP 日历类的交易,如此处所示http://pastebin.com/QERgmn8y我还有一个 ajax 脚本,如果满足某些条件,它将切换日历日期和年份,如此处所示http: //pastebin.com/kfrmz79J

并且由于我还没有足够的 repotation 这里是 PasteBin 上最终脚本的 ID id 是JKdi6H8f这个脚本使这一切发生

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

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

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

和许多其他条件,但到目前为止这些条件似乎效果最好,唯一的问题是,当我将日期切换到 12 月然后返回时,它会上升一年,或者如果我到 12 月然后切换到 1 月并返回到 12 月年份将保持在 2014 年,而不是回到 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();  
        }
4

0 回答 0