Laravel tính chênh lệch thời gian giữa hai ngày tính bằng giờ và phút

Tính toán sự khác biệt giữa hai ngày không phải là dễ dàng như nó có vẻ. Có rất nhiều thứ có thể sai nếu bạn không cẩn thận. Ví dụ: bạn phải xem xét cả năm nhuận và giờ tiết kiệm ánh sáng ban ngày. Chỉ cần chuyển đổi hai ngày thành dấu thời gian và trừ đi các giá trị là không đáng tin cậy trong mọi trường hợp

May mắn thay, lớp DateTime của PHP đảm nhận tất cả những điều này cho chúng ta. Trong hướng dẫn này, chúng tôi sẽ sử dụng các phương thức khác nhau từ lớp này để lấy sự khác biệt giữa hai ngày

Trên trang này

Nhận sự khác biệt giữa hai ngày bằng cách sử dụng DateTime. khác biệt()

Lớp

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 có phương thức diff() tính toán sự khác biệt giữa hai đối tượng
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4. Nó trả về một đối tượng
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
6 chứa thông tin như số năm, tháng và ngày, v.v. giữa hai ngày đã cho

Khi bạn có

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
6, bạn có thể sử dụng phương thức format() để định dạng nó theo nhu cầu của mình. Phương thức này chấp nhận các ký tự khác nhau như một phần của tham số chuỗi của nó. Chỉ cần nhớ rằng tất cả các ký tự này phải được bắt đầu bằng dấu phần trăm (%)

Dưới đây là danh sách tất cả các ký tự định dạng và cách sử dụng chúng

  • % — Điều này sẽ xuất ra ký tự % theo nghĩa đen
  • Y - Điều này sẽ xuất ra các năm bằng số với ít nhất 2 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ như 01, 03, 15
  • y - Điều này sẽ xuất ra các năm bằng số mà không có bất kỳ số 0 nào ở đầu. Ví dụ là 1, 3, 15
  • M — Điều này sẽ xuất ra các tháng bằng số với ít nhất 2 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ như 01, 05, 28
  • m - Điều này sẽ xuất ra các tháng bằng số mà không có bất kỳ số 0 nào ở đầu. Ví dụ là 1, 3, 12
  • D — Điều này sẽ xuất ra số ngày có ít nhất 2 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ như 01, 09, 29
  • d — Điều này sẽ xuất ra số ngày mà không có bất kỳ số 0 nào ở đầu. Ví dụ là 1, 9, 31
  • a — Điều này sẽ xuất ra tổng số ngày được tính bằng
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    1 hoặc (không xác định) nếu không
  • H — Điều này sẽ xuất ra số giờ có ít nhất 2 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ là 01, 23
  • h — Điều này sẽ xuất ra số giờ mà không có bất kỳ số 0 nào ở đầu. Ví dụ là 1, 23
  • I — Điều này sẽ xuất ra số phút với ít nhất 2 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ như 01, 03, 59
  • i - Điều này sẽ xuất số phút mà không có bất kỳ số 0 nào ở đầu. Ví dụ là 1, 3, 59
  • S — Điều này sẽ xuất ra số giây với ít nhất 2 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ như 01, 03, 57
  • là - Điều này sẽ xuất ra số giây mà không có bất kỳ số 0 nào ở đầu. Ví dụ là 1, 3, 57
  • F — Điều này sẽ xuất ra micro giây bằng số với ít nhất 6 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ như 007701, 052738, 428291
  • f — Điều này sẽ xuất ra micro giây bằng số với ít nhất 6 chữ số. Các chữ số đơn lẻ sẽ được theo sau bởi số 0 đứng đầu. Ví dụ như 7701, 52738, 428291
  • R — Điều này sẽ xuất ra “-” khi âm và “+” khi dương
  • r — Điều này sẽ xuất ra “-” khi âm và không có gì khi dương

Trong ví dụ sau, chúng tôi đã tính số năm, số tháng và số ngày giữa hai ngày

PHP

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

Lớp

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 đảm nhận mọi thứ cho bạn như số ngày khác nhau trong các tháng khác nhau, năm nhuận và giờ mùa hè, v.v.

Lấy số ngày giữa hai ngày

Khi bạn đã tính toán chênh lệch giữa hai ngày bằng phương pháp

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
3, việc tính số ngày giữa chúng thật dễ dàng. Bạn chỉ cần truyền chuỗi ký tự
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 cho phương thức
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
5. Điều quan trọng là hãy nhớ rằng những ngày được trả về bởi
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
6 hoặc
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
7 và
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 là khác nhau

