How would I generate a random date that has to be between two other given dates?
The function's signature should something like this:
randomDate("1/1/2008 1:30 PM", "1/1/2009 4:50 AM", 0.34)
^ ^ ^
date generated has date generated has a random number
to be after this to be before this
and would return a date such as: 2/4/2008 7:20 PM
The easiest way of doing this is to convert both numbers to timestamps, then set these as the minimum and maximum bounds on a random number generator.
A quick PHP example would be:
This function makes use of
strtotime()
to convert a datetime description into a Unix timestamp, anddate()
to make a valid date out of the random timestamp which has been generated.This is a different approach - that sort of works..
BETTER APPROACH
You can Use
Mixer
,and,
refer
I made this for another project using random and time. I used a general format from time you can view the documentation here for the first argument in strftime(). The second part is a random.randrange function. It returns an integer between the arguments. Change it to the ranges that match the strings you would like. You must have nice arguments in the tuple of the second arugment.