循环通过日历周

时间:2015-10-30 16:01:27

标签: php arrays date for-loop strtotime

我在班上建立了这个功能:

class CalenderWeekHelper {
    public static function getCalenderWeek($year = 2016)
    {
        for ($i=1; $i <= 52; $i++)
        {
             $from = date("d.m.Y", strtotime("{$year}-W{$i}-1")); //Returns the date of monday in week
             $to   = date("d.m.Y", strtotime("{$year}-W{$i}-7")); //Returns the date of sunday in week
             $weekArray[$i] = array('start' => $from, 'end' => $to);

        }
        return $weekArray;
    }
}

并称之为:

$kw = CalenderWeekHelper::getCalenderWeek(2015);
echo $kw[1]['start']

但它仍然让我感到满意:

01.01.1970

我只想循环所有日历周,有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

请参阅以下答案,了解正确的strtotime格式: How to convert week number and year into unix timestamp?

返回的日子是星期一,所以你可以增加6天来获得星期日:

$week = sprintf('%02s', $i); // make sure it is formatted in double figures
$from = date("d.m.Y", strtotime("{$year}W{$week}")); //Returns the date of monday in week
$to   = date("d.m.Y", strtotime("{$year}W{$week} +6 days")); //Returns the date of sunday in week