How to compare two dates in php

前端 未结 15 2441
臣服心动
臣服心动 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: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);
    }
    

提交回复
热议问题