SteenhouwerD
SteenhouwerD

Reputation: 1807

Mysql date and rails dates with utc

We want to get all the people that are born on a certain date. For the moment we use the query like this :

User.where(["day(birthdate) = day(?) AND month(birtdate) = month(?)", Time.now.utc, Time.now.utc ])

Now when we safe a user with date 29/03/1989 in the form, the field birthdate in the db contains this : 28/03/1989 22:00:00 UTC+2

The problem is that mysql will take 28 as day instead of 29 when we use the day() function of mysql.

How can I fix this?

Upvotes: 2

Views: 527

Answers (2)

0x4a6f4672
0x4a6f4672

Reputation: 28245

Maybe it is helpful to set the right time zone in environment.rb (Rails 2.x) or application.rb (Rails 3.x), for example config.time_zone = "Europe/Berlin"? Active Record should auto-convert the time to this zone.

Upvotes: 1

0x4a6f4672
0x4a6f4672

Reputation: 28245

What kind of MySQL data type do you use to store the birthday attribute? It probably helps to store only the date (not the time) by choosing the data type date instead of datetime.

Upvotes: 1

Related Questions