복붙노트

PHP에서 두 날짜를 비교하는 방법

PHP

PHP에서 두 날짜를 비교하는 방법

날짜가 '03_01_12'및 '31_12_11'형식 인 경우 PHP에서 두 날짜를 비교하는 방법.

이 코드를 사용하고 있습니다 :

$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;

하지만 그게 작동하지 ..

해결법

  1. ==============================

    1.날짜가 유효한 날짜 개체인지 확인해야합니다.

    날짜가 유효한 날짜 개체인지 확인해야합니다.

    이 시도:

    $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]));
    

    그런 다음 strtotime () 메서드를 수행하여 차이를 얻을 수 있습니다.

  2. ==============================

    2.대괄호가 모두 일치하지는 않습니다.

    대괄호가 모두 일치하지는 않습니다.

    if(strtotime($date1))<strtotime($date2)))
    

    다음으로 변경하십시오.

    if(strtotime($date1) < strtotime($date2))
    
  3. ==============================

    3.DateTime :: createFromFormat 사용 :

    DateTime :: createFromFormat 사용 :

    $format = "d_m_y";
    $date1  = \DateTime::createFromFormat($format, "03_01_12");
    $date2  = \DateTime::createFromFormat($format, "31_12_11");
    
    var_dump($date1 > $date2);
    
  4. ==============================

    4.date_diff () 함수는 두 DateTime 객체 간의 차이점을 반환합니다.

    date_diff () 함수는 두 DateTime 객체 간의 차이점을 반환합니다.

    첫 번째 날짜가 두 번째 날짜 이전 인 경우 양수가 반환됩니다. 그렇지 않은 경우 음수 일 :

    <?php
    $date1=date_create("2013-03-15");
    $date2=date_create("2013-12-12");
    $diff=date_diff($date1,$date2);
    echo $diff->format("%R%a days");
    ?>
    

    출력은 "+272 일"이됩니다.

    작고 보기 흉한 사람 $ date1 = "2014-03-15"

     <?php
        $date1=date_create("2014-03-15");
        $date2=date_create("2013-12-12");
        $diff=date_diff($date1,$date2);
        echo $diff->format("%R%a days");
        ?>
    

    출력은 "-93 일"이됩니다.

  5. ==============================

    5.@ nevermind의 답변을 확장하면 DateTime :: createFromFormat : like를 사용할 수 있습니다.

    @ nevermind의 답변을 확장하면 DateTime :: createFromFormat : like를 사용할 수 있습니다.

    // use - instead of _. replace _ by - if needed.
    $format = "d-m-y";
    $date1  = DateTime::createFromFormat($format, date('d-m-y'));
    $date2  = DateTime::createFromFormat($format, str_replace("_", "-",$date2));    
    var_dump($date1 > $date2);
    
  6. ==============================

    6.

    <?php
           $expiry_date = "2017-12-31 00:00:00"
           $today = date('d-m-Y',time()); 
           $exp = date('d-m-Y',strtotime($expiry_date));
           $expDate =  date_create($exp);
           $todayDate = date_create($today);
           $diff =  date_diff($todayDate, $expDate);
           if($diff->format("%R%a")>0){
                 echo "active";
           }else{
               echo "inactive";
           }
           echo "Remaining Days ".$diff->format("%R%a days");
    ?>
    
  7. ==============================

    7.당신은 다음과 같은 것을 시도 할 수있다 :

    당신은 다음과 같은 것을 시도 할 수있다 :

            $date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
            $date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
            $dateDiff = date_diff($date1, $date2);
            var_dump($dateDiff);
    

    그런 다음 $ dateDiff -> d와 같은 날짜의 차이에 액세스 할 수 있습니다.

  8. ==============================

    8.당신이 문제가 뭔지 모르지만 :

    당신이 문제가 뭔지 모르지만 :

    function date_compare($d1, $d2)
    {
        $d1 = explode('_', $d1);
        $d2 = explode('_', $d2);
    
        $d1 = array_reverse($d1);
        $d2 = array_reverse($d2);
    
        if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
        {
            return $d2;
        }
        else
        {
            return $d1;
        }
    }
    
  9. ==============================

    9.이 시도

    이 시도

    $data1 = strtotime(\date("d/m/Y"));
    $data1 = date_create($data1);
    $data2 = date_create("21/06/2017");
    
    if($data1 < $data2){
        return "The most current date is date1";
    }
    
    return "The most current date is date2";
    
  10. ==============================

    10.각 시간에 대한 maketime ()의 결과를 비교합니다.

    각 시간에 대한 maketime ()의 결과를 비교합니다.

  11. ==============================

    11.나는 이것이 늦었다 고 알고있다. 그러나 나중에 참조 할 수 있도록 str_replace를 사용하여 날짜 형식을 인식 된 형식으로 지정하면 함수가 작동 할 것이다. 밑줄을 대시로 바꿉니다.

    나는 이것이 늦었다 고 알고있다. 그러나 나중에 참조 할 수 있도록 str_replace를 사용하여 날짜 형식을 인식 된 형식으로 지정하면 함수가 작동 할 것이다. 밑줄을 대시로 바꿉니다.

    //change the format to dashes instead of underscores, then get the timestamp
    $date1 = strtotime(str_replace("_", "-",$date1));
    $date2 = strtotime(str_replace("_", "-",$date2));
    
    //compare the dates
    if($date1 < $date2){
       //convert the date back to underscore format if needed when printing it out.
       echo '1 is small='.$date1.','.date('d_m_y',$date1);
    }else{
       echo '2 is small='.$date2.','.date('d_m_y',$date2);
    }
    
  12. ==============================

    12.당신은 정수로 변환하여 비교할 수 있습니다.

    당신은 정수로 변환하여 비교할 수 있습니다.

    예 :

    $ date_1 = 날짜 ( 'Ymd'); $ date_2 = '31_12_2011';

    $ date_2 = (int) implode (array_reverse (explode ( "_", $ date_2)));

    echo ($ date_1 <$ date_2)? '$ date_2는 $ date_1보다 큽니다.': '$ date_2는 $ date_1보다 작습니다.';

  13. ==============================

    13.OPs의 실제 문제에 답하지 않고 제목에 답하는 것. 이것은 "PHP에서 날짜 비교"의 가장 큰 결과이기 때문에.

    OPs의 실제 문제에 답하지 않고 제목에 답하는 것. 이것은 "PHP에서 날짜 비교"의 가장 큰 결과이기 때문에.

    꽤 단순한 Datetime 객체 (v> = 5.3.0)를 사용하여 직접 비교

    $date1 = new DateTime("now");
    $date2 = new DateTime("tomorrow");
    var_dump($date1 < $date2);
    

    참고 : Datetime 개체는 다음과 같이 미리 정의 된 날짜에 대해 만들 수도 있습니다.

    $date1 = new DateTime('2009-10-11');
    
  14. from https://stackoverflow.com/questions/8722806/how-to-compare-two-dates-in-php by cc-by-sa and MIT license