one random number per day

Discussion in 'PHP' started by smartknight, Jan 9, 2010.

  1. #1
    Hey, I just wanted to ask if anybody can show me how i can create a random number that doesnt change for 24hours, I know i could use rand() but that means it changes every refresh, I could also use a cookie and set it to expire in 24hours but is there a way to do it just using php? I need it for my site where the user sees the same message for a day and a new one next day.
     
    smartknight, Jan 9, 2010 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #2
    u want a random number per person/per day or per day for all users ?
     
    Bohra, Jan 9, 2010 IP
  3. spiderman3

    spiderman3 Peon

    Messages:
    177
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Best Solution is Firstly You may create cookie and set expire date is 24 hour and than use rand function with if condition coz their is no other way to known the session base of user.
     
    spiderman3, Jan 10, 2010 IP
  4. Asgeco

    Asgeco Member

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    You could do something like this.

    srand(date("Ymd"));
    echo rand();

    Each day the date will be different so the random number also will be different. You could do even like this.

    srand(date("Ymd"));
    echo rand(1,50);
     
    Last edited: Jan 10, 2010
    Asgeco, Jan 10, 2010 IP
  5. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    - first generate the random number and current time.
    - save the generated number and time on text file.
    - then read the random number from the text file till the saved time exceeds the 24 hours.
    if the time exceeds then update the text file info to new random number. if not keep reading the current value from text file.
     
    xenon2010, Jan 10, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would use Asgeco's method
     
    JAY6390, Jan 10, 2010 IP
  7. smartknight

    smartknight Active Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #7
    Thanks all for the solution !
     
    smartknight, Jan 10, 2010 IP