Paul Mason
Paul Mason

Reputation:

PHP - Consistent Random Output by the Day

I have some content on a website which I want to randomize every day. I want to keep that random output consistent for the entire day. So that if someone is going back and forth from the page they won't get lost.

I guess the key is to find a rand function that is consistent, and that uses todays date as a salt.

Any ideas?

Upvotes: 0

Views: 503

Answers (3)

Trav L
Trav L

Reputation: 15192

Instead of trying to use rand() function, you can simply manipulate with date value as @Soura suggested.

A different (and dummier) example can be:

$key = time() / (60*60*60); // a numeric key, increment by 1 per day.
$total_banner_count = 20;
$banner_id_to_display = $key / $total_banner_count;

Upvotes: 0

Ripred
Ripred

Reputation: 157

One thing to consider might be just taking the day of the year and doing a modulas of that against the array size of your set of available "random" outputs to select what to present the user. After all, to the user "random" is a relative term as long as the content is different from yesterday. You could randomize the values in your table of available outputs only once and then you can just index into them for each day. The output will be fresh and new for the users each day and you can avoid the constant re-hashing for each request.

Upvotes: 0

Sourav
Sourav

Reputation: 17530

$random_key=md5(date('y-m-s'));

or

$random_key=sha1(date('y-m-s'));

Upvotes: 3

Related Questions