x-ray
x-ray

Reputation: 123

Randomising a past date from today, but not going past a set date

This is what I am trying to achieve. Setting a past date of -3 days from today, but having this rotate between -3, -2 or -1 days, but never going back in the past. E.g.

Today: 08/02/2022 Result of -3 days: 05/02/22

Next day: 09/02/2022 Result of -2 days: 07/02/22

etc

$today           = strtotime(date('d-m-Y'));
$maxDaysInPast = strtotime(date('d-m-Y', strtotime('-3 day')));

$result = date('d/m/Y', mt_rand($threeDaysInPast, $today));

var_dump($result);

I am trying to achieve a random past date for each day, but not go past -3 days.

Upvotes: 0

Views: 28

Answers (1)

Don't Panic
Don't Panic

Reputation: 41810

If you're only concerned with the day part, it would be simpler to just randomize the number you pass to strtotime() instead.

$result = date('d/m/Y', strtotime('-' . mt_rand(1, 3) . ' day'));

Upvotes: 1

Related Questions