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 seconds5
$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
5 khi truy xuất năm và tháng, hoặ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]; } . . .
- Truyền
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.$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--; } } }
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; // 140
$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; // 141
$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; // 142
$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; // 143
$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; // 144
$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; // 140
$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; // 146
$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; // 142
$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; // 148
$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; // 144
$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; // 140
$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; // -141
$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; // -142
$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; // 141
$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; // -144
$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; // 146
$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; // 144
$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; // 140
$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; // -148
$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; // -141
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; // 144
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; // 140
$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; // 141
$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; // 142
$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; // 143
$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; // 144
$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; // 140
$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; // 146
$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; // 142
$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; // 148
$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; // 144
$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; // 140
$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; // -141
$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; // -142
$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; // 141
$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; // -144
$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; // 146
$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; // 144
$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; // 140
$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; // -148
$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; // -141
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; // 144
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.
DateTime
4DateTime
5
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];
}
. . .
05Difference between two dates: 1036
$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; // 140
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 months0
$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; // 144
$bdate = strtotime['2011-01-03'];
$edate = strtotime['2011-02-03'];
$age = 0;
while [strtotime['+1 MONTH', $bdate] < $edate] {
$age++;
$bdate = strtotime['+1 MONTH', $bdate];
}
. . .
172 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds5
Difference between two dates: 1036
$bdate = strtotime['2011-01-03'];
$edate = strtotime['2011-02-03'];
$age = 0;
while [strtotime['+1 MONTH', $bdate] < $edate] {
$age++;
$bdate = strtotime['+1 MONTH', $bdate];
}
. . .
20DateTime
7$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; // 140
+2 years 3 months3
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 months7
$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; // 144
$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; // 140____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 seconds2
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 months3
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds5
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; // 144
$bdate = strtotime['2011-01-03'];
$edate = strtotime['2011-02-03'];
$age = 0;
while [strtotime['+1 MONTH', $bdate] < $edate] {
$age++;
$bdate = strtotime['+1 MONTH', $bdate];
}
. . .
402 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds5
$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; // 140____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: 1031
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 seconds0
Difference between two dates: 1034
$bdate = strtotime['2011-01-03'];
$edate = strtotime['2011-02-03'];
$age = 0;
while [strtotime['+1 MONTH', $bdate] < $edate] {
$age++;
$bdate = strtotime['+1 MONTH', $bdate];
}
. . .
342 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds5
Difference between two dates: 1036
$bdate = strtotime['2011-01-03'];
$edate = strtotime['2011-02-03'];
$age = 0;
while [strtotime['+1 MONTH', $bdate] < $edate] {
$age++;
$bdate = strtotime['+1 MONTH', $bdate];
}
. . .
20DateTime
7$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];
}
. . .
592 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds5
$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; // 140
$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; // -144
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds9
$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; // -144
Difference between two dates: 1036
$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];
}
. . .
05DateTime
7$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; // -144
$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; // -144
$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; // -144
$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; // 144
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
0Difference 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.
7Difference between two dates: 103
1Difference 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; // 140
$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; // 141
$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; // 142
$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; // 143
$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; // 144
$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; // 140
$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; // 146
$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; // 142
$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; // 148
$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; // 144
$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; // 140
$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; // -141
$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; // -142
$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; // 141
$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; // -144
$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; // 146
$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; // 144
$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; // 140
$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; // -148
$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; // -141
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; // 144
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.