2010-01-20 6 views

답변

7

첫 번째 월요일을 찾아서 시작하면 1 년 단위까지 추가 할 수 있습니다.

<?php 
define('NL', "\n"); 

$year   = 2010; 
$firstDayOfYear = mktime(0, 0, 0, 1, 1, $year); 
$nextMonday  = strtotime('monday', $firstDayOfYear); 
$nextSunday  = strtotime('sunday', $nextMonday); 

while (date('Y', $nextMonday) == $year) { 
    echo date('c', $nextMonday), '-', date('c', $nextSunday), NL; 

    $nextMonday = strtotime('+1 week', $nextMonday); 
    $nextSunday = strtotime('+1 week', $nextSunday); 
} 
+0

첫 번째 월요일은 반드시 ISO 주 1의 월요일과 같을 필요는 없습니다. 주 1은 첫 번째 Thurday를 보유하는 주인 것으로 간주됩니다. 예를 들어 2013 년 1 월 1 일은 2013 년 12 월 31 일부터 시작됩니다. 이 값을 계산에 반영하려면 다음과 같이하십시오. $ firstDayOfYear = mktime (0, 0, 0, 1, 1, $ year); $ first_thursday = strtotime ('thursday', $ firstDayOfYear); $ first_monday = strtotime (날짜 ("Y-m-d", $ first_thursday). "- 3 일"); –

+0

@ 케빈 : 고마워, 그걸 몰랐어. 당신은 정말로 옳습니다 : http://en.wikipedia.org/wiki/ISO_week_date#First_week 하지만 OP의 질문을 이해하면 그는 1 년 동안 1 주일 동안 찾고 있습니다. –

1

..using 다음과 같은 올해 :

$year = 2010; 

$date = new Zend_Date(); 
$date->set("01.01.$year", Zend_Date::DATES); 

while(true) 
{ 
    if($date->equals('Mon', Zend_Date::MONTH_NAME_SHORT)) 
    { 
     //It's monday - print date 
     break; 
    } 
    else 
    { 
     //It's not monday - move to the next day 
     $date->add('1', Zend_Date::DAY_SHORT); 
    } 

} 
관련 문제