Selecting from database where date is same month and day
Moderator: General Moderators
Selecting from database where date is same month and day
Alright I've got a mysql table of users with a date field for their birth dates. How would I go about selecting users who's birthday is today? I was looking at EXTRACT() but I can't figure out how to use it to do what I want. Would this be a job for regex?
Code: Select all
function getBirthdays($date = null)
{
if (is_null($date))
{
$date = time();
}
$birthdate = getdate($date);
$query = "
SELECT
username, DATE_FORMAT( birth_date, '%m-%d' ) AS bday
FROM
users
WHERE
MONTH( birth_date ) = '" . $birthdate['month'] . "'
AND
DAYOFMONTH( birth_date ) = '" . $birthdate['mday'] . "'
";
// perform query, etc.
}Nah, I've got a better way
No need to use any PHP variables, just let MySQL do the work.[/syntax]
Code: Select all
$query = "
SELECT
username, DATE_FORMAT( birth_date, '%m-%d' ) AS bday
FROM
users
WHERE
MONTH( birth_date ) = MONTH(NOW())
AND
DAY( birth_date ) = DAY(NOW())
";
// perform query, etc.