phpmysqlcontinuous-integrationpyrocms

How to slice a date so, i can compare with month and day only?


I have a problem with date . I have a Birth date eg. 1993-06-01 in database. And now i want to compare with now date so, I can pop up the birthday message. How is this possible to compare with only month and day field. Especially in query so i can attached their name with birthday message Used framework Pyrocms.


Solution

  • For PHP just use DateTime.

    Example:

    $date1 = new DateTime('1993-06-01');
    $date2 = new DateTime();
    if ($date1->format('m-d') === $date2->format('m-d')) {
      // send the birthday message
    }
    

    If you want to do this in SQL then use MONTH() and DAY().

    Example:

    $sql = "SELECT * FROM `table` WHERE MONTH(`date_column`) = " . $date->format('n') . " AND DAY(`date_column`) = " . $date->format('j');