Hướng dẫn count month between two dates php - đếm tháng giữa hai ngày php

Gần đây tôi cần tính tuổi trong các tháng từ trước khi sinh đến 5 tuổi (hơn 60 tháng).

Cả hai câu trả lời ở trên đều làm việc cho tôi. Cái đầu tiên tôi thử, về cơ bản là 1 liner cho câu trả lời của Deceze

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

Điều này thất bại với ngày tập hợp, rõ ràng câu trả lời phải là 0 vì mốc tháng (2011-12-04) chưa đạt được, bao nhiêu mã trả về 1.

Phương pháp thứ hai tôi đã thử, sử dụng mã của Adam

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .

Điều này thất bại và nói 0 tháng, khi nó phải là 1.

Những gì đã làm việc cho tôi, là một chút mở rộng của mã này. Những gì tôi đã sử dụng là như sau:

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

$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--;
        }
    }
}
04
$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--;
        }
    }
}
05
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
95
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
5
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
88
$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--;
        }
    }
}
09

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo hướng dẫn PHP và các ví dụ PHP này.

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14

Nếu bạn không muốn bao gồm dấu hiệu tiêu cực cho các khoảng thời gian âm, bạn có thể thực hiện một trong những điều sau đây:

  • Chỉ cần xóa định dạng định dạng
    $bdate = strtotime('2011-01-03'); 
    $edate = strtotime('2011-02-03');
    $age = 0;
    
    while (strtotime('+1 MONTH', $bdate) < $edate) {
        $age++;
        $bdate = strtotime('+1 MONTH', $bdate);
    }
    . . .
    
    5 khi truy xuất năm và tháng, hoặc;
  • Truyền
    $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 làm đối số thứ hai cho phương thức
    $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--;
            }
        }
    }
    
    1 để luôn trả về một khoảng thời gian tuyệt đối / dương.

Thay vì sử dụng phương thức

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
2, bạn cũng có thể truy cập trực tiếp các giá trị năm và tháng bằng các thuộc tính
$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--;
        }
    }
}
3 và
$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--;
        }
    }
}
4 trên đối tượng
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
0. Tương tự, để kiểm tra khoảng thời gian âm, bạn có thể truy cập thuộc tính
$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--;
        }
    }
}
6 (bằng
$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--;
        }
    }
}
7 nếu khoảng thời gian biểu thị khoảng thời gian âm và
$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--;
        }
    }
}
8 nếu không).


Hy vọng bạn tìm thấy bài viết này hữu ích. Nó đã được xuất bản ngày 02 tháng 8 năm 2021. Vui lòng thể hiện tình yêu và sự hỗ trợ của bạn bằng cách chia sẻ bài đăng này.

Trong bài viết này, chúng ta sẽ thấy cách tính chênh lệch giữa 2 ngày trong PHP, cùng với việc hiểu được việc thực hiện của nó thông qua các ví dụ. Cho hai ngày tức là., Start_date và end_date & chúng ta cần tìm sự khác biệt giữa hai ngày.

Xem xét ví dụ dưới đây:

Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.

Phương pháp 1: Sử dụng hàm Date_Diff () Using date_diff() Function

Hàm này được sử dụng để tìm sự khác biệt giữa hai ngày. Hàm này sẽ trả về một đối tượng DateInterval về thành công và trả về sai khi thất bại.

Ví dụ: Ví dụ này minh họa việc sử dụng hàm date_diff () để tính chênh lệch giữa 2 ngày.: This example illustrates the use of the date_diff() function to calculate the difference between the 2 dates.

PHP

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

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
3
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
6
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
8
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
6
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
8
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
1
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
0
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
3

Output:

+2 years 3 months

Phương pháp 2: Sử dụng công thức toán học ngày thời gian để tìm sự khác biệt giữa hai ngày. Nó trả lại các năm, tháng, ngày, giờ, phút, giây giữa hai ngày được chỉ định. 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.

Ví dụ: Trong ví dụ này, chúng tôi sẽ sử dụng công thức toán học ngày thời gian để tính toán sự khác biệt giữa các ngày sẽ được trả lại trong nhiều năm, tháng, ngày, giờ, phút, giây.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

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

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
3
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
6
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
8
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
6
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
8
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
1
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
0
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

Phương pháp 2: Sử dụng công thức toán học ngày thời gian để tìm sự khác biệt giữa hai ngày. Nó trả lại các năm, tháng, ngày, giờ, phút, giây giữa hai ngày được chỉ định.

