简体   繁体   English

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

[英]what date format is 2022-07-24T19:00:00.000Z?

I have this code running in my Kernel.php to blast an email at a set date:我在我的 Kernel.php 中运行此代码,以在设定的日期爆破 email:

$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;
        });

I realized that the format of $currentDate is Ymd, while the LAUNCH_COUNTDOWN I set is 2022-07-24T19:00:00.000Z.我意识到 $currentDate 的格式是 Ymd,而我设置的 LAUNCH_COUNTDOWN 是 2022-07-24T19:00:00.000Z。 is this going to run successfully, or do I have to change the currentDate format?这会成功运行,还是我必须更改 currentDate 格式? Thanks in advance.提前致谢。

You can use Carbon to parse it properly:您可以使用Carbon正确解析它:

$schedule->call(function() {

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

This way the heavy lifting is done by Carbon.这样,繁重的工作就由 Carbon 完成了。 Note that this function will be called hourly till eternity after the given date.请注意,这个 function 将在给定日期之后每小时调用一次,直到永恒。

Also, you shouldn't be calling env() in your code manually, as that function is designed to be called only in config files in [project]/config/someconfig.php so that it can be cached using php artisan config:cache . Also, you shouldn't be calling env() in your code manually, as that function is designed to be called only in config files in [project]/config/someconfig.php so that it can be cached using php artisan config:cache . I know it is an easy thing to do but it's nicer to use something like:我知道这是一件容易的事,但使用类似的东西会更好:

file: config/project.php :文件: config/project.php

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

You then call app('project.launch_countdown') which defaults to false when the env variable is not set.然后调用app('project.launch_countdown') ,当未设置 env 变量时默认为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM