Calculate the number of months between two dates in PHP?

前端 未结 12 1742
无人及你
无人及你 2020-12-01 05:12

Without using PHP 5.3\'s date_diff function (I\'m using PHP 5.2.17), is there a simple and accurate way to do this? I am thinking of something like the code below, but I don

12条回答
  •  遥遥无期
    2020-12-01 06:12

    Here is my solution. It's only checks years and monthes of dates. So, if one date is '31.10.15' and other is '02.11.15' it returns 1 month.

    function get_interval_in_month($from, $to) {
        $month_in_year = 12;
        $date_from = getdate(strtotime($from));
        $date_to = getdate(strtotime($to));
        return ($date_to['year'] - $date_from['year']) * $month_in_year -
            ($month_in_year - $date_to['mon']) +
            ($month_in_year - $date_from['mon']);
    }
    

提交回复
热议问题