Php日期问题与今年发生冲突&明年

时间:2017-10-26 11:30:07

标签: php

当我每年的这一周,我得到了这个结果

week of45 date:2017-11-06
week of46 date:2017-11-13
week of47 date:2017-11-20
week of48 date:2017-11-27
week of49 date:2017-12-04
week of50 date:2017-12-11
week of51 date:2017-12-18
week of52 date:2017-12-25
week of1 date:1969-12-31
week of2 date:1969-12-31
week of3 date:1969-12-31
week of4 date:1969-12-31
week of5 date:1969-12-31

正如你可以看到它再次从1开始,日期搞砸了, 这是代码:

for($i=0;$i< 13;$i++) {
    $weekNumnew = $weekNum + $i;
    $week_date = date('Y-m-d', strtotime("$thisYear-W$weekNumnew-1")); 
    if($weekNumnew > '52'){
        $weekNumnew = $weekNumnew - 52;
        $week_date = date('Y-m-d', strtotime("$nextyear-W$weekNumnew-1"));
    }
    echo "week of" . $weekNumnew . " date:" .$week_date . "<br>";
}

$weekNumnew从45开始,每周增加,具体取决于一年中的一周。同样$thisYear =&#39; 2017&#39; &安培; $nextyear =&#39; 2018&#39; 有没有办法解决这个问题?我认为明年我加入的时候会修好但是没有用,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您可以使用DateInterval和DatePeriod的组合为您生成这些数据:

<?php

$start = new DateTime('now');
$end = new DateTime('+3 years');

$interval = DateInterval::createFromDateString('1 week');
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $week) {
  $monday = $week->modify('monday this week');
  printf("Week %d. Date: %s \n", $monday->format('W'), $monday->format('Y-m-d'));
}