从指定日期php / laravel检查当前日期是一天/一个月/一周

时间:2016-04-22 07:53:57

标签: php laravel date

所以我试图安排一个函数来每天/每周或每月调用。我有一个来自数据库的当前日期(格式为'DD-MM-YYYY HH:mm',如果有任何重要的话)。我想检查当前日期是否恰好是该日期的一天/一周/一个月。我是php / laravel的新手,所以我还不知道如何做到这一点,因此没有代码:(。

我当前日期的类型是DateTime

2 个答案:

答案 0 :(得分:2)

在php中,您可以使用DateTime

进行检查
$d1 = new DateTime();
$d2 = new DateTime();
$interval = $d2->diff($d1);
echo $interval->y; // count if year between dates
echo $interval->days; // count if days between dates

答案 1 :(得分:0)

Laravel中的任务调度> = 5.1 -
Laravel命令调度程序允许您使用易于理解的PHP语法管理任务执行日期和时间,并且您的服务器上只需要一个Cron条目。

您的任务计划是在app/Console/Kernel.php文件的计划方法中定义的。

当然,您可以为自己的任务分配各种时间表:

Method                      Description
===================================================
->cron('* * * * * *');      Run the task on a custom Cron schedule
->everyMinute();            Run the task every minute
->everyFiveMinutes();       Run the task every five minutes
->everyTenMinutes();        Run the task every ten minutes
->everyThirtyMinutes();     Run the task every thirty minutes
->hourly();                 Run the task every hour
->daily();                  Run the task every day at midnight
->dailyAt('13:00');         Run the task every day at 13:00
->twiceDaily(1, 13);        Run the task daily at 1:00 & 13:00
->weekly();                 Run the task every week
->monthly();                Run the task every month
->yearly();                 Run the task every year

有关详情,请参阅此链接 - https://laravel.com/docs/5.1/scheduling

希望这会对你有所帮助!!