Php so sánh chuỗi ngày giờ

Hầu hết các ứng dụng web ngày nay đều yêu cầu bạn làm việc với các giá trị ngày tháng, dù là ứng dụng thương mại điện tử hay ứng dụng đặt vé

Hướng dẫn này sẽ giúp bạn tìm hiểu cách so sánh ngày tháng trong PHP

  • PHP So sánh ngày trong chuỗi
  • So sánh ngày PHP sử dụng strtotime[]
  • Sử dụng đối tượng DateTime để so sánh ngày

PHP So sánh ngày trong chuỗi

Khi ngày của bạn là các chuỗi có định dạng

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
5, thì bạn có thể sử dụng toán tử so sánh để so sánh các giá trị của chúng


PHP biết cách so sánh hai chuỗi ngày ở trên, vì vậy đầu ra sẽ là “2020-10-11 lớn hơn 2019-09-15”

Nhưng phương pháp này sẽ không hoạt động khi bạn có ngày tháng ở các định dạng khác, vì vậy hãy xem một cách khác để so sánh ngày tháng trong PHP tiếp theo

So sánh ngày PHP sử dụng strtotime[]

Hàm PHP

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
6 chuyển đổi mô tả thời gian bằng tiếng Anh thành dấu thời gian Unix

Dấu thời gian Unix là giá trị

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
7 đại diện cho giá trị ngày giờ, vì vậy bạn có thể so sánh các dấu thời gian này bằng toán tử so sánh

Đây là một ví dụ về việc sử dụng hàm

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
6 vào ngày tháng

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}

Giá trị của

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
9 sẽ thay đổi tùy thuộc vào thời điểm bạn chạy đoạn mã trên, nhưng rất có thể kết quả sẽ là “12 tháng 1 năm 2019 đã là quá khứ” [vì bài viết này được viết vào năm 2022 😉]

Chuyển đổi mô tả thời gian bằng cách sử dụng

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
6 cho phép bạn so sánh ngày chính xác bằng cách sử dụng dấu thời gian Unix

Nhưng vì con người khó đọc được những dấu thời gian này, nên bạn nên giữ nguyên ngày tháng. Đó là lý do tại sao có các biến

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
1 và
// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
2 trong ví dụ trên

Ngoài ra, bạn cũng có thể chuyển đổi kết quả về ngày có thể đọc được bằng hàm

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
3

$date = strtotime["today"];

// 👇 after comparison is done, format back the timestamp
$date = date["Y-m-d", $date];

print $date; // 2022-09-23

Và đó là cách bạn sử dụng

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
6 để so sánh ngày ở các định dạng khác nhau

Sử dụng đối tượng DateTime để so sánh ngày

Lớp PHP

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
5 có thể được sử dụng để tạo các đối tượng
// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
5

Giống như hàm

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
6, bạn có thể so sánh các đối tượng
// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
5 bằng toán tử so sánh

Điểm khác biệt là bạn cần sử dụng hàm

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
9 để định dạng đối tượng khi gọi cấu trúc
$date = strtotime["today"];

// 👇 after comparison is done, format back the timestamp
$date = date["Y-m-d", $date];

print $date; // 2022-09-23
0

Xem xét ví dụ sau

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
1

Khi sử dụng hàm

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
6, bạn cần sử dụng hàm
// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
3 để chuyển đổi dấu thời gian thành định dạng có thể đọc được

Nhưng việc tạo các đối tượng

// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
5 cho phép bạn sử dụng hàm
// 👇 declare dates
$date1 = "12 January 2019";
$date2 = "today";

// 👇 convert to Unix timestamps
$date1_unix = strtotime[$date1];
$date2_unix = strtotime[$date2];

// compare dates
if [$date1_unix > $date2_unix] {
    print "$date1 is in the future";
} else {
    print "$date1 is in the past";
}
9 từ đối tượng để chuyển đổi ngày tháng

Chủ Đề