简体   繁体   English

使用碳查找一周中的每一天 [1st,2nd,3rd] 对于接下来的 15 天

[英]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
  • Allows you to iterate over each 'period' between the two dates允许您遍历两个日期之间的每个“期间”
  • You then do the check when iterating them while populating your initial array然后在填充初始数组时迭代它们时进行检查
  • You can simply return $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.

相关问题 如何确定星期几在一个月中的第一,第二,第三等发生? - How to determine if day of week is 1st, 2nd, 3rd, etc occurrence in a month? PHP日期和时间,在一个月内获得第1周,第2周,第3周和第4周 - PHP Date and Time, getting the 1st, 2nd, 3rd, and 4th week in a month 在php中显示当前星期的1、2、3、4和5日 - Display the 1st, 2nd, 3rd, 4th, & 5th date of the current week in php st、nd、rd、th 格式的 PHP Carbon 格式化日 - PHP Carbon formatting day in st, nd, rd, th format 如何改变 <option>第3个<select>基于第二次变化<select> ,其中第二个是通过使用jquery ajax更改第一个下拉列表的值来绘制的 - How to change <option> of a 3rd <select> based on change in 2nd <select>, where 2nd is drawn by changing value of 1st dropdown using jquery ajax 我想在考试成绩中显示第 1、2、3 位 - I want to show position 1st, 2nd, 3rd in exam result MySQL - mysql日期格式后缀(第1,第2,第3 ......) - MySQL - mysql date format suffix (1st, 2nd, 3rd…) 从第2和第3个表中获取第1个表中匹配值的值 - Get the values from 2nd and 3rd table for the matched values in 1st table 在代码点火器中显示前10个结果,并显示第1,第2和第3个最高值 - Display top 10 results in codeigniter and display 1st, 2nd and 3rd top values 循环的第1次和第3次迭代 - Every 1st and 3rd Iteration of a Loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM