Hướng dẫn này chỉ cho bạn cách kiểm tra ngày là ngày trong tương lai trong php. bạn sẽ học php kiểm tra xem ngày có sau hôm nay không. tôi đã giải thích đơn giản về kiểm tra php nếu ngày sau ngày hôm nay. Chúng ta sẽ xem xét ví dụ về kiểm tra php nếu ngày lớn hơn hôm nay. Được rồi, hãy đi sâu vào các bước
Tôi sẽ cung cấp cho bạn một ví dụ rất đơn giản về cách kiểm tra xem ngày có phải là ngày trong tương lai hay không trong PHP. vì vậy hãy xem cả hai ví dụ với đầu ra
ví dụ 1. Ngày kiểm tra PHP là Ngày trong tương lai từ Ngày hiện tại
mục lục. php
$startDate = strtotime[date['Y-m-d', strtotime['2021-11-30'] ] ];
$currentDate = strtotime[date['Y-m-d']];
if[$startDate > $currentDate] {
echo 'date is a future';
}
?>
đầu ra
date is a future
ví dụ 2. Ngày kiểm tra PHP là Ngày trong tương lai từ Ngày
mục lục. php
$startDate = strtotime[date['Y-m-d', strtotime['2021-11-30'] ] ];
$currentDate = strtotime['2021-11-20'];
if[$startDate > $currentDate] {
echo 'date is a future';
}
?>
đầu ra
date is a future
Tôi hy vọng nó có thể giúp bạn
Cho hai ngày [date1 và date2] và nhiệm vụ là so sánh các ngày đã cho. So sánh hai ngày trong PHP rất đơn giản khi cả hai ngày đều có cùng định dạng nhưng vấn đề phát sinh khi cả hai ngày có định dạng khác nhau
Phương pháp 1. Nếu các ngày đã cho có cùng định dạng thì hãy sử dụng toán tử so sánh đơn giản để so sánh các ngày
Ví dụ
12-03-26 is latest than 2011-10-245
12-03-26 is latest than 2011-10-246
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-248
12-03-26 is latest than 2011-10-249
12-03-26 is latest than 2011-10-240
12-03-26 is latest than 2011-10-241____40
12-03-26 is latest than 2011-10-241
12-03-26 is latest than 2011-10-242
12-03-26 is latest than 2011-10-241____44
12-03-26 is latest than 2011-10-241
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-248
12-03-26 is latest than 2011-10-249
20date is a future
12-03-26 is latest than 2011-10-240
22date is a future
12-03-26 is latest than 2011-10-242
24date is a future
25date is a future
26date is a future
27date is a future
12-03-26 is latest than 2011-10-241
29date is a future
25date is a future
26date is a future
12-03-26 is latest than 2011-10-2452____41
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2455
đầu ra.
4$startDate = strtotime[date['Y-m-d', strtotime['2021-11-30'] ] ];
$currentDate = strtotime['2021-11-20'];
if[$startDate > $currentDate] {
echo 'date is a future';
}
?>
Phương pháp 2. Nếu cả hai ngày đã cho ở các định dạng khác nhau thì hãy sử dụng hàm strtotime[] để chuyển đổi các ngày đã cho thành định dạng dấu thời gian tương ứng và cuối cùng so sánh các dấu thời gian dạng số này để có được kết quả mong muốn
Ví dụ
12-03-26 is latest than 2011-10-245
12-03-26 is latest than 2011-10-246
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2459
12-03-26 is latest than 2011-10-2460
12-03-26 is latest than 2011-10-240
12-03-26 is latest than 2011-10-241____763____41
12-03-26 is latest than 2011-10-242
12-03-26 is latest than 2011-10-241____767
12-03-26 is latest than 2011-10-241
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2470
12-03-26 is latest than 2011-10-2471
12-03-26 is latest than 2011-10-2472
12-03-26 is latest than 2011-10-241____774
20date is a future
12-03-26 is latest than 2011-10-240
12-03-26 is latest than 2011-10-2477
12-03-26 is latest than 2011-10-2478
12-03-26 is latest than 2011-10-241____774
20date is a future
12-03-26 is latest than 2011-10-242
12-03-26 is latest than 2011-10-2477
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2485
12-03-26 is latest than 2011-10-249
20date is a future
12-03-26 is latest than 2011-10-2472
22date is a future
12-03-26 is latest than 2011-10-2478
24date is a future
25date is a future
26date is a future
27date is a future
12-03-26 is latest than 2011-10-241
29date is a future
25date is a future
26date is a future
12-03-26 is latest than 2011-10-2452____41
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2455
đầu ra.
12-03-26 is latest than 2011-10-24
Phương pháp 3. Sử dụng lớp DateTime để so sánh hai ngày
Ví dụ
12-03-26 is latest than 2011-10-245
12-03-26 is latest than 2011-10-246
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2459
12-03-26 is latest than 2011-10-2407
12-03-26 is latest than 2011-10-2408
12-03-26 is latest than 2011-10-240
12-03-26 is latest than 2011-10-241
12-03-26 is latest than 2011-10-2411
12-03-26 is latest than 2011-10-2412
12-03-26 is latest than 2011-10-2413
12-03-26 is latest than 2011-10-2477
12-03-26 is latest than 2011-10-242
12-03-26 is latest than 2011-10-241
12-03-26 is latest than 2011-10-2411
12-03-26 is latest than 2011-10-2412
12-03-26 is latest than 2011-10-2419
12-03-26 is latest than 2011-10-2477
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2402
12-03-26 is latest than 2011-10-249
20date is a future
12-03-26 is latest than 2011-10-240
22date is a future
12-03-26 is latest than 2011-10-242
24date is a future
25date is a future
26date is a future
12-03-26 is latest than 2011-10-240
12-03-26 is latest than 2011-10-2412
12-03-26 is latest than 2011-10-2413
12-03-26 is latest than 2011-10-2414
12-03-26 is latest than 2011-10-2415
_______416____417____42
12-03-26 is latest than 2011-10-2412
12-03-26 is latest than 2011-10-2413
12-03-26 is latest than 2011-10-2477
29date is a future
25date is a future
26date is a future
12-03-26 is latest than 2011-10-240
12-03-26 is latest than 2011-10-2412
12-03-26 is latest than 2011-10-2413
12-03-26 is latest than 2011-10-2414
12-03-26 is latest than 2011-10-2429
_______416____417____42
12-03-26 is latest than 2011-10-2412
12-03-26 is latest than 2011-10-2413
12-03-26 is latest than 2011-10-2477
12-03-26 is latest than 2011-10-247
12-03-26 is latest than 2011-10-2455
đầu ra.
2date is a future
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