0

I have stored in the database in a varchar column the birthdays like this

dd/mm/YYYY

How can I select the birthday people from the current month directly from MySQL query??

And show using PHP

Thanks

Atul Baldaniya
  • 761
  • 8
  • 14

7 Answers7

10

First, do not store dates as a VARCHAR. Convert it to a DATE.

Once that's fixed, use one of the many MySQL date time functions:

SELECT * FROM users WHERE MONTH(birthday) = MONTH(NOW());
Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
9
SELECT
    *
FROM
    yourtable
WHERE
    MONTH(STR_TO_DATE(yourdatefield, '%d/%m/%Y')) = MONTH(NOW())
Toretto
  • 4,721
  • 5
  • 27
  • 46
user4035
  • 22,508
  • 11
  • 59
  • 94
2

Assuming date is stored in %m/%d/%Y this format you can change this format according to your need.

and %m we are selecting only the month and comparing it to the current month MONTH(NOW()).

Replace DOB by your column and table by your table name

select * from table
  where date_format(str_to_date(DOB, '%m/%d/%Y'), '%m') = MONTH(NOW());; 
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
1

You should change your column type to DATE. e.g.

ALTER TABLE `people` CHANGE  `dob` `dob` DATE NOT NULL;

By doing so you can then use the MySQL query date functions to filter the results.

SELECT * FROM people WHERE MONTH(dob) = MONTH(CURDATE()) AND YEAR(dob) = YEAR(CURDATE())
AlexP
  • 9,906
  • 1
  • 24
  • 43
  • the year part is only neccessary when you want someone born in a specific year and month, not just month – Garytje Jul 26 '13 at 11:28
  • @Garytje I interpreted the "current month" as the month we are currently in (meaning this year) – AlexP Jul 26 '13 at 11:31
1
For Get User list whose birthday in current month in mysql if field datatype is date 
        $Toaday = '%'.date('-m-').'%';

        $query = " select * from client where birth_date LIKE '$Toaday' ";

    In your case declare $Today = '%'.date('/m/').'%';
manoj singh
  • 338
  • 2
  • 9
0

As you used a VARCHAR field instead a DATE field,

you have to cast the value and then use the normal date functions. like

SELECT * 
   FROM table_name 
   WHERE MONTH(CAST(col_name AS DATE)) = MONTH(NOW());
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
0

I have store dates as a timestamp in tables so I create a query like this. and it's working fine.

don't use varchar for a date.

select first_name, last_name, date_format(FROM_UNIXTIME(`dateofbirth`), '%m/%d/%Y') as dob from users where date_format(FROM_UNIXTIME(`dateofbirth`), '%m') = MONTH(NOW())
Atul Baldaniya
  • 761
  • 8
  • 14