Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận If the given dates are in the same format then use a simple comparison operator to compare the dates.
Example:
2012-11-24 is latest than 2011-03-260
Cho hai ngày [ngày 1 và ngày 2] 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 ở cùng định dạng nhưng vấn đề phát sinh khi cả hai ngày ở một định dạng khác nhau.
Phương pháp 1: Nếu các ngày đã cho ở cùng định dạng thì sử dụng toán tử so sánh đơn giản để so sánh các ngày.
2012-11-24 is latest than 2011-03-261
2012-11-24 is latest than 2011-03-262
2012-11-24 is latest than 2011-03-263
2012-11-24 is latest than 2011-03-264
2012-11-24 is latest than 2011-03-265
2012-11-24 is latest than 2011-03-2622.
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
92012-11-24 is latest than 2011-03-269
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
02012-11-24 is latest than 2011-03-261
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
22012-11-24 is latest than 2011-03-265
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
4$date1=date['d/m/y'];
$tempArr=explode['_', '31_12_11'];
$date2 = date["d/m/y", mktime[0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]]];
4Output:
1998-11-24 is latest than 1997-03-26
5$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
6 $date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
7$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
2012-11-24 is latest than 2011-03-26
4 If both of the given dates are in different formats then use strtotime[] function to convert the given dates into the corresponding timestamp format and lastly compare these numerical timestamps to get the desired result.
Example:
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
5$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
6 $date1=date['d/m/y'];
$tempArr=explode['_', '31_12_11'];
$date2 = date["d/m/y", mktime[0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]]];
22012-11-24 is latest than 2011-03-264
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 số này để có kết quả mong muốn.
2012-11-24 is latest than 2011-03-260
2012-11-24 is latest than 2011-03-261
2012-11-24 is latest than 2011-03-262
$date1=date['d/m/y'];
$tempArr=explode['_', '31_12_11'];
$date2 = date["d/m/y", mktime[0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]]];
82012-11-24 is latest than 2011-03-264
2012-11-24 is latest than 2011-03-265
2012-11-24 is latest than 2011-03-262
$format = "d_m_y";
$date1 = \DateTime::createFromFormat[$format, "03_01_12"];
$date2 = \DateTime::createFromFormat[$format, "31_12_11"];
var_dump[$date1 > $date2];
2224$format = "d_m_y";
$date1 = \DateTime::createFromFormat[$format, "03_01_12"];
$date2 = \DateTime::createFromFormat[$format, "31_12_11"];
var_dump[$date1 > $date2];
4 2012-11-24 is latest than 2011-03-262
$format = "d_m_y";
$date1 = \DateTime::createFromFormat[$format, "03_01_12"];
$date2 = \DateTime::createFromFormat[$format, "31_12_11"];
var_dump[$date1 > $date2];
6$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
02012-11-24 is latest than 2011-03-261
$format = "d_m_y";
$date1 = \DateTime::createFromFormat[$format, "03_01_12"];
$date2 = \DateTime::createFromFormat[$format, "31_12_11"];
var_dump[$date1 > $date2];
92012-11-24 is latest than 2011-03-265
2012-11-24 is latest than 2011-03-2622.
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
92012-11-24 is latest than 2011-03-269
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
02012-11-24 is latest than 2011-03-261
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
22012-11-24 is latest than 2011-03-265
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
4$date1=date['d/m/y'];
$tempArr=explode['_', '31_12_11'];
$date2 = date["d/m/y", mktime[0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]]];
4Output:
12-03-26 is latest than 2011-10-24
5$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
6 $date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
7$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
2012-11-24 is latest than 2011-03-26
4 Using DateTime class to compare two dates.
Example:
2012-11-24 is latest than 2011-03-260
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
5$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
6 $date1=date['d/m/y'];
$tempArr=explode['_', '31_12_11'];
$date2 = date["d/m/y", mktime[0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]]];
22012-11-24 is latest than 2011-03-264
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 số này để có kết quả mong muốn.
2012-11-24 is latest than 2011-03-261
2012-11-24 is latest than 2011-03-262
2012-11-24 is latest than 2011-03-263
2012-11-24 is latest than 2011-03-264
2012-11-24 is latest than 2011-03-265
2012-11-24 is latest than 2011-03-2622.
12-03-26 is latest than 2011-10-2408
12-03-26 is latest than 2011-10-2409
2012-11-24 is latest than 2011-03-265
12-03-26 is latest than 2011-10-2404
12-03-26 is latest than 2011-10-2405
$format = "d_m_y";
$date1 = \DateTime::createFromFormat[$format, "03_01_12"];
$date2 = \DateTime::createFromFormat[$format, "31_12_11"];
var_dump[$date1 > $date2];
9$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
92012-11-24 is latest than 2011-03-269
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
02012-11-24 is latest than 2011-03-261
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
22012-11-24 is latest than 2011-03-265
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
412-03-26 is latest than 2011-10-2408
12-03-26 is latest than 2011-10-2409
2012-11-24 is latest than 2011-03-265
12-03-26 is latest than 2011-10-2404
12-03-26 is latest than 2011-10-2405
$format = "d_m_y";
$date1 = \DateTime::createFromFormat[$format, "03_01_12"];
$date2 = \DateTime::createFromFormat[$format, "31_12_11"];
var_dump[$date1 > $date2];
9$date1=date['d/m/y'];
$tempArr=explode['_', '31_12_11'];
$date2 = date["d/m/y", mktime[0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]]];
4
Output:
2012-11-24 is latest than 2011-03-26
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
5$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
6 $date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
72012-11-24 is latest than 2011-03-264
Cách so sánh hai ngày trong PHP nếu ngày ở định dạng
12-03-26 is latest than 2011-10-2429 và
12-03-26 is latest than 2011-10-2430.
Tôi đang sử dụng mã này:
$date1=date['d_m_y'];
$date2='31_12_11';
if[strtotime[$date1] < strtotime[$date2]]
echo '1 is small ='.strtotime[$date1].','.$date1;
else
echo '2 is small ='.strtotime[$date2].','.$date2;
Nhưng nó không hoạt động ..
Đã hỏi ngày 4 tháng 1 năm 2012 lúc 6:18Jan 4, 2012 at 6:18
Rohan Kumarrohan KumarRohan Kumar
Phù bằng vàng 40K1173 Huy hiệu bạc103 Huy hiệu đồng11 gold badges73 silver badges103 bronze badges
0
Bạn sẽ phải đảm bảo rằng ngày của bạn là các đối tượng ngày hợp lệ.
Thử cái này:
$date1=date['d/m/y'];
$tempArr=explode['_', '31_12_11'];
$date2 = date["d/m/y", mktime[0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]]];
Sau đó, bạn có thể thực hiện phương thức
12-03-26 is latest than 2011-10-2431 để có được sự khác biệt.
Đã trả lời ngày 4 tháng 1 năm 2012 lúc 7:17Jan 4, 2012 at 7:17
1
Sử dụng DateTime :: createdFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat[$format, "03_01_12"];
$date2 = \DateTime::createFromFormat[$format, "31_12_11"];
var_dump[$date1 > $date2];
Đã trả lời ngày 4 tháng 1 năm 2012 lúc 7:50Jan 4, 2012 at 7:50
Nevvermindnevvermindnevvermind
3.2621 Huy hiệu vàng35 Huy hiệu bạc44 Huy hiệu đồng1 gold badge35 silver badges44 bronze badges
1
Không trả lời vấn đề thực tế của OPS, nhưng chỉ trả lời tiêu đề. Vì đây là kết quả hàng đầu cho "so sánh ngày trong PHP".
Khá đơn giản để sử dụng các đối tượng DateTime [
12-03-26 is latest than 2011-10-2432] và so sánh chúng trực tiếp
$date1 = new DateTime["2009-10-11"];
$date2 = new DateTime["tomorrow"]; // Can use date/string just like strtotime.
var_dump[$date1 < $date2];
Đã trả lời ngày 29 tháng 5 năm 2018 lúc 21:01May 29, 2018 at 21:01
Mohd Abdul Mujibmohd Abdul MujibMohd Abdul Mujib
12.3k8 Huy hiệu vàng60 Huy hiệu bạc86 Huy hiệu Đồng8 gold badges60 silver badges86 bronze badges
1
Hàm date_diff [] trả về sự khác biệt giữa hai đối tượng DateTime.
Nếu ngày đầu tiên là trước ngày thứ hai, số ngày dương sẽ được trả lại; nếu không thì một số ngày âm:
đầu ra sẽ là "+272 ngày";
Thay đổi $ ngày1 = "2014-03-15"
Đầu ra sẽ là "-93 ngày"
Đã trả lời ngày 17 tháng 3 năm 2015 lúc 13:03Mar 17, 2015 at 13:03
RishabhrishabhRishabh
4865 Huy hiệu bạc8 Huy hiệu Đồng5 silver badges8 bronze badges
Đã trả lời ngày 17 tháng 11 năm 2015 lúc 8:09Nov 17, 2015 at 8:09
ManikantamanikantaManikanta
Phù hiệu Bạc 19333 silver badges15 bronze badges
1
Mở rộng câu trả lời của @nevermind, người ta có thể sử dụng datetime :: createdfromformat: thích,
12-03-26 is latest than 2011-10-240
Đã trả lời ngày 20 tháng 5 năm 2015 lúc 8:45May 20, 2015 at 8:45
Rohan Kumarrohan KumarRohan Kumar
Phù bằng vàng 40K1173 Huy hiệu bạc103 Huy hiệu đồng11 gold badges73 silver badges103 bronze badges
Bạn sẽ phải đảm bảo rằng ngày của bạn là các đối tượng ngày hợp lệ.
12-03-26 is latest than 2011-10-241
Thử cái này:
Sau đó, bạn có thể thực hiện phương thức
12-03-26 is latest than 2011-10-2431 để có được sự khác biệt.Dec 8, 2014 at 12:16
1
Đã trả lời ngày 4 tháng 1 năm 2012 lúc 7:17
12-03-26 is latest than 2011-10-242
Sử dụng DateTime :: createdFromFormat:Jun 21, 2017 at 19:07
Đã trả lời ngày 4 tháng 1 năm 2012 lúc 7:50
Nevvermindnevvermind
12-03-26 is latest than 2011-10-243
3.2621 Huy hiệu vàng35 Huy hiệu bạc44 Huy hiệu đồngJan 12, 2021 at 1:41
Không trả lời vấn đề thực tế của OPS, nhưng chỉ trả lời tiêu đề. Vì đây là kết quả hàng đầu cho "so sánh ngày trong PHP".
Khá đơn giản để sử dụng các đối tượng DateTime [
12-03-26 is latest than 2011-10-2432] và so sánh chúng trực tiếp
Đã trả lời ngày 29 tháng 5 năm 2018 lúc 21:0123 gold badges127 silver badges187 bronze badges
Mohd Abdul Mujibmohd Abdul MujibJan 4, 2012 at 6:25
12.3k8 Huy hiệu vàng60 Huy hiệu bạc86 Huy hiệu Đồngianace
Hàm date_diff [] trả về sự khác biệt giữa hai đối tượng DateTime.2 gold badges17 silver badges31 bronze badges
Nếu ngày đầu tiên là trước ngày thứ hai, số ngày dương sẽ được trả lại; nếu không thì một số ngày âm:
12-03-26 is latest than 2011-10-244
đầu ra sẽ là "+272 ngày";Mar 4, 2015 at 9:31
Thay đổi $ ngày1 = "2014-03-15"Alun
Đầu ra sẽ là "-93 ngày"2 bronze badges
Đã trả lời ngày 17 tháng 3 năm 2015 lúc 13:03
Eg.:
12-03-26 is latest than 2011-10-245
Rishabhrishabh♦
4865 Huy hiệu bạc8 Huy hiệu Đồng21 gold badges75 silver badges127 bronze badges
Đã trả lời ngày 17 tháng 11 năm 2015 lúc 8:09Mar 20, 2017 at 13:49
Manikantamanikanta
12-03-26 is latest than 2011-10-246
Phù hiệu Bạc 1933
Mở rộng câu trả lời của @nevermind, người ta có thể sử dụng datetime :: createdfromformat: thích,5 gold badges40 silver badges57 bronze badges
Đã trả lời ngày 20 tháng 5 năm 2015 lúc 8:45Jan 4, 2012 at 6:30
Bạn có thể thử một cái gì đó như:devdRew
Sau đó, bạn có thể truy cập sự khác biệt trong những ngày như $ datediff-> d này;3 gold badges22 silver badges33 bronze badges
Đã trả lời ngày 8 tháng 12 năm 2014 lúc 12:16
Thử cái nàyĐã trả lời ngày 21 tháng 6 năm 2017 lúc 19:07Dec 4, 2018 at 10:51
So sánh điểm chuẩn Date_Create, Strtotime, DateTime và Direct
12-03-26 is latest than 2011-10-248
Tôi chỉ đã thêm hai dòng nữa với mã của bạn
Đã trả lời ngày 20 tháng 12 năm 2018 lúc 7:11Dec 20, 2018 at 7:11
SantisantiSanti
6288 Huy hiệu bạc22 Huy hiệu Đồng8 silver badges22 bronze badges
Nếu cả hai ngày ở cùng định dạng thì hãy sử dụng toán tử so sánh.
12-03-26 is latest than 2011-10-249
Đầu ra: 2018-05-05 cũ hơn 2019-08-19
Bhargav Rao
47.4K27 Huy hiệu vàng122 Huy hiệu bạc137 Huy hiệu đồng27 gold badges122 silver badges137 bronze badges
Đã trả lời ngày 20 tháng 8 năm 2019 lúc 5:31Aug 20, 2019 at 5:31
1