Random MySQL date
Random date/time
Need to generate a random date in MySQL? Here:
mysql> SELECT FROM_UNIXTIME(RAND() * 2147483647) AS `rand`; +---------------------+ | rand | +---------------------+ | 1998-04-01 21:42:48 | +---------------------+ 1 row in set (0.00 sec)
Let's try once again and get a different random value:
mysql> SELECT FROM_UNIXTIME(RAND() * 2147483647) AS `rand`; +---------------------+ | rand | +---------------------+ | 2028-03-21 22:44:43 | +---------------------+ 1 row in set (0.00 sec)
OK, why 2147483647?
Well, 2147483647 is the latest timestamp:
mysql> SELECT FROM_UNIXTIME(2147483647) AS `rand`; +---------------------+ | rand | +---------------------+ | 2038-01-18 19:14:07 | +---------------------+ 1 row in set (0.00 sec)
If you try one more second (2147483648), that's too much:
mysql> SELECT FROM_UNIXTIME(2147483648) AS `rand`; +------+ | rand | +------+ | NULL | +------+ 1 row in set (0.01 sec)
So RAND() gives you a random value between 0 and 1 and you multiply it with the largest value posible and this way you get a random date between 1969 and 2038.
What if you want to generate random date in a defined period?
A random date in 2009
Here goes:
mysql> SELECT FROM_UNIXTIME(RAND() * (1262246400 - 1230796800) + 1230796800) AS `the_date`; +---------------------+ | the_date | +---------------------+ | 2009-04-27 05:01:04 | +---------------------+ 1 row in set (0.00 sec)
And once again:
mysql> SELECT FROM_UNIXTIME(RAND() * (1262246400 - 1230796800) + 1230796800) AS `the_date`; +---------------------+ | the_date | +---------------------+ | 2009-08-15 12:54:51 | +---------------------+ 1 row in set (0.01 sec)
"What just happened?"
When you want to limit the values in a range, say between start and end and you have a random value between 0 and 1 you'd need to subtract end - start which gives you the desired interval and then "move" the interval to the start (the beginning of the range). So the super-duper complicated formula is like:
(end - start) * RAND + start
And because you cannot possibly remember timestamps, UNIX_TIMESTAMP comes to the rescue:
mysql> SELECT
-> UNIX_TIMESTAMP('2009-01-01') AS start,
-> UNIX_TIMESTAMP('2009-12-31') AS end;
+------------+------------+
| start | end |
+------------+------------+
| 1230796800 | 1262246400 |
+------------+------------+
1 row in set (0.00 sec)
Thanks
Thanks for reading!
This entry was posted on Thursday, January 29th, 2009 and is filed under mysql. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

January 29th, 2009 at 2:14 pm
[...] here to see the original: Random MySQL date Share and [...]
February 12th, 2009 at 1:35 am
[...] Random MySQL date [...]
November 9th, 2011 at 2:11 am
I’ve learn several just right stuff here. Certainly worth bookmarking for revisiting. I wonder how a lot attempt you place to create this kind of magnificent informative site.
November 17th, 2011 at 9:28 pm
Mack Tactics…
[...]Random MySQL date / Stoyan’s phpied.com[...]…
November 20th, 2011 at 5:19 pm
top resorts in kerala…
[...]Random MySQL date / Stoyan’s phpied.com[...]…
November 23rd, 2011 at 7:57 am
google adwords…
[...]Random MySQL date / Stoyan’s phpied.com[...]…
December 12th, 2011 at 7:15 am
[...] more information about all those “strange” numbers, visit Stoyan’s phpied.com. Posted on: December 12th, 2011 Author: delcastanher Filed under: [...]
December 21st, 2011 at 2:54 pm
Great solution! Just what I was looking for.
December 31st, 2011 at 4:09 pm
Bi Curious – try bi dating…
[...]Random MySQL date / Stoyan’s phpied.com[...]…
April 17th, 2013 at 1:30 pm
Thanks for ones marvelous posting! I definitely enjoyed reading it, you may be a great
author.I will be sure to bookmark your blog and will eventually come back someday.
I want to encourage you continue your great job, have a nice holiday weekend!
May 14th, 2013 at 3:05 am
Very great post. I just stumbled upon your blog and wished to mention that I’ve really loved browsing your weblog posts. In any case I will be subscribing to your feed and I’m hoping you write again very soon!
June 7th, 2013 at 9:34 am
I believe that is among the such a lot important info for me. And i’m happy studying your article. But should remark on some common issues, The website taste is ideal, the articles is in point of fact excellent : D. Just right activity, cheers