Hướng dẫn compare date php

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two dates (date1 and date2) and the task is to compare the given dates. Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format.

    Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates.

    Example:

    $date1 = "1998-11-24";

    $date2 = "1997-03-26";

    if ($date1 > $date2)

        echo "$date1 is latest than $date2";

    else

        echo "$date1 is older than $date2";

    ?>

    Output:

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

    Method 2: 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 = "12-03-26";

    $date2 = "2011-10-24";

    $dateTimestamp1 = strtotime($date1);

    $dateTimestamp2 = strtotime($date2);

    if ($dateTimestamp1 > $dateTimestamp2)

        echo "$date1 is latest than $date2";

    else

        echo "$date1 is older than $date2";

    ?>

    Output:

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

    Method 3: Using DateTime class to compare two dates.

    Example:

    $date1 = new DateTime("12-11-24");

    $date2 = new DateTime("2011-03-26");

    if ($date1 > $date2)

        echo $date1->format("Y-m-d") . " is latest than "

                . $date2->format("Y-m-d");

    else

        echo $date1->format("Y-m-d") . " is older than " 

                . $date2->format("Y-m-d");

    ?>

    Output:

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

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    answer

    83

    trong cơ sở dữ liệu, ngày có dạng như thế này 2011-10-2

    Lưu trữ nó trong YYYY-MM-DD và sau đó so sánh chuỗi sẽ hoạt động vì '1'> '0', v.v.

    83 hữu ích 4 bình luận chia sẻ

    answer

    228

    Nếu tất cả các ngày của bạn đều sau ngày 1 tháng 1 năm 1970, bạn có thể sử dụng một cái gì đó như:

    $today = date("Y-m-d");
    $expire = $row->expireDate; //from database
    
    $today_time = strtotime($today);
    $expire_time = strtotime($expire);
    
    if ($expire_time < $today_time) { /* do Something */ }
    

    Nếu bạn đang sử dụng PHP 5> = 5.2.0, bạn có thể sử dụng lớp DateTime:

    $today_dt = new DateTime($today);
    $expire_dt = new DateTime($expire);
    
    if ($expire_dt < $today_dt) { /* Do something */ }
    

    Hoặc một cái gì đó dọc theo những dòng này.

    228 hữu ích 5 bình luận chia sẻ

    answer

    25

    Chỉ để khen các câu trả lời đã cho sẵn, hãy xem ví dụ sau:

    $today = new DateTime('');
    $expireDate = new DateTime($row->expireDate); //from database
    
    
    
    if($today->format("Y-m-d") < $expireDate->format("Y-m-d")) { 
        //do something; 
    }
    

    Cập nhật: Hoặc sử dụng hàm old-school date () đơn giản :

    if(date('Y-m-d') < date('Y-m-d', strtotime($expire_date))){
        //echo not yet expired! 
    }   
    

    25 hữu ích 2 bình luận chia sẻ

    answer

    8

    Tôi sẽ không làm điều này với PHP. Một cơ sở dữ liệu nên biết hôm nay là ngày gì. (Ví dụ: sử dụng MySQL-> NOW ()), vì vậy sẽ rất dễ dàng so sánh trong Truy vấn và trả về kết quả, mà không gặp bất kỳ vấn đề nào tùy thuộc vào Loại ngày được sử dụng

    SELECT IF(expireDate < NOW(),TRUE,FALSE) as isExpired FROM tableName
    

    8 hữu ích 2 bình luận chia sẻ

    answer

    5

    $today = date('Y-m-d');//Y-m-d H:i:s
    $expireDate = new DateTime($row->expireDate);// From db 
    $date1=date_create($today);
    $date2=date_create($expireDate->format('Y-m-d'));
    $diff=date_diff($date1,$date2);
    
    //echo $timeDiff;
    if($diff->days >= 30){
        echo "Expired.";
    }else{
        echo "Not expired.";
    }
    

    5 hữu ích 0 bình luận chia sẻ

    answer

    2

    Đây là một cách về cách nhận được sự khác biệt giữa hai ngày trong vài phút.

    // set dates
    $date_compare1= date("d-m-Y h:i:s a", strtotime($date1));
    // date now
    $date_compare2= date("d-m-Y h:i:s a", strtotime($date2));
    
    // calculate the difference
    $difference = strtotime($date_compare1) - strtotime($date_compare2);
    $difference_in_minutes = $difference / 60;
    
    echo $difference_in_minutes;
    

    2 hữu ích 3 bình luận chia sẻ

    answer

    2

    Bạn có thể chuyển đổi ngày tháng thành dấu thời gian UNIX và so sánh sự khác biệt giữa chúng trong vài giây.

    $today_date=date("Y-m-d");
    $entered_date=$_POST['date'];
    $dateTimestamp1 = strtotime($today_date);
    $dateTimestamp2 = strtotime($entered_date);
    $diff= $dateTimestamp1-$dateTimestamp2;
    //echo $diff;
    if ($diff<=0)
       {
         echo "Enter a valid date";
       }
    

    2 hữu ích 0 bình luận chia sẻ

    answer

    0

    Tôi cũng đã gặp vấn đề đó và tôi giải quyết nó bằng cách:

    $today = date("Ymd");
    $expire = str_replace('-', '', $row->expireDate); //from db
    
    if(($today - $expire) > $NUMBER_OF_DAYS) 
    { 
        //do something; 
    }
    

    0 hữu ích 2 bình luận chia sẻ

    answer

    0

    Đây là phần xoay của tôi về cách nhận chênh lệch số ngày giữa hai ngày bằng PHP. Lưu ý việc sử dụng '!' ở định dạng để loại bỏ phần thời gian của ngày, nhờ vào thông tin từ DateTime createFromFormat mà không có thời gian .

    $today = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
    $wanted = DateTime::createFromFormat('!d-m-Y', $row["WANTED_DELIVERY_DATE"]);
    $diff = $today->diff($wanted);
    $days = $diff->days;
    if (($diff->invert) != 0) $days = -1 * $days;
    $overdue = (($days < 0) ? true : false);
    print "\n";
    

    0 hữu ích 0 bình luận chia sẻ

    answer

    0

    Nếu bạn muốn một ngày ($ date) hết hạn trong một khoảng thời gian nào đó, chẳng hạn như ngày hết hạn mã thông báo khi thực hiện đặt lại mật khẩu, đây là cách bạn có thể làm:

    $date = $row->expireDate;
    
    $date->add(new DateInterval('PT24H')); // adds 24 hours
    
    $now = new \DateTime();
    
    if($now < $date) { /* expired after 24 hours */ }
    

    Nhưng trong trường hợp của bạn, bạn có thể thực hiện phép so sánh như sau:

    $today = new DateTime('Y-m-d');
    
    $date = $row->expireDate;
    
    if($today < $date) { /* do something */ }
    

    0 hữu ích 5 bình luận chia sẻ

    answer

    -1

    Tìm thấy câu trả lời trên blog và nó đơn giản như sau:

    strtotime(date("Y"."-01-01")) -strtotime($newdate))/86400
    

    Và bạn sẽ nhận được những ngày giữa 2 ngày.

    -1 hữu ích 1 bình luận chia sẻ

    answer

    -1

    Điều này hoạt động nhờ logic so sánh chuỗi của PHP. Đơn giản là bạn có thể kiểm tra ...

    if ($startdate < $date) {// do something} 
    if ($startdate > $date) {// do something}
    

    Cả hai ngày phải ở cùng một định dạng. Các chữ số cần không được đệm ở bên trái và được sắp xếp theo thứ tự từ quan trọng nhất đến ít quan trọng nhất. Y-m-dY-m-d H:i:sthỏa mãn các điều kiện này.

    -1 hữu ích 3 bình luận chia sẻ

    answer

    -2

    trước hết, hãy cố gắng cung cấp định dạng bạn muốn cho ngày giờ hiện tại của máy chủ của bạn:

    1. Lấy ngày giờ hiện tại

      $ current_date = getdate ();

    2. Ngày và giờ riêng biệt để quản lý chúng như bạn muốn:

    $ current_date_only = $ current_date [năm] .'- '. $ current_date [mon] .'-'. $ current_date [mday]; $ current_time_only = $ current_date ['giờ']. ':'. $ current_date ['phút']. ':'. $ current_date ['seconds'];

    1. So sánh nó tùy thuộc vào việc bạn đang sử dụng date hay datetime trong DB của mình:

      $ today = $ current_date_only. ' '. $ current_time_only;

      hoặc là

      $ today = $ current_date_only;

      if ($ today <$ expireDate)

    hy vọng nó giúp

    -2 hữu ích 1 bình luận chia sẻ