Find every [ 1st,2nd,3rd ]day's date of week using carbon
For next 15 day.
Is there any method similar to momentjs
in Carbon
Below showcase same example using momentjs
.
momementObj.recur().every([1, 2, 3, 4, 5]).daysOfWeek();
I think you need this part of the documentation: https://carbon.nesbot.com/docs/#api-week
So for example for one week:
$en = CarbonImmutable::parse('2017-02-05');
var_dump($en->week(1)->format('Y-m-d H:i')); //string(16) "2017-01-01 00:00"
var_dump($en->week(6)->format('Y-m-d H:i')); //string(16) "2017-01-06 00:00"
check the documentation its clear, please if you need futher clarification just comment.
Week days go from 0 to 6
I don't think Carbon has this out-of-the-box but with great tools like this you can use CarbonPeriod
to achieve this, see: https://carbon.nesbot.com/docs/#api-period . You can end up with something like this:
use Carbon\CarbonPeriod;
...
function dateInRange(array $days_of_week = [1,2,3], int $in_days = 15)
{
$days = CarbonPeriod::create(Carbon::now(), Carbon::now()->addDay($in_days));
$result = [];
/**
* @var Carbon $carbon
*/
foreach ($days as $carbon) { //This is an iterator
if (in_array($carbon->dayOfWeek, $days_of_week, true)) {
$result[] = $carbon->format('Y-m-d D');
}
}
return $result;
}
$days
gives you Carbon period which is an Iterator
$carbon
as a Carbon instance inside the loop instead of formatting it (I just did it to demo that it returns the day of the week)PS: By default, 0 is Sunday in Carbon, 1 is Monday...and so on.
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.