Hướng dẫn can you compare time in php? - bạn có thể so sánh thời gian trong php?

Làm thế nào để so sánh thời gian trong PHP?

Tôi muốn nói điều này:

$ThatTime ="14:08:10";
$todaydate = date('Y-m-d');
$time_now=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('G:i:s',$time_now);
if($NowisTime >= $ThatTime) {
    echo "ok";
}

Mã trên không in ok. Tôi mong đợi nó.

Hướng dẫn can you compare time in php? - bạn có thể so sánh thời gian trong php?

Khi được hỏi ngày 27 tháng 5 năm 2011 lúc 23:56May 27, 2011 at 23:56

2

$ThatTime ="14:08:10";
if (time() >= strtotime($ThatTime)) {
  echo "ok";
}

Một giải pháp sử dụng DateTime (cũng liên quan đến múi giờ).

$dateTime = new DateTime($ThatTime);
if ($dateTime->diff(new DateTime)->format('%R') == '+') {
  echo "OK";
}

http://php.net/datetime.diff

Đã trả lời ngày 28 tháng 5 năm 2011 lúc 0:03May 28, 2011 at 0:03

KingcrunchKingCrunchKingCrunch

Huy hiệu vàng 126K2020 gold badges147 silver badges171 bronze badges

11

Để thấy thời gian tò mò lớn hơn hoặc bằng 14:08:10, hãy làm điều này:

if (time() >= strtotime("14:08:10")) {
  echo "ok";
}

Tùy thuộc vào các nguồn đầu vào của bạn, hãy đảm bảo tính đến múi giờ.

Xem Php Time () và Php Strtotime ()

Orlymee

2.3531 Huy hiệu vàng21 Huy hiệu bạc24 Huy hiệu đồng1 gold badge21 silver badges24 bronze badges

Đã trả lời ngày 28 tháng 5 năm 2011 lúc 0:02May 28, 2011 at 0:02

Lance vội vã vội vãLance Rushing

7.4504 huy hiệu vàng28 Huy hiệu bạc34 Huy hiệu đồng4 gold badges28 silver badges34 bronze badges

Cách đơn giản để so sánh thời gian là:

$time = date('H:i:s',strtotime("11 PM"));
if($time < date('H:i:s')){
     // your code
}

Đã trả lời ngày 1 tháng 12 năm 2015 lúc 7:21Dec 1, 2015 at 7:21

Hướng dẫn can you compare time in php? - bạn có thể so sánh thời gian trong php?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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:

    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.

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    0
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    2

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    4
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    6
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    8

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    8
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    9
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    0
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    3

    if (time() >= strtotime("14:08:10")) {
      echo "ok";
    }
    
    3

    Output:

    1998-11-24 is latest than 1997-03-26
    

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    5
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    6
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3
    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:

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    5
    if (time() >= strtotime("14:08:10")) {
      echo "ok";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3

    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.

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    0
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    if (time() >= strtotime("14:08:10")) {
      echo "ok";
    }
    
    7
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    4
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    $time = date('H:i:s',strtotime("11 PM"));
    if($time < date('H:i:s')){
         // your code
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3

    $time = date('H:i:s',strtotime("11 PM"));
    if($time < date('H:i:s')){
         // your code
    }
    
    3
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    $time = date('H:i:s',strtotime("11 PM"));
    if($time < date('H:i:s')){
         // your code
    }
    
    5
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    9
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    0
    $time = date('H:i:s',strtotime("11 PM"));
    if($time < date('H:i:s')){
         // your code
    }
    
    8

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    4
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    6
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    8

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    8
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    9
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    0
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    3

    if (time() >= strtotime("14:08:10")) {
      echo "ok";
    }
    
    3

    Output:

    12-03-26 is latest than 2011-10-24
    

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    5
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    6
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3
    Using DateTime class to compare two dates.

    Example:

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    5
    if (time() >= strtotime("14:08:10")) {
      echo "ok";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3

    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.

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    0
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    2

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    4
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    6
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3

    78

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    434
    $time = date('H:i:s',strtotime("11 PM"));
    if($time < date('H:i:s')){
         // your code
    }
    
    8

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    8

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    8
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    9
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    0
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    1
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    3

    78

    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    434
    $time = date('H:i:s',strtotime("11 PM"));
    if($time < date('H:i:s')){
         // your code
    }
    
    8

    if (time() >= strtotime("14:08:10")) {
      echo "ok";
    }
    
    3

    Output:

    2012-11-24 is latest than 2011-03-26
    

    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    4
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    5
    $dateTime = new DateTime($ThatTime);
    if ($dateTime->diff(new DateTime)->format('%R') == '+') {
      echo "OK";
    }
    
    6
    $ThatTime ="14:08:10";
    if (time() >= strtotime($ThatTime)) {
      echo "ok";
    }
    
    3


    Làm thế nào tôi có thể so sánh ngày và giờ hiện tại trong PHP?

    Bạn có thể so sánh ngày php với ngày hiện tại bằng cách sử dụng hàm strtotime () cùng với các toán tử so sánh.Hàm date_diff () cho phép bạn so sánh hai ngày và tìm sự khác biệt giữa chúng.using the strtotime() function along with comparison operators. The date_diff() function allows you to compare two dates and find the difference between them.

    Làm thế nào để MySQL so sánh thời gian trong PHP?

    Chỉ cần so sánh hai dấu thời gian sẽ đủ: $ t1 = strtotime ($ thời gian);$ t2 = strtotime ($ max_time);if ($ t1> $ t2) {..} Lưu câu trả lời này.$t1 = strtotime($time); $t2 = strtotime($Max_Time); if($t1 > $t2) { .. } Save this answer.

    Làm thế nào tôi có thể so sánh hai ngày trong PHP?

    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.echo "$ date1 lớn hơn $ ngày2";?>use strtotime() function to convert the given dates into the corresponding timestamp format and lastly compare these numerical timestamps to get the desired result. echo "$date1 is older than $date2" ; ?>

    Làm thế nào tôi có thể hẹn giờ trong PHP?

    Php Date Cú pháp & ví dụ..
    Cấm Y-M-D, cho định dạng ngày PHP Yyyy-MM-DD ..
    “Y để hiển thị năm hiện tại ..
    Tiết [Timestamp] là tùy chọn.Nếu không có dấu thời gian nào được cung cấp, PHP sẽ nhận được thời gian ngày PHP hiện tại trên máy chủ ..