计算两个时间相差几个月

//计算相差几个月
    public function get_month_diff($start,$end)
    {
        $start = date('Y-m-d',$start);
        $end = date('Y-m-d',$end);
        $new_start = new \DateTime($start);
        $new_end = new \DateTime($end);
        $interval = \DateInterval::createFromDateString('1 month');
        $period = new \DatePeriod($new_start, $interval, $new_end);
        $month = [];
        foreach ($period as $dt) {
            $month[] = $dt->format("Y-m");
        }
        return count($month);
    }

  

posted @ 2020-09-15 10:51  hjr_rong  阅读(274)  评论(0编辑  收藏  举报