Home » Php » PHP: Birthday check today´s date

PHP: Birthday check today´s date

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have user´s birthday stored in birthday as 1999-02-26.

How can I check if the birthday is today?

if(date('m-d') == ..?
How to&Answers:
if(date('m-d') == substr($birthday,5,5))

To add what Tim said:

if(date('m-d') == substr($birthday,5,5) or (date('y')%4 <> 0 and substr($birthday,5,5)=='02-29' and date('m-d')=='02-28'))

Answer:

This answer should work, but it depends on strtotime being able to figure out your database’s date format:

$birthDate = '1999-02-26'; // Read this from the DB instead
$time = strtotime($birthDate);
if(date('m-d') == date('m-d', $time)) {
    // They're the same!
}

Answer:

<?php
/**
 * @param string $birthday Y-m-d
 * @param int $now
 * @return bool
 */
function birthdayToday($birthday, $now = null) {
    $birthday = substr($birthday, -5);
    if ($now === null) {
        $now = time();
    }
    $today = date('m-d', $now);
    return $birthday == $today || $birthday == '02-29' && $today == '02-28' && !checkdate(2, 29, date('Y', $now));
}

Answer:

I used this one

$birthday = new DateTime("05-28-2020");
$today = new DateTime(date("Y-m-d"));

if ($birthday->format("m-d") == $today->format("m-d")) {
 echo 'Today is your birthday';
} else {
 echo 'Today is not your birthday';
}

Answer:

From PHP 5.2 upwards:

if (substr($dateFromDb, -5) === date_create()->format('m-d')) {
    // Happy birthday!
}