Hi all, I have google a bit but I haven't what I'm looking for, so better to try with DP 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
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
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.
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?
Leading zeros can be easily added using inbuilt PHP str_pad function. echo str_pad($randomnumber, 3, "0", STR_PAD_LEFT); PHP: