I'm trying to make a simple function, that echos a random element from an array, but it should change, every time that day changes. Example:
$myarray = array(
'foo',
'bar',
'winnie',
'the',
'poo');
echo array_rand($myarray);
This would print a random thing from the array, every time the page loads. But I would like for it to only change each day. I would like for it to work, so if you load the page Monday at 8:00 and Monday at 17:00, then you would see the same (random) element, from the array. But if the page was loaded at Tuesday at 13:00, then a different element from the array would be printed.
I thought about getting integer-value of the date, and then use modulo to the length of the array (since the length of the array will get more and more values with time). Something along the lines of echo $myarray[date(U) % count($myarray)] (this hasn't been tested and wont work, since it's the second since 1970 (or whenever) and not the days, but it was just to give an idea of what kind of solution I had in mind).
There is no database on the site, so I can't store the value in a database.
Aucun commentaire:
Enregistrer un commentaire