Search code examples
phpphp-carbon

php carbon between dates inclusive


I want to get boolean result between two dates inclusively and here's my code.

$now = Carbon::now();
$now = 2021-09-29 00:00:00

dd( $now->between('2021-09-29 08:00:00', '2021-10-05 23:59:59') );

this would returns false;

Always got false value when trying to dump.Is there a way to make carbon date gives inclusive inclusive results so that it includes range between first and second date.

Basically I want to get all dates between 2021-09-29 00:00:00 and 2021-10-05 23:59:59 and I want to test whether 2021-09-29 08:00:00 is inside between that dates. I also want carbon gives inclusive results between 2021-09-29 00:00:00 and 2021-10-05 23:59:59


Solution

  • Going by your comment:

    Basically I want to get all dates between 2021-09-29 00:00:00 and 2021-10-05 23:59:59 and I want to test whether 2021-09-29 08:00:00 is inside between that dates

    The typical way you would do this would be:

    dd( $now->gte('2021-09-29') && $now->lt('2021-10-06') )
    

    This corresponds to the following raw SQL:

    WHERE NOW() >= '2021-09-29' AND NOW() < '2021-10-06'
    

    Note that the second comparison includes the entire day of 2021-10-05.