How would I get the birthdays of friends who are celebrating their birthday this week, this month and next month using MYSQL and PHP?

前端 未结 2 1419
旧巷少年郎
旧巷少年郎 2020-12-10 23:48

I am currently working in an site which needs to get the birthdays of friends who are celebrating their birthday this week, this month and next month using MYSQL and PHP.

2条回答
  •  醉梦人生
    2020-12-10 23:57

    Provided you store birthdates in DATE (or DATETIME) format in MySQL you can use the following Queries:

    // This week
    SELECT * FROM person WHERE WEEK( birthdate ) = WEEK( NOW() )
    
    // This month
    SELECT * FROM person WHERE MONTH( birthdate ) = MONTH( NOW() )
    
    // Next month
    

    SELECT * FROM person WHERE MONTH( birthdate ) = MONTH( NOW() ) + 1;

    SELECT * FROM person WHERE IF
      ( MONTH( NOW() ) < 12, MONTH( birthdate ) = MONTH( NOW() ) + 1,
      MONTH( birthdate ) = 1)
    

提交回复
热议问题