Getting random date between two dates

senty picture senty · Jan 19, 2017 · Viewed 8.3k times · Source

Is there a way I can get a random date between two dates in Carbon? For example, I am trying to get a random date between now and 55 mins ago.

$dateNow = Carbon::now();
$date25MinsAgo = Carbon::now()->subMinutes(55);

However, I am stuck at this point. I found some info on php, but I want to use 'now' as it's a seeder. What should I do?

Answer

Alexey Mezenin picture Alexey Mezenin · Jan 19, 2017

Use rand():

 $random = Carbon::now()->subMinutes(rand(1, 55));