Viết tập lệnh PHP để lấy chênh lệch thời gian theo ngày và năm, tháng, ngày, giờ, phút, giây giữa hai ngày
Ghi chú. Sử dụng lớp DateTime
Giải pháp mẫu
Mã PHP
Đầu ra mẫu
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds
Sơ đồ
Trình soạn thảo mã PHP
Có một cách khác để giải quyết giải pháp này?
Trước. Viết đoạn mã PHP để kiểm tra xem các ngày đã cho có hợp lệ hay không?
Tiếp theo. Viết PHP script đổi số tháng thành tên tháng.
Mức độ khó của bài tập này là gì?
Dễ dàng trung bình khóKiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource
PHP. Lời khuyên trong ngày
PHP. Tìm phần tử cuối cùng của một mảng trong khi sử dụng vòng lặp foreach trong PHP
$numItems = count[$arr]; $i = 0; foreach[$arr as $key=>$value] { if[++$i === $numItems] { echo "last index!"; } }
Điều đó đang được nói, bạn không cần phải lặp lại một "mảng" bằng cách sử dụng foreach trong php
Giới thiệu. https. //chút. ly/3nEHqwB
- bài tập. Top 16 chủ đề phổ biến nhất hàng tuần
- Bài tập SQL, Thực hành, Lời giải - THAM GIA
- Bài tập, Thực hành, Giải pháp SQL - SUBQUERIES
- JavaScript cơ bản - Bài tập, Thực hành, Lời giải
- Mảng Java. Bài tập, Luyện tập, Lời giải
- Bài Tập Lập Trình C, Thực Hành, Lời Giải. Tuyên bố có điều kiện
- Cơ sở dữ liệu nhân sự - SORT FILTER. Bài tập, Luyện tập, Lời giải
- Bài Tập Lập Trình C, Thực Hành, Lời Giải. Chuỗi
- Kiểu dữ liệu Python. Từ điển - Bài tập, Thực hành, Lời giải
- Câu đố lập trình Python - Bài tập, Thực hành, Giải pháp
- Mảng C++. Bài tập, Luyện tập, Lời giải
- Câu lệnh điều kiện và vòng lặp JavaScript - Bài tập, Thực hành, Lời giải
- Thuật toán cơ bản C# Sharp. Bài tập, Luyện tập, Lời giải
- Python Lambda - Bài tập, Thực hành, Giải pháp
- Khung dữ liệu Python Pandas. Bài tập, Luyện tập, Lời giải
- Công cụ chuyển đổi
- JavaScript. Xác thực biểu mẫu HTML
Trong bài viết này, chúng ta sẽ xem cách tính toán sự khác biệt giữa 2 ngày trong PHP, cùng với việc hiểu cách thực hiện nó thông qua các ví dụ. Đưa ra 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[]
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 nếu thành công và trả về FALSE nếu 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
PHP
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds05
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds07
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds08
+2 years 3 months0
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months3
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds08
+2 years 3 months5
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months8
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds00
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds01_______107
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
+2 years 3 months3
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds07
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds09
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds00
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds81
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds82
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds84
đầu ra
+2 years 3 months
Phương pháp 2. Để sử dụng công thức toán học ngày-giờ để tìm sự khác biệt giữa hai ngày. Nó trả về năm, tháng, ngày, giờ, phút, giây giữa hai ngày được chỉ định
Ví dụ. Trong ví dụ này, chúng ta sẽ sử dụng công thức toán học ngày-thời gian để tính chênh lệch giữa các ngày sẽ được trả về theo năm, tháng, ngày, giờ, phút, giây
PHP
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds87
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds89
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030____1031
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds032
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds033
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds036
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030____1031
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds032
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds040
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds043
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds047
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds032
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds036
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds89
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds054
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds056
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds058
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds040
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds032
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds043
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds047
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds049
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds071
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds040
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds074
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds058
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds078
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds079
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds080
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds082
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds084
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds086
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds088
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds040
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds074
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds058
+2 years 3 months05
________ 406 ________ 1071 ________ 408
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months10
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months12
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months14
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months16
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds040
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds074
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds058
+2 years 3 months23
_______424____1050____1071____427
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds088
+2 years 3 months29
+2 years 3 months30
+2 years 3 months31
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months33
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months35
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months37
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months39
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds040
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds074
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds058
+2 years 3 months23
+2 years 3 months47
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050____1071____427
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds088
+2 years 3 months52
+2 years 3 months53
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
+2 years 3 months16
+2 years 3 months56
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months33
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months60
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months62
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030_______1040
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds074
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds045
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds058
+2 years 3 months23
+2 years 3 months47
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050____1071____427
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds088
+2 years 3 months52
+2 years 3 months76
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds050
+2 years 3 months16
+2 years 3 months79
+2 years 3 months39
+2 years 3 months81
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
+2 years 3 months83
_______104____485____486
+2 years 3 months87
+2 years 3 months88
+2 years 3 months89
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds058
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds071
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
+2 years 3 months06
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds088
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
+2 years 3 months16
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
+2 years 3 months39
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
+2 years 3 months62
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds84
đầu ra
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds0
Phương pháp 3. Phương pháp này được sử dụng để lấy tổng số ngày giữa hai ngày được chỉ định
PHP
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds03
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds007
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds009
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030____1031
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds032
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds013
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds016
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds031
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds032
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds020
+2 years 3 months1
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds023
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds025
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds027
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds04
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds09
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds030
________ 1031
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds032
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds016 ________ 1050
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds009 _______ 1036
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds84
đầu ra
710Total days 1 years 11 months 10 days 8 hours 57 minutes 9 seconds8
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à Ví dụ về PHP này