Picture of the day - how to get a daily random number?

Discussion in 'PHP' started by Istvan, Oct 16, 2008.

  1. #1
    Hi all,

    I have google a bit but I haven't what I'm looking for, so better to try with DP :D

    How can I generate daily (not at every page refresh!) a random three digit (for example: 003, 047, 123...) number that then i can use as part of the image.

    <a href="photos/image-<?php echo $randomnumber; ?>.jpg" rel="lightbox"><img src="photos/thumb_image-<?php echo $randomnumber; ?>.jpg" /></a>
    Code (markup):
    How can I generate this "$randomnumber"?

    I have tried with rand but it gives me a different number at every refresh...

    Thanks
     
    Istvan, Oct 16, 2008 IP
  2. crivion

    crivion Guest

    Best Answers:
    0
    #2
    make a script that will be accessed with crontab one time per 24 hours and insert the given number into a file or a database and then get it from there
     
    crivion, Oct 16, 2008 IP
  3. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use rand() but set the seed as the current date. This will generate a random number that will stay the same for that day.

    $seed = (int)floor(time()/(24*60*60));
    srand($seed);
    $randomnumber = rand(999);
    PHP:
    $randomnumber will be 0, 1, 2 ... 999.

    If you need the leading zeros then use this function:

    function leading_zeros($value, $places){
    
        if(is_numeric($value)){
            for($x = 1; $x <= $places; $x++){
                $ceiling = pow(10, $x);
                if($value < $ceiling){
                    $zeros = $places - $x;
                    for($y = 1; $y <= $zeros; $y++){
                        $leading .= "0";
                    }
                $x = $places + 1;
                }
            }
            $output = $leading . $value;
        }
        else{
            $output = $value;
        }
        return $output;
    }
    PHP:
    Usage

    $randomnumber = leading_zeros(rand(999),3);
    PHP:
    $randomnumber will be 000, 001, 002 ... 999.
     
    Limotek, Oct 16, 2008 IP
    Istvan likes this.
  4. Istvan

    Istvan Well-Known Member

    Messages:
    1,544
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    175
    #4
    Thank Limotek, seems working fine. I will check it tomorrow :) +rep for you

    Just a question. From where it gets the time? The image will change at server's midnight or 24 hrs after first image request?
     
    Istvan, Oct 16, 2008 IP
  5. Ueland

    Ueland Peon

    Messages:
    66
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    time() is based on the servers clock. :)
     
    Ueland, Oct 16, 2008 IP
  6. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Limotek, Oct 16, 2008 IP
  7. FredL

    FredL Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Leading zeros can be easily added using inbuilt PHP str_pad function.
    
    echo str_pad($randomnumber, 3, "0", STR_PAD_LEFT);
    
    PHP:
     
    FredL, Oct 16, 2008 IP