Posted By

mepatterson on 11/10/09


Tagged

mysql time date rails ruby between


Versions (?)

generate a mysql BETWEEN clause to find within a date range


 / Published in: Rails
 

requires Rails libraries. could be rewritten to just use core Ruby libs, but I'm too lazy right now and I use this entirely in my Rails apps.

  1. def mysql_date_compare(start_date, end_date, col_name = :created_at)
  2. range_start = (Date.parse(start_date.to_s) - 1.day).strftime("%Y-%m-%d 23:59:59")
  3. range_end = (Date.parse(end_date.to_s) + 1.day).strftime("%Y-%m-%d 00:00:00")
  4. "#{col_name.to_s} BETWEEN '#{range_start}' AND '#{range_end}'"
  5. end

Report this snippet  

You need to login to post a comment.