列表开始&一年中每周结束日期的动态

时间:2016-01-04 07:56:24

标签: php date datetime php-carbon

如何动态获取一年中所有周的列表以及每周的开始和结束日期?

每周应从星期一开始

提前致谢:)

2 个答案:

答案 0 :(得分:1)

代码:

$year = 2016;

$weeksOfYear = new DatePeriod(
    new DateTime("$year-1 monday"),
    new DateInterval('P1W'),
    new DateTime("$year-12-31")
);

$weekCounter = 1;

foreach ($weeksOfYear as $startOfWeek) {

    echo "Week $weekCounter => "
        . "(start): "
        . $startOfWeek->format('Y-m-d')
        . " - (end): "
        . $startOfWeek->modify('+6 days')->format('Y-m-d')
        . "<br>";

    $weekCounter ++;
}

输出:

Week 1 => (start): 2016-01-04 - (end): 2016-01-10
Week 2 => (start): 2016-01-11 - (end): 2016-01-17
Week 3 => (start): 2016-01-18 - (end): 2016-01-24
...
Week 51 => (start): 2016-12-19 - (end): 2016-12-25
Week 52 => (start): 2016-12-26 - (end): 2017-01-01

答案 1 :(得分:1)

uri->segment()