0

I'm trying to retrieve all the entries of the day in my model:

where('date(created_at) = date(?)', Date.today)

But, because of the timezone (My local time is UTC+2), I don't have the expected result. For example, if an entry has been created one minute after midnight, in the database, it will be stored at 22:01 (so not "Date.today").

Is there a way to do that ?

EDIT:

As the admin of the website, I want to display the number of the entries on my local time "today" (It's only for statistics purpose!). So I want to keep timestamps store in utc, but convert them to my local timezone during this request!

Mathieu Mahé
  • 2,647
  • 3
  • 35
  • 50

3 Answers3

0

You can set the correct time zone in several ways. One is described in this answer:

https://stackoverflow.com/a/6118837/567126

adding following to application.rb works

config.time_zone = 'Eastern Time (US & Canada)'

Community
  • 1
  • 1
spaudanjo
  • 764
  • 7
  • 13
0
where('date(created_at) = ?', Time.zone.now.to_date)
house9
  • 20,359
  • 8
  • 55
  • 61
0

Time in the database should be stored as UTC. This way, when you do a worldwide website, you can serve each user with their own timezone.

It's the UI (views) responsibility to show the time in the user's local timezone.

For your where you should use Time.now.utc This way, you are searching the DB with the common UTC.

Chen Fisher
  • 1,405
  • 12
  • 12