1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to generate random time, pls?

Discussion in 'PHP' started by whosedomain, May 7, 2013.

  1. #1
    say i need random time between 01-01-1970 to 12-31-2012,
    how to do, pls?
    whosedomain, May 7, 2013 IP
  2. VPSNine

    VPSNine Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    how about this?
    echo date("Y/m/d",mt_rand(0,time()));
    VPSNine, May 7, 2013 IP
  3. VPSNine

    VPSNine Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    sorry the above us random to current days date, to end 2012it would be
    echo date("Y/m/d",mt_rand(0,13569048001));
    VPSNine, May 7, 2013 IP
  4. publishdaily

    publishdaily Greenhorn

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    how about random time from 01-01-1970 to current?
    how do you get 13569048001?
    publishdaily, May 7, 2013 IP
  5. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #5
    PHP:
    1.  
    2. $timestamp1 = strtotime('01-01-1970');
    3. $timestamp2 = strtotime('31-12-2012');
    4. echo "timestamp 1 (01-01-1970) : ".$timestamp1."<br />timestamp 2 (31-12-2012) : ".$timestamp2;
    5.  
    6. $rand_timestamp = rand(-3600, 1356908400);
    7. $rand_date = date('d-m-Y', $rand_timestamp);
    8. echo "<br />random date between 01-01-1970 and 31-12-2012 : ".$rand_date;
    9.  
    10. $now_timestamp = time();
    11. $rand_timestamp_till_now = rand(-3600, $now_timestamp);
    12. $rand_date_till_now = date('d-m-Y', $rand_timestamp_till_now);
    13. echo "<br />random date between 01-01-1970 and NOW : ".$rand_date_till_now;
    14.  
    dujmovicv, May 8, 2013 IP
  6. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #6
    simpler example:
    PHP:
    1. <?php
    2.  
    3. $timestamp = strtotime('2012-12-31');
    4.  
    5. // unix timestamp for 1970-01-01 is => 0
    6.  
    7. $result = rand(0, $timestamp);
    8.  
    9. print date('Y/m/d h:i:s', $result);
    10.  
    11. ?>
    Hamidsam, May 8, 2013 IP