获取两个日期之间的随机日期
Getting random date between two dates
有没有办法在 Carbon 中获取两个日期之间的随机日期?例如,我试图获取从现在到 55 分钟前的随机日期。
$dateNow = Carbon::now();
$date25MinsAgo = Carbon::now()->subMinutes(55);
然而,我卡在了这一点上。我在 php 上找到了一些信息,但我想使用 'now',因为它是播种机。我该怎么办?
使用rand()
:
$random = Carbon::now()->subMinutes(rand(1, 55));
要获取去年的随机日期:
$random = Carbon::today()->subDays(rand(0, 365));
使用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
您也可以使用 rand()
,但我认为使用
加密安全函数。
有没有办法在 Carbon 中获取两个日期之间的随机日期?例如,我试图获取从现在到 55 分钟前的随机日期。
$dateNow = Carbon::now();
$date25MinsAgo = Carbon::now()->subMinutes(55);
然而,我卡在了这一点上。我在 php 上找到了一些信息,但我想使用 'now',因为它是播种机。我该怎么办?
使用rand()
:
$random = Carbon::now()->subMinutes(rand(1, 55));
要获取去年的随机日期:
$random = Carbon::today()->subDays(rand(0, 365));
使用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
您也可以使用 rand()
,但我认为使用
加密安全函数。