PHP Carbon, Get All Dates Between Date Range

PHP Carbon, get all dates between date range?

As of Carbon 1.29 it is possible to do:

$period = CarbonPeriod::create('2018-06-14', '2018-06-20');

// Iterate over the period
foreach ($period as $date) {
echo $date->format('Y-m-d');
}

// Convert the period to an array of dates
$dates = $period->toArray();

See documentation for more details: https://carbon.nesbot.com/docs/#api-period.

CARBON Get all dates between date range using number of the weekday

Check this Code:

function weekDaysBetween($requiredDays, $start, $end){
$startTime = Carbon::createFromFormat('d-m-Y', $start);
$endTime = Carbon::createFromFormat('d-m-Y', $end);

$result = [];

while ($startTime->lt($endTime)) {

if(in_array($startTime->dayOfWeek, $requiredDays)){
array_push($result, $startTime->copy());
}

$startTime->addDay();
}

return $result;
}

And you can call it like:

weekDaysBetween([1,2, 3], "01-09-2021", "01-10-2021")

And the reult would be like:

[
"2021-09-01T22:02:21.000000Z",
"2021-09-06T22:02:21.000000Z",
"2021-09-07T22:02:21.000000Z",
"2021-09-08T22:02:21.000000Z",
"2021-09-13T22:02:21.000000Z",
"2021-09-14T22:02:21.000000Z",
"2021-09-15T22:02:21.000000Z",
"2021-09-20T22:02:21.000000Z",
"2021-09-21T22:02:21.000000Z",
"2021-09-22T22:02:21.000000Z",
"2021-09-27T22:02:21.000000Z",
"2021-09-28T22:02:21.000000Z",
"2021-09-29T22:02:21.000000Z"
]

Laravel Carbon, how do I get all the dates between two date ranges?

You can indeed use CarbonPeriod for this.

$period = CarbonPeriod::create('2019-05-30', '2019-06-03');
$period->toArray();

This should already return the desired result for you.
Check the docs for some nice features and options such as excluding start/end dates if needed.

PHP: Return all dates between two dates in an array

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

To iterate

foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}

Laravel: How can I get all dates between date ranges and all dates within date ranges separately?

As you have solved your first question. I'm now trying to help u out for the last one. Again there are so many ways to solve it, the easy ( though not smart but floats the boat ;) ) way:

You can simply slice the first and the last element from your date array. usually , you may use array shift and pop

array_shift($all_dates);
array_pop($all_dates);

you can use array_slice as well but I think shift and pop is better than slice. it performs better.

How to get array of dates between two dates in laravel?

You can use CarbonPeriod for this.

I found something helpful at https://stackoverflow.com/a/50854594/13642447.

How to get all between two dates by carbon

Try this:

$from = Carbon::parse('2016-10-01');
$to = Carbon::parse('2016-11-05');

With Carbon

$dates = [];

for($d = $from; $d->lte($to); $d->addDay()) {
$dates[] = $d->format('Y-m-d');
}

return $dates;

php carbon between dates inclusive

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.



Related Topics



Leave a reply



Submit