def random_date_in_period(period)
rand(period).seconds.ago
end
random_date_in_period 1.year #=> Tue, 22 May 2018 04:55:54 UTC +00:00
random_date_in_period 1.year #=> Mon, 06 Nov 2017 19:50:07 UTC +00:00
random_date_in_period 1.week #=> Sat, 16 Jun 2018 18:07:31 UTC +00:00
random_date_in_period 1.week #=> Wed, 13 Jun 2018 19:02:37 UTC +00:00
Kudos to Marc O'Morain.
Source: https://stackoverflow.com/a/4886512/6212572