2

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?

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Indeed, but if the date is 28/03/1989 22:00:00 UTC+2 in the DB, then the mysql function day(28/03/1989 22:00:00 UTC+2) will give back 28 and activerecord day(Time.now) 29 if we are 28/03/1989 22:00:00 UTC+2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.