Getting random date between two dates
Use random_int()
:
use Carbon\Carbon;
$upTo55MinsAgo = Carbon::now()->subMinutes(random_int(0, 55));
(PHP 7, PHP 8) random_int — Generates cryptographically secure pseudo-random integers
You can also use rand()
, but I think it's good practice to use the
cryptographically secure function.
Use rand()
:
$random = Carbon::now()->subMinutes(rand(1, 55));
To get a random date in the last year:
$random = Carbon::today()->subDays(rand(0, 365));