{"id":6723,"date":"2016-02-26T20:54:06","date_gmt":"2016-02-26T12:54:06","guid":{"rendered":"http:\/\/onthe8spot.com\/?p=6723"},"modified":"2016-02-26T20:54:06","modified_gmt":"2016-02-26T12:54:06","slug":"mysql-insert-random-datetime-in-a-given-datetime-range","status":"publish","type":"post","link":"http:\/\/onthe8spot.com\/index.php\/2016\/02\/26\/mysql-insert-random-datetime-in-a-given-datetime-range\/","title":{"rendered":"Mysql insert random datetime in a given datetime range"},"content":{"rendered":"<p>Found this nice sql.<br \/>\nI needed to randomize a date for the random data I was using to test a batch job I was fixing. \u00a0I would have used several excellent random data generators available online except my internet sucks so have to do this on my own.<\/p>\n<blockquote><p>This works perfectly even for leap years:select from_unixtime( unix_timestamp(&#8216;2000-1-1&#8217;) + floor( rand() * ( unix_timestamp(&#8216;2010-12-31&#8217;) &#8211; unix_timestamp(&#8216;2000-1-1&#8217;) + 1 ) ))The idea is simple: Just take a random timestamp between the two timestamps, then convert it to a datetime using from_unixtime. This way you can ensure that each option has equal probability.<\/p><\/blockquote>\n<p>Source: <em><a href=\"http:\/\/stackoverflow.com\/questions\/11906572\/mysql-insert-random-datetime-in-a-given-datetime-range\/28944156\">sql &#8211; Mysql insert random datetime in a given datetime range &#8211; Stack Overflow<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Found this nice sql. I needed to randomize a date for the random data I was using to test a batch job I was fixing. \u00a0I would have used several excellent random data generators available online except my internet sucks so have to do this on my own. This works perfectly even for leap years:select &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/onthe8spot.com\/index.php\/2016\/02\/26\/mysql-insert-random-datetime-in-a-given-datetime-range\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Mysql insert random datetime in a given datetime range&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[79],"tags":[],"class_list":["post-6723","post","type-post","status-publish","format-standard","hentry","category-reposts"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/posts\/6723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/comments?post=6723"}],"version-history":[{"count":0,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/posts\/6723\/revisions"}],"wp:attachment":[{"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/media?parent=6723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/categories?post=6723"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/onthe8spot.com\/index.php\/wp-json\/wp\/v2\/tags?post=6723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}