How to get month between two dates php

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--;
        }
    }
}

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

$StartDate = @strtotime["Jan 2003"];
$StopDate = @strtotime["Apr 2004"];
/**
* Gets list of months between two dates
* @param int $start Unix timestamp
* @param int $end Unix timestamp
* @return array
*/
function echoDate[ $start, $end ]{
$current = $start;
$ret = array[];
while[ $current

Output:

+2 years 3 months

Method 2: To use the date-time mathematical formula to find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between two specified dates.

Example: In this example, we will be using the date-time mathematical formula to calculate the difference between the dates that will be returned in years, months, days, hours, minutes, seconds.

PHP

Output:

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds

Method 3: This method is used to get the total number of days between two specified dates.

PHP

Output:

Difference between two dates: 103

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How can I get the number of months between two dates in php?

“find how many months between two dates php” Code Answer.
$d1 = new DateTime["2018-01-10 00:00:00"];.
$d2 = new DateTime["2019-05-18 01:23:45"];.
$interval = $d1->diff[$d2];.
$diffInSeconds = $interval->s; //45..
$diffInMinutes = $interval->i; //23..
$diffInHours = $interval->h; //8..
$diffInDays = $interval->d; //21..

How can I get days between two dates in php?

The date_diff[] function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.

How get number of days in a month in php?

The cal_days_in_month[] function returns the number of days in a month for a specified year and calendar.

How do I calculate the number of weeks between two dates in php?

php function week_between_two_dates[$date1, $date2] { $first = DateTime::createFromFormat['m/d/Y', $date1]; $second = DateTime::createFromFormat['m/d/Y', $date2]; if[$date1 > $date2] return week_between_two_dates[$date2, $date1]; return floor[$first->diff[$second]->days/7]; } $dt1 = '1/1/2014'; $dt2 = '12/31/2014'; ...

Chủ Đề