How to compare two dates without years in php?

前端 未结 3 607
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 06:18

I would like to compare the two dates ,one date is which the 02 aug 1921 , and the second date is present date.How to compare the old date and present date day and month .if

相关标签:
3条回答
  • 2021-01-20 07:14

    Probably the most straight forward way is strtotime and date.

    $date1 = '1 Aug 2012';
    $date2 = '1 Aug 1912';
    if (date('m-d', strtotime($date1)) === date('m-d', strtotime($date2)) {
    
    }
    
    0 讨论(0)
  • 2021-01-20 07:15

    Why don't you split the dates into year, month and day? and then:

    if (($month1 == $month2) && ($day1 == $day2)) {
      echo 'hi';
    }
    
    0 讨论(0)
  • 2021-01-20 07:20

    If you are on PHP 5.3 you can use the new DateTime class:

    $oDate1 = new DateTime( $datetime1 );
    $oDate2 = new DateTime( $datetime2 );
    
    if( $oDate1->format('m-d') == $oDate2->format('m-d')
      echo 'yes';
    
    0 讨论(0)
提交回复
热议问题