Chuỗi ký tự

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
6 và
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
7 trả về số ngày giữa hai ngày sau khi trừ đi số tháng và năm. Giá trị của chúng sẽ không bao giờ lớn hơn 31. Mặt khác, chuỗi ký tự
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 trả về tổng số ngày đã trôi qua giữa hai ngày. Đây là một ví dụ

PHP

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');

Tính Số Giờ Giữa Hai Ngày

Mặc dù phương thức định dạng có chuỗi ký tự

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 để lấy tổng số ngày đã trôi qua giữa hai ngày, nhưng không có ký tự nào như vậy để lấy tổng số giờ. Tuy nhiên, chúng ta có thể tự tính toán

Cho đến nay, chúng tôi chỉ tạo các đối tượng

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 mà chúng tôi không chuyển bất kỳ thông tin nào về thời gian trong ngày trong chuỗi. Điều này có nghĩa là giá trị được trả về cho
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
34 (giờ),
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
35 (phút) và
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
36 (giây) sẽ luôn bằng 0. Vì chúng tôi muốn tìm hiểu cách tính số giờ giữa hai ngày, nên lần này chúng tôi sẽ chuyển các chuỗi có thời gian và/hoặc ngày khác nhau. Điểm hay của phương thức
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
3 của lớp
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
38 là nó cũng quan tâm đến sự khác biệt về múi giờ

Trong ví dụ sau, số giờ được trả về sẽ khác do múi giờ khác nhau

PHP

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
3

Dấu (+) ngụ ý rằng ngày thứ hai là trong tương lai đối với ngày đầu tiên. Dấu (-) ngụ ý rằng ngày thứ hai đã là quá khứ đối với ngày thứ nhất

Khi chênh lệch giữa hai ngày nhỏ hơn một ngày, giá trị được trả về bởi

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
34 sẽ cung cấp chính xác cho bạn chênh lệch giữa chúng theo giờ. Khi chênh lệch giữa hai ngày nhiều hơn một ngày, chẳng hạn như một tuần, vài tháng hoặc vài năm, v.v., bạn chỉ cần nhân số ngày mà
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 trả về với 24 rồi cộng số đó với số giờ mà
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
34 trả về. Một lần nữa, bạn không phải lo lắng về năm nhuận, v.v. vì lớp
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 lo tất cả những việc đó cho bạn

PHP

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
2

Bạn có thể nhận thấy rằng ngày thứ hai trong ví dụ trên là trong quá khứ so với ngày đầu tiên. Thời gian trong ngày thứ hai trở thành 9. 19. 29 sau khi tính đến sự khác biệt về múi giờ. Điều đó giải thích giá trị 02 được trả về bởi

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
34

Trong ví dụ sau, tôi đã chuyển ngày. Chênh lệch múi giờ vẫn vậy. Do đó, thời gian của ngày thứ hai vẫn là 9. 19. 29. Vì đây là ít hơn 11. 56. 59 trong ngày đầu tiên, số ngày trôi qua thay đổi từ 10 thành 9. Điều này là do ngày chỉ được tính đến 2018-01-18 11. 56. 59 như 2018-01-19 11. 56. 59 sẽ nhiều hơn ngày thứ hai (sau khi tính đến chênh lệch múi giờ). Bây giờ, giờ đã trôi qua từ 2018-01-18 11. 56. 59 đến 2018-01-19 9. 19. 29 là 21. Điều này mất tổng số giờ của chúng tôi đến 237

Như bạn có thể thấy, lớp

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 xử lý tất cả các vấn đề phức tạp cho bạn và cho bạn kết quả chính xác mà không gặp vấn đề gì

PHP

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4

Tính Số Phút Giữa Hai Ngày

Số phút được trả về bởi chuỗi ký tự

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
35 trong phương thức
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
5 bằng với số phút còn lại sau khi hàm
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
3 đã tính tất cả số ngày và giờ đã trôi qua. Nếu bạn muốn biết tổng số phút đã trôi qua giữa hai ngày, bạn sẽ phải tự mình thực hiện một số phép tính

Trước tiên, chúng ta phải chuyển đổi số ngày được trả về bởi

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
3 thành giờ bằng cách nhân nó với 24 và sau đó chuyển đổi tổng số giờ thành phút bằng cách nhân kết quả với 60. Lớp
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
4 tự động chăm sóc các năm nhuận và tiết kiệm thời gian trong ngày cho bạn nên phép nhân đơn giản cho kết quả chính xác