DateTime4DateTime5

Ví dụ: Trong ví dụ này, chúng tôi sẽ sử dụng công thức toán học ngày thời gian để tính toán sự khác biệt giữa các ngày sẽ được trả lại trong nhiều năm, tháng, ngày, giờ, phút, giây.

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
05
Difference between two dates: 103
6
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
07

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
6
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
7
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
8
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
9
+2 years 3 months
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
17
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
5
Difference between two dates: 103
6
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

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

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
22

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

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

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
24

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
+2 years 3 months
3
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
7
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
8
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
9
+2 years 3 months
7
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0____70
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
7
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
2
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
9
+2 years 3 months
3
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
5
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
6
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
40
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
5
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

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

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
43

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0____79
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
7
Difference between two dates: 103
1
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
9
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
0
Difference between two dates: 103
4

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
34
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
5
Difference between two dates: 103
6
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

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

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
39

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
59
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
5
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

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

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

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

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
64

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

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

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
67

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

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

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

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
70
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
9
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
Difference between two dates: 103
6
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
75

$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
05DateTime7
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
09
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
26
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
$bdate = strtotime('2011-01-03'); 
$edate = strtotime('2011-02-03');
$age = 0;

while (strtotime('+1 MONTH', $bdate) < $edate) {
    $age++;
    $bdate = strtotime('+1 MONTH', $bdate);
}
. . .
45
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
3

Output:

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

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
Difference between two dates: 103
6
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
7
Difference between two dates: 103
1
Difference between two dates: 103
9__
This method is used to get the total number of days between two specified dates.

PHP

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

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
3
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
6
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
8
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
2
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
4
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
6
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
0
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
8
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $end->diff($start);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // -14
1
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
0
Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
1
$start = new DateTime('2020-01-01 00:00:00');
$end = new DateTime('2021-03-15 00:00:00');
$diff = $start->diff($end);

$yearsInMonths = $diff->format('%r%y') * 12;
$months = $diff->format('%r%m');
$totalMonths = $yearsInMonths + $months;

echo $totalMonths; // 14
4

Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.
3

Output:

Difference between two dates: 103

Phương pháp 2: Sử dụng công thức toán học ngày thời gian để tìm sự khác biệt giữa hai ngày. Nó trả lại các năm, tháng, ngày, giờ, phút, giây giữa hai ngày được chỉ định.


Làm thế nào tôi có thể tính số tháng giữa hai ngày trong PHP?

PHP $ sdate = "1981-11-04"; $ edate = "2013-09-04"; $ date_diff = abs (strtotime ($ edate) - strtotime ($ sdate)); $ năm = sàn ($ date_diff / (365*60*60*24)); $ tháng = sàn (($ date_diff - $ năm*365*60*60*24) / (30*60*60*24)); $ days = FLOOR (($ date_diff - $ năm*365*60*60*24 - $ tháng*30*60*60*24)/ (60*60*24)); printf ("%d ...$months = floor(($date_diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($date_diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); printf("%d ...

Làm thế nào tôi có thể có được sự khác biệt giữa hai ngày trong PHP?

Làm thế nào để tính toán sự khác biệt giữa hai ngày trong PHP?.
PHP |hàm date_diff () ..
Chương trình tìm số ngày giữa hai ngày trong PHP ..
Trả về tất cả các ngày giữa hai ngày trong một mảng trong PHP ..
PHP |fopen () (chức năng mở tệp hoặc url).
PHP |chức năng fread () ..
PHP |hàm fclose () ..
PHP |hàm fwrite () ..

Làm thế nào để có được số ngày trong một tháng trong PHP?

Hàm cal_days_in_month () trả về số ngày trong một tháng cho một năm và lịch được chỉ định.cal_days_in_month() function returns the number of days in a month for a specified year and calendar.

Làm cách nào để tính số tuần giữa hai ngày trong PHP?

Hàm php tuần_between_two_dates ($ date1, $ date2) {$ first = datetime :: createFromFormat ('m/d/y', $ date1);$ Thứ hai = DateTime :: createdFromFormat ('m/d/y', $ date2);if ($ date1> $ date2) return week_between_two_dates ($ date2, $ date1);Tầng trả lại ($ đầu tiên-> diff ($ giây)-> ngày/7);} $ dt1 = '1/1/2014';$ dt2 = '12/31/2014 ';...