Calculate the number of months between two dates in PHP?

前端 未结 12 1745
无人及你
无人及你 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:07

    my function to resolve issue

    function diffMonth($from, $to) {
    
            $fromYear = date("Y", strtotime($from));
            $fromMonth = date("m", strtotime($from));
            $toYear = date("Y", strtotime($to));
            $toMonth = date("m", strtotime($to));
            if ($fromYear == $toYear) {
                return ($toMonth-$fromMonth)+1;
            } else {
                return (12-$fromMonth)+1+$toMonth;
            }
    
        }
    

提交回复
热议问题