How to generate random date between two dates using php?

前端 未结 13 1964
甜味超标
甜味超标 2020-11-27 15:55

I am coding an application where i need to assign random date between two fixed timestamps

how i can achieve this using php i\'ve searched first but only found the a

13条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 16:28

    By using carbon and php rand between two dates

    $startDate = Carbon::now();
    $endDate   = Carbon::now()->subDays(7);
    
    $randomDate = Carbon::createFromTimestamp(rand($endDate->timestamp, $startDate->timestamp))->format('Y-m-d');
    

    OR

    $randomDate = Carbon::now()->subDays(rand(0, 7))->format('Y-m-d');
    

提交回复
热议问题