generate a mysql BETWEEN clause to find within a date range


/ Published in: Rails
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.