计算今天想前向后推几个月后的算法,可以跨年

        /**
         *  计算下个月的今日
         *  @ $time 当前的时间戳
         *  @ $nums 退后或前进的月数(正数表示向后  负数表示向前)
         * **/
        function next_month_today($time , $nums="1"){ 
                //本月的时间戳
                $last_month = date("Y-m-d",$time);
                $last_month_data = explode("-", $last_month);
               $next_month_time = mktime(date("G", $time), date("i", $time),date("s", $time), $last_month_data['1']+1+$nums, 0, date("Y", $time));
                $last_month_t =  date("t", $next_month_time);
                if ($last_month_t < date("j", $time))   //如果下个月的天数小于当前的日期值(例如28号小于30号)则返回下个月的月末
                {
                      return date("Y-m-t", $next_month_time);
                }
                        return date(date("Y-m", $next_month_time) . "-d", $time);
                  
          }

 

posted @ 2014-04-16 21:53  scrit  阅读(351)  评论(0编辑  收藏  举报