PHP

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
9

Một điều mà tôi muốn nhắc lại là bạn phải sử dụng

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
35 để lấy số phút. Người ta thường sử dụng
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
41 khi vội vàng nhưng hãy nhớ rằng
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
41 được dự trữ trong nhiều tháng

Tính Số Giây Giữa Hai Ngày

Cũng giống như phút, số giây được trả về bởi chuỗi ký tự

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
36 trong phương thức
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
5 được tính sau khi phương thức
$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
3 đã tính số ngày, giờ và phút đã trôi qua. Tuy nhiên, việc tính toán tổng số giây đã trôi qua giữa hai ngày rất dễ dàng khi chúng ta có ngày, giờ và phút. Tất cả những gì bạn phải làm là chuyển đổi chúng thành giây

Chúng tôi bắt đầu bằng cách chuyển đổi ngày và giờ thành phút như chúng tôi đã làm trong phần trước. Sau đó, chúng ta chỉ cần nhân kết quả với 60 và cộng số giây được trả về bởi chuỗi ký tự

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-01-18');
$interval = $first_date->diff($second_date);

// Output — 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');

$first_date = new DateTime('2010-07-19T10:56:37+00:00');
$second_date = new DateTime('2018-09-12T23:10:15+05:30');
$interval = $first_date->diff($second_date);

// Output — 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.
echo $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');
36

PHP

$first_date = new DateTime('1988-11-09');
$second_date = new DateTime('2023-05-09');
$interval = $first_date->diff($second_date);

// Output — 34 years 06 months and 00 days.
echo $interval->format('%Y years %M months and %D days.');

// Output — Total number of days passed: 12599.
echo $interval->format('Total number of days passed: %a.');
7

Tóm tắt nhanh

Hãy tóm tắt lại mọi thứ mà chúng tôi đã trình bày trong hướng dẫn này

  1. Bạn có thể tính chênh lệch giữa hai ngày bằng cách sử dụng phương thức
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    3 của lớp
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    4. Sau đó, bạn có thể lặp lại kết quả ở một định dạng cụ thể bằng phương thức
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    5
  2. Nếu bạn muốn tính tổng số giờ giữa hai ngày, bạn chỉ cần nhân số ngày do
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    3 trả về với 24 rồi cộng kết quả với số giờ do
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    3 trả về
  3. Tương tự, bạn có thể tính tổng số phút giữa hai ngày bằng cách tính tổng số giờ đã trôi qua trước tiên rồi nhân số đó với 60. Sau đó, chỉ cần thêm kết quả vào số phút được trả về bởi
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    3
  4. Kỹ thuật tương tự có thể được áp dụng để tính số giây giữa hai ngày. Chỉ cần tính số phút trước và chuyển đổi chúng thành giây bằng cách nhân với 60. Sau đó, bạn chỉ cần thêm kết quả vào số giây được trả về bởi
    $first_date = new DateTime('1988-11-09');
    $second_date = new DateTime('2023-05-09');
    $interval = $first_date->diff($second_date);
    
    // Output — 34 years 06 months and 00 days.
    echo $interval->format('%Y years %M months and %D days.');
    
    // Output — Total number of days passed: 12599.
    echo $interval->format('Total number of days passed: %a.');
    3

Hãy cho tôi biết nếu có bất cứ điều gì bạn muốn tôi làm rõ trong hướng dẫn này. Ngoài ra, rất hoan nghênh bạn bình luận nếu bạn biết các kỹ thuật khác để tính số ngày, giờ, phút hoặc giây giữa hai ngày trong PHP

Làm cách nào để tính giờ và phút giữa hai ngày trong php?

Làm cách nào để nhận chênh lệch múi giờ giữa hai ngày trong laravel?

function dateDiffInDays($date1, $date2) { $diff = strtotime($date2) - strtotime($date1); . 03. 00"; // Ngày kết thúc $date2 = date('Y-m-d'); $dateDiff = dateDiffInDays($date1, $date2); // Start date $date1 = "2021-03-01 04:03:00"; // End date $date2 = date('Y-m-d'); $dateDiff = dateDiffInDays($date1, $date2);

Làm cách nào để tính chênh lệch về số giờ giữa hai ngày trong php?

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

Làm cách nào để tìm sự khác biệt giữa hai ngày tính bằng phút trong php?

$start = date_create('2015-01-26 12. 01. 00'); . 15. 00'); . Lưu câu trả lời này. $diff=date_diff($end,$start); print_r($diff); You get results what do you want. Save this answer.