Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user's birthday based on day and month only

I want to have a member list for today's birthday and upcoming birthday(tommorow).

In users.controller.rb
def birthday
   @birthday_users = User.all.order("created_at DESC").where(:birthday => Date.today)
end

def upcoming_birthday
   @upcoming_birthday_user = User.all.order("created_at DESC").where(:birthday => 1.days.from_now )
end

These codes work only when the day, month and year are the same as today.

Example:

User 1's birthday = October 3 2018 (Showing on the birthday list)

User 2's birthday = October 3 2000 (Not showing on the birthday list)

like image 660
Garbela Avatar asked Sep 05 '25 03:09

Garbela


1 Answers

In User model

PostgreSql

scope :today_birthday, { where('EXTRACT(month FROM birthday) = ? AND EXTRACT(day FROM birthday) = ?', Date.today.month, Date.today.day).order("created_at DESC") }

scope :upcoming_birthday, { where('EXTRACT(month FROM birthday) = ? AND EXTRACT(day FROM birthday) = ?', 1.days.from_now.month, 1.days.from_now.day).order("created_at DESC") }

MySql

scope :today_birthday, { where('MONTH(birthday) = ? AND DAY(birthday) = ?', Date.today.month, Date.today.day).order("created_at DESC") }

scope :upcoming_birthday, { where('MONTH(birthday) = ? AND DAY(birthday) = ?', 1.days.from_now.month, 1.days.from_now.day).order("created_at DESC") }
like image 87
Anand Avatar answered Sep 07 '25 23:09

Anand