How To Generate a Random Date/Time [using date() function] Within a Given Time Range

Discussion in 'PHP' started by techbongo, Jul 16, 2011.

  1. #1
    Hi Folks,

    I'm busy in making an article directory - article.b2bplanet.net. I need your help again.

    Please give me the piece of PHP code to generate a random time (Format - YYYY-MM-DD HH:MM:SS) within a given period of time.
    Suppose a random time within the range 2011-01-01 00:00:00 to 2011-05-01 12:34:00, i.e. I can define a max and min range of the random time just like mt_rand() function.
     
    techbongo, Jul 16, 2011 IP
  2. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    This thread can be set closed. I've done it myself and happy to share it:
    
    function rand_time($max, $min){
       return date("Y-m-d H:i:s", mt_rand(strtotime($min),strtotime($max)));
    }
    
    PHP:
     
    techbongo, Jul 16, 2011 IP