因为日期范围问题而没有下周

时间:2013-12-30 07:15:39

标签: php date

我希望显示当前和下一周的日期范围的下降数字。

我使用过这段代码         

  for ($i = $j; ($i <= $j + 1) && ($i <= 52); $i++) {
    $str = '';
    if (in_array($i, $week_no_array)) {
        $str ='selected = "selected"';
    }
    ?>

        <option value="<?php echo $i; ?>" <?php echo $str; ?> >
        <?php
    $year = date('Y');

    $week_start_date = date('d/m/Y', strtotime($year . "W" . $i . '1'));
    $week_end_date = date('d/m/Y', strtotime($year . "W" . $i . '7'));

    echo $i . ' (' . $week_start_date . ' - ' . $week_end_date . ')'
    ?>
        </option>

     但是对于今天的日期(2013年12月30日),它显示错误的一周范围,因为它需要2013年,但是一周没有是2014年的1。

1 个答案:

答案 0 :(得分:1)

更改行:

$year = date('Y');

使用:

$year = date('o');
$i = sprintf('%02d', $i);


在你的foreach循环中你有条件$i <= 52,这是不行的,因为多年也可以有53周(每400年有71年,有53周)。 < / p>

DateTime demo