How to compare two dates in php

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

    You will have to make sure that your dates are valid date objects.

    Try this:

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

    You can then perform the strtotime() method to get the difference.

提交回复
热议问题