2022-07-24T19:00:00.000Z的日期格式是什么



我在Kernel.php中运行了以下代码,以便在指定日期发送电子邮件:

$schedule->call(function () {BlastEmail::blastAll();})->hourly()->when(function() {
$targetDate = strtotime(ENV('LAUNCH_COUNTDOWN'));
$currentDate = strtotime(date("Y-m-d"));
if($currentDate >= $targetDate) {
return true;
}
return false;
});

我意识到$currentDate的格式是Y-m-d,而我设置的LAUNCH_COUNTDOWN是2022-07-24T19:00:00.000Z。这会成功运行吗,还是我必须更改currentDate格式?提前谢谢。

您可以使用Carbon正确解析它:

$schedule->call(function() {
})->hourly()->when(function() {
$launchDate = Carbon::parse(env('LAUNCH_COUNTDOWN'));
return Carbon::now()->greaterThan($launchDate);
});

通过这种方式,重型吊装由Carbon完成。请注意,此函数将在给定日期后每小时调用一次,直到永恒。

此外,您不应该在代码中手动调用env(),因为该函数设计为仅在[project]/config/someconfig.php中的配置文件中调用,因此可以使用php artisan config:cache缓存它。我知道这是一件容易的事情,但使用这样的东西更好

文件:config/project.php:

<?php
return [
'launch_countdown' => env('LAUNCH_COUNTDOWN', false),
];

然后调用app('project.launch_countdown'),在未设置env变量时默认为false

最新更新