Calculate the number of months between two dates in PHP?

前端 未结 12 1715
无人及你
无人及你 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 05:47

    Like this:

    $date1 = strtotime('2000-01-25');
    $date2 = strtotime('2010-02-20');
    $months = 0;
    
    while (($date1 = strtotime('+1 MONTH', $date1)) <= $date2)
        $months++;
    
    echo $months;
    

    If you want to include days to, then use this:

    $date1 = strtotime('2000-01-25');
    $date2 = strtotime('2010-02-20');
    
    $months = 0;
    
    while (strtotime('+1 MONTH', $date1) < $date2) {
        $months++;
        $date1 = strtotime('+1 MONTH', $date1);
    }
    
    echo $months, ' month, ', ($date2 - $date1) / (60*60*24), ' days'; // 120 month, 26 days
    
    0 讨论(0)
  • 2020-12-01 05:54

    Here is my solution. It checks both years and months of dates and find difference.

     $date1 = '2000-01-25';
     $date2 = '2010-02-20';
     $d1=new DateTime($date2); 
     $d2=new DateTime($date1);                                  
     $Months = $d2->diff($d1); 
     $howeverManyMonths = (($Months->y) * 12) + ($Months->m);
    
    0 讨论(0)
  • 2020-12-01 05:54

    I recently needed to calculate age in months ranging from prenatal to 5 years old (60+ months).

    Neither of the answers above worked for me. The first one I tried, which is basically a 1 liner for deceze's answer

    $bdate = strtotime('2011-11-04'); 
    $edate = strtotime('2011-12-03');
    $age = ((date('Y',$edate) - date('Y',$bdate)) * 12) + (date('m',$edate) - date('m',$bdate));
    . . .
    

    This fails with the set dates, obviously the answer should be 0 as the month mark (2011-12-04) hasn't been reached yet, how ever the code returns 1.

    The second method I tried, using Adam's code

    $bdate = strtotime('2011-01-03'); 
    $edate = strtotime('2011-02-03');
    $age = 0;
    
    while (strtotime('+1 MONTH', $bdate) < $edate) {
        $age++;
        $bdate = strtotime('+1 MONTH', $bdate);
    }
    . . .
    

    This fails and says 0 months, when it should be 1.

    What did work for me, is a little expansion of this code. What I used is the following:

    $bdate = strtotime('2011-11-04');
    $edate = strtotime('2012-01-04');
    $age = 0;
    
    if($edate < $bdate) {
        //prenatal
        $age = -1;
    } else {
        //born, count months.
        while($bdate < $edate) {
            $age++;
            $bdate = strtotime('+1 MONTH', $bdate);
            if ($bdate > $edate) {
                $age--;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 05:55

    How about this:

    $d1 = new DateTime("2009-09-01");
    $d2 = new DateTime("2010-09-01");
    $months = 0;
    
    $d1->add(new \DateInterval('P1M'));
    while ($d1 <= $d2){
        $months ++;
        $d1->add(new \DateInterval('P1M'));
    }
    
    print_r($months);
    
    0 讨论(0)
  • 2020-12-01 05:56
    $date1 = '2000-01-25';
    $date2 = '2010-02-20';
    
    $ts1 = strtotime($date1);
    $ts2 = strtotime($date2);
    
    $year1 = date('Y', $ts1);
    $year2 = date('Y', $ts2);
    
    $month1 = date('m', $ts1);
    $month2 = date('m', $ts2);
    
    $diff = (($year2 - $year1) * 12) + ($month2 - $month1);
    

    You may want to include the days somewhere too, depending on whether you mean whole months or not. Hope you get the idea though.

    0 讨论(0)
  • 2020-12-01 06:03

    This is how I ended up solving it. I know I'm a little late but I hope this saves someone a lot time and lines of code.
    I used DateInterval::format to display a human readable countdown clock in years, months, and days. Check https://www.php.net/manual/en/dateinterval.format.php for the format table to see your options on how to modify your return values. Should give you what you're looking for.

    $origin = new DateTime('2020-10-01');
    $target = new DateTime('2020-12-25');
    $interval = $origin->diff($target);
    echo $interval->format('%y years, %m month, %d days until Christmas.');
    

    Outputs: 0 years, 2 month, 24 days

    0 讨论(0)
提交回复
热议问题