How to compare two dates in php

前端 未结 15 2453
臣服心动
臣服心动 2020-11-22 08:37

How to compare two dates in php if dates are in format \'03_01_12\' and \'31_12_11\' .

I am using this code:

$date1=date(\'         


        
相关标签:
15条回答
  • 2020-11-22 09:10

    Don't know what your problem is but:

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:11

    Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".

    Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly

    $date1 = new DateTime("2009-10-11");
    $date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
    var_dump($date1 < $date2);
    
    0 讨论(0)
  • 2020-11-22 09:12
    <?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");
    ?>
    
    0 讨论(0)
  • 2020-11-22 09:16

    you can try something like:

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

    You can then access the difference in days like this $dateDiff->d;

    0 讨论(0)
  • 2020-11-22 09:16

    I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)

    //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);
    }
    
    0 讨论(0)
  • 2020-11-22 09:21

    If both dates are in the same format then use a comparison operator.

    $date1 = "2018-05-05"; 
    $date2 = "2019-08-19"; 
    
    //comparison operator to  
    
    if ($date1 > $date2) {
        echo "$date1 is latest than $date2"; 
        }
    else{
        echo "$date1 is older than $date2"; 
        }
    

    Output: 2018-05-05 is older than 2019-08-19

    0 讨论(0)
提交回复
热议问题