Hướng dẫn php compare timestamps - php so sánh dấu thời gian

Tôi có ba ngày A, B và C.

Những gì tôi đang cố gắng làm là kiểm tra xem C có ở trong A và B không, nếu nó trả về đúng. Bất cứ ai có bất kỳ ý tưởng về cách làm điều này?

Bạn có thể sử dụng hàm strtotime () để có được chênh lệch thời gian giữa hai ngày (dữ liệu) trong vài phút bằng PHP.use strtotime() function to get the time difference between two dates (DateTimes) in minutes using PHP.1 gold badge6 silver badges15 bronze badges

Bạn có thể chuyển đổi chúng thành Unix Timestamp để so sánh.

Sử dụng hàm strtotime.

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

Lưu bài viết

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

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    0

    Bàn luận If the given dates are in the same format then use a simple comparison operator to compare the dates.

    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.

    Output:

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    1
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    3
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    7
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    9
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    9
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    0
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    1
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    4
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    64

    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    5
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    6
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    7
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    45
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    6
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    62
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    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:

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    0
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    6

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    15
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    6
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    62
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    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.

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    1
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    68
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    32
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    34
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    36
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    0
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    1
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    39
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    7
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    9
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    9
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    0
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    1
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    4
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    64

    Output:

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    3

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    15
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    6
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    7
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4 Using DateTime class to compare two dates.5
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    6
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    7
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
    Using DateTime class to compare two dates.

    Example:

    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    5
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    6
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    62
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    6

    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.

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    1
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    3
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    7
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    88
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    89

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    84
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    85
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    39
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    9
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    9
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    0
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    1
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    2
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    4

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    88
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    89

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    5
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    84
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    85
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    39
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    64

    Output:

        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    3
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    5
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    6
    $A = strtotime($A); //gives value in Unix Timestamp (seconds since 1970)
    $B = strtotime($B);
    $C = strtotime($C);
    
    if ((($C < $A) && ($C > $B)) || (($C > $A) && ($C < $B)) ){
      echo "Yes '$C' is between '$A' and '$B'";
     }
    
    7
        if ($time >= $date_start && $time <= $date_end)
        {
            echo "is between\n";
        } else {
            echo 'no';
        }
    
    4

    Bạn có thể sử dụng hàm strtotime () để có được chênh lệch thời gian giữa hai ngày (dữ liệu) trong vài phút bằng PHP.use strtotime() function to get the time difference between two dates (DateTimes) in minutes using PHP.use strtotime() function to get the time difference between two dates (DateTimes) in minutes using PHP.

    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.echo "$ date1 lớn hơn $ ngày2";?>use a simple comparison operator to compare the dates. echo "$date1 is older than $date2" ; ?>use a simple comparison operator to compare the dates. echo "$date1 is older than $date2" ; ?>

    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.using the strtotime() function along with comparison operators. The date_diff() function allows you to compare two dates and find the difference between them.