SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    get a rand created time between 2012-10-1 and now.

    i will use DATE_ADD,now if i want to get a rand time from 2012-10-1 to now,how should i do?

    Code:
      created=DATE_ADD( DATE_ADD( DATE_ADD( DATE_ADD( DATE_ADD( DATE_ADD(
             NOW(), 
             INTERVAL 1 YEAR),
             INTERVAL 11*RAND() MONTH),
             INTERVAL 30*RAND() DAY),
             INTERVAL 23*RAND() HOUR),
             INTERVAL 59*RAND() MINUTE),
             INTERVAL 60*RAND() SECOND)
    when i use the above code ,it get time isn't what i want. thank you.how to correct it?

    eg: 30*RAND(3) what's the value will be?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    so you want a datetime, not a time, right?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT @secs := ROUND(RAND() * (UNIX_TIMESTAMP() - UNIX_TIMESTAMP('2012-10-01'))); 
    
    SELECT '2012-10-01' + INTERVAL @secs SECOND;
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •