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

前端 未结 6 924
醉话见心
醉话见心 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 21:59

    Use DATE_FORMAT() function to extract part of date:

    Ref: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

    SELECT * from table_name WHERE DATE_FORMAT(subscription_date, '%d-%m') = "05-05";
    
    0 讨论(0)
  • 2021-01-22 22:07

    use like

    $current_date = date("d-m");
    $subscription = "03-06-2016";
    $subscription_date  = date("d-m", strtotime($subscription));
    if($current_date ==$subscription_date)
    {
        echo "date is equal";
    }else
    {
        echo "date is not equal";
    }
    
    0 讨论(0)
  • 2021-01-22 22:11

    Use DateTime() PHP objects.

    Considering you have an array with user info from mysql query result: ($userData['suscriptionDate'])

    $today = new DateTime();
    
    $userSuscription = new DateTime($userData['suscriptionDate']);
    
    if ( $today->format('d') == $userSuscription->format('d') && $today->format('m') == $userSuscription->format('m')) {
    
        echo 'Congratulations!!';
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-22 22:21

    This may help you

    $sdate = $row['subscription_date'];
    $date1 = date("m-d");
    $date2 = date("m-d",strtotime($sdate)) ;
    if ($date1 == $date2) {
    
    }
    
    0 讨论(0)
  • 2021-01-22 22:25

    The trick in this is to let the month come first. This way PHP can compare the numbers by highest value. Take a look at the following example:

    $aDate = DateTime::createFromFormat('m-d', '05-20');
    $bDate = DateTime::createFromFormat('m-d', '06-29');
    
    if ($aDate->format('md') > $bDate->format('md')) {
        echo "'aDate' is bigger than 'bDate'";
    }
    
    0 讨论(0)
提交回复
热议问题