简体   繁体   English

在 php 检查项目是否在给定的日期范围内

[英]In php check if an item is between a given date range

I'm suck on the logic for this, my goal is to execute if an item is with in a given date range.我对此的逻辑很烂,我的目标是在一个项目在给定的日期范围内时执行。

I get yesterday's date and the date from 8 days ago with Carbon like so:我用 Carbon 得到昨天的日期和 8 天前的日期,如下所示:

$dt = \Carbon\Carbon::yesterday();
$dtB = \Carbon\Carbon::yesterday()->subDays(8);

$today = $dt->toDateString();
$todayBack = $dtB->toDateString();

I then need to execute this if statement to find if the item in the database fits with in these time frames.然后我需要执行这个 if 语句来查找数据库中的项目是否适合这些时间范围。

    if($orderSet->item_clicked == 'printing' && $orderSet->completed_date == $today) {
      // run some stuff here
    }

Currently I can execute if it's today but I would like do in between these two days.目前我可以在今天执行,但我想在这两天之间执行。 In example.例如。 09-20-19 - 10-09-19 in between these two dates. 09-20-19 - 10-09-19 在这两个日期之间。 Just as an example.举个例子。

Carbon has a between() method. Carbon 有一个between()方法。 Use the original carbonized dates instead of the date strings.使用原始碳化日期而不是日期字符串。

$dtCompleted = \Carbon\Carbon::parse($orderSet->completed_date);
if (if($orderSet->item_clicked == 'printing' && $dtCompleted->between($dtB, $dt)) {
    // run some stuff
}

See the documentation of Comparison查看比较的文档

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

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