Compare only day and month without year in php ex. birth date

前端 未结 6 931
醉话见心
醉话见心 2021-01-22 21:45

I want to compare current date\'s day and month with subscription date\'s day and month only. ex.current date(d-m) = 3-6 and I want compare it with any other

6条回答
  •  一向
    一向 (楼主)
    2021-01-22 22:13

    I think, more elegant way to compare, especially when you have a full date with time is diff function of Datetime class:

    $d1 = new Datetime();
    $d2 = new Datetime('+3 months +2 days +3 hours');
    
    $diff = $d1->diff($d2);
    var_dump($diff->d); // 2
    var_dump($diff->m); // 2
    // or have a comparison as a string
    var_dump($diff->format('Difference is in %R%a days'));
    // output: Difference is in 63 days
    

    Enjoy! Link to doc

提交回复
热议问题