How to compare two dates in php

前端 未结 15 2434
臣服心动
臣服心动 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:33

    Try this

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

    compare the result of maketime() for each of the time

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

    You can to converte for integer number and compare.

    Eg.:

    $date_1 = date('Ymd');
    $date_2 = '31_12_2011';
    
    $date_2 = (int) implode(array_reverse(explode("_", $date_2)));
    
    echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
    
    0 讨论(0)
提交回复
热议问题