一年的碳清单

发布于 2025-01-24 13:52:15 字数 951 浏览 0 评论 0原文

我每年都有回报周的功能:

function getWeek(){

    $today = \Carbon\Carbon::today();
    $date = $today->copy()->firstOfYear()->startOfDay();
    $eom = $today->copy()->endOfYear()->startOfDay();
  
    $dates = [];

    for($i = 1; $date->lte($eom); $i++){
        $startDate = $date->copy();
        while($date->dayOfWeek != \Carbon\Carbon::SUNDAY && $date->lte($eom)){
                $date->addDay(); 
            }
        
        $dates['w'.$i] = $startDate->format('d/m/Y') . ' - ' . $date->format('d/m/Y');
        $date->addDay();
    }
    
    return $dates;
}

功能在2022年的功能返回53周,但在2022年为52周。这是返回$ dates

”在此处输入图像描述

如何更改功能以返回第一个位置以返回一周的完整日期(27/12/2021-02/02/01/2022 ),在年份的最后一个位置相同

I have function of return weeks in year:

function getWeek(){

    $today = \Carbon\Carbon::today();
    $date = $today->copy()->firstOfYear()->startOfDay();
    $eom = $today->copy()->endOfYear()->startOfDay();
  
    $dates = [];

    for($i = 1; $date->lte($eom); $i++){
        $startDate = $date->copy();
        while($date->dayOfWeek != \Carbon\Carbon::SUNDAY && $date->lte($eom)){
                $date->addDay(); 
            }
        
        $dates['w'.$i] = $startDate->format('d/m/Y') . ' - ' . $date->format('d/m/Y');
        $date->addDay();
    }
    
    return $dates;
}

And the function return 53 weeks in year 2022 but in 2022 is 52 weeks. This is a result of return $dates

enter image description here

How to change the function to return first position to return full dates of week (27/12/2021 - 02/01/2022) and the same at the last position in year

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除会话 2025-01-31 13:52:15

该解决方案总是显示完整的几周。使用ISO 8601周数。
只需要日期时间。添加了钥匙的一年,以使它们始终是独一无二的。

$year = date_create('today')->format('Y');
//remove comment next line for test's
//$year = 2001;

$dtStart = date_create('2 jan '.$year)->modify('last Monday');
$dtEnd = date_create('last monday of Dec '.$year);

for($weeks = [];$dtStart <= $dtEnd;$dtStart->modify('+1 week')){
  $key = $dtStart->format('W-Y');
  $from = $dtStart->format('d/m/Y');
  $to = (clone $dtStart)->modify('+6 Days')->format('d/m/Y');
  $weeks[$key] = $from.' - '.$to;
}

var_export($weeks);

由于不需要碳,因此您还可以对其进行测试在这里

This solution always shows the complete weeks. Uses the ISO 8601 week number.
Only DateTime is required. The year was added for the key so that they are always unique.

$year = date_create('today')->format('Y');
//remove comment next line for test's
//$year = 2001;

$dtStart = date_create('2 jan '.$year)->modify('last Monday');
$dtEnd = date_create('last monday of Dec '.$year);

for($weeks = [];$dtStart <= $dtEnd;$dtStart->modify('+1 week')){
  $key = $dtStart->format('W-Y');
  $from = $dtStart->format('d/m/Y');
  $to = (clone $dtStart)->modify('+6 Days')->format('d/m/Y');
  $weeks[$key] = $from.' - '.$to;
}

var_export($weeks);

Since carbon is not required, you can also test it here.

一人独醉 2025-01-31 13:52:15

我认为你是错误的。

w1-&gt; w53 = 52周

I think you are mistaken.

w1->w53 = 52 weeks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文