简体   繁体   中英

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:

$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($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. Just as an example.

Carbon has a between() method. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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