[英]Find every [ 1st,2nd,3rd ] day of week using carbon For next 15 day
Find every [ 1st,2nd,3rd ]day's date of week using carbon
For next 15 day.使用carbon
在接下来的 15 天中查找每个 [1st,2nd,3rd] 天的日期。
Is there any method similar to momentjs
in Carbon Carbon中有没有类似momentjs
的方法
Below showcase same example using momentjs
.下面展示了使用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我认为您需要这部分文档: 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工作日 go 从 0 到 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 .我不认为 Carbon 具有开箱即用的功能,但使用像这样的出色工具,您可以使用CarbonPeriod
来实现这一点,请参阅: 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
$days
给你碳周期,它是一个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)您可以简单地将$carbon
作为循环内的 Carbon 实例返回,而不是对其进行格式化(我只是为了演示它返回星期几)PS: By default, 0 is Sunday in Carbon, 1 is Monday...and so on. PS:默认情况下,在 Carbon 中 0 是星期日,1 是星期一……以此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.