Shortest way to compare 2 dates in dd/mm/yyyy format

后端 未结 7 1249
轻奢々
轻奢々 2021-01-20 23:44

can anyone suggest the neatest way to do this comparison? I need to test if a date provided in dd/mm/yyyy format is less than a fixed date, e.g 01/05/2009 - I know I can convert

7条回答
  •  滥情空心
    2021-01-21 00:35

    if you want to improve your code, I would suggest you to use DateTime class. It's been introduced in php 5.2.0 (so, it still might not be supported on all servers)

    in php 5.3.0 you can write something like this:

    $d1 = DateTime::createFromFormat('d/m/Y', '02/03/2009');
    $d2 = DateTime::createFromFormat('d/m/Y', '02/05/2009');
    $interval = $d1->diff($d2);
    echo $interval->format('%R%d days');
    

提交回复
热议问题