I am attempting to find ‘entries’ create today (in my current timezone) using the following:
Entry.where("DATE(created_at) = DATE(?) AND email = ?", Time.now, email)
Time.now is giving me the time in the current zone, but the query seems like it is searching against the UTC time of each entry’s created_at column.
Any ideas how I can find entries created today in the server’s timezone?
You’re losing track of the timezone information as soon as you call the
DATE()function in your SQL. There are two things to keep in mind:DATE().Something like this should work:
The times should be converted to UTC automatically. You might want to handle the upper bound a little differently; using
end_of_daygives you23:59:59so there is a little room for something to slip through that you want to capture. You can get around that problem using an explicit half-open interval (rather than the closed interval that SQL’sbetweenuses) like this: