2010-10-20 51 views
0

我着手生成一個數組,其中包含從此(現在)月份開始的12個月日曆。這是針對特定的應用程序,並且需要特殊的代碼,所以我不能使用日曆庫。日曆代奇怪的失敗

這裏是我的代碼:

header('Content-type: text/plain'); 

$Cal1 = array(); 

$now = new fTimestamp('now'); 
$now = $now->modify('Y-m-1 00:00:00'); 

for ($i = 0; $i < 12; $i++) { 
if ($i > 1) { 
    $then = $now->adjust("+$i months"); 
} 
elseif ($i == 1) { 
    $then = $now->adjust("+1 month"); 
} 
else { 
    $then = $now; 
} 
$thisMonth = $then->format('F'); 
$Cal1[$thisMonth] = array(); 
$thisMonthDays = $then->format('t'); 
for ($j = 0; $j < $thisMonthDays; $j++) { 
    if ($i > 1) { 
    $then = $then->adjust("+$i days"); 
    } 
    elseif ($i == 1) { 
    $then = $then->adjust("+1 day"); 
    } 
    $thisDate = $then->format('j'); 
    $thisDay = $then->format('l'); 
    $Cal1[$thisMonth][$thisDate] = $thisDay; 
} 
} 

var_dump($Cal1); 

這應該生成一種形式的數組:

array { 
    ["Month_Name"] => array { 
     [Day_Number] => "Day_Name" 
     etc... 
    } 
    etc... 
} 

的腳本輸出月份的正確數量,而不是幾天的權數。 ..全部轉儲非常冗長,所以我只會發布10月,2月和3月:

array(12) { 
    ["October"]=> 
    array(1) { 
    [1]=> 
    string(6) "Friday" 
    } 
    ["February"]=> 
    array(22) { 
    [5]=> 
    string(8) "Saturday" 
    [9]=> 
    string(9) "Wednesday" 
    [13]=> 
    string(6) "Sunday" 
    [17]=> 
    string(8) "Thursday" 
    [21]=> 
    string(6) "Monday" 
    [25]=> 
    string(6) "Friday" 
    [1]=> 
    string(7) "Tuesday" 
    [29]=> 
    string(7) "Tuesday" 
    [2]=> 
    string(8) "Saturday" 
    [6]=> 
    string(9) "Wednesday" 
    [10]=> 
    string(6) "Sunday" 
    [14]=> 
    string(8) "Thursday" 
    [18]=> 
    string(6) "Monday" 
    [22]=> 
    string(6) "Friday" 
    [26]=> 
    string(7) "Tuesday" 
    [30]=> 
    string(8) "Saturday" 
    [4]=> 
    string(9) "Wednesday" 
    [8]=> 
    string(6) "Sunday" 
    [12]=> 
    string(8) "Thursday" 
    [16]=> 
    string(6) "Monday" 
    [20]=> 
    string(6) "Friday" 
    [24]=> 
    string(7) "Tuesday" 
    } 
    ["March"]=> 
    array(19) { 
    [6]=> 
    string(6) "Sunday" 
    [11]=> 
    string(6) "Friday" 
    [16]=> 
    string(9) "Wednesday" 
    [21]=> 
    string(6) "Monday" 
    [26]=> 
    string(8) "Saturday" 
    [31]=> 
    string(8) "Thursday" 
    [5]=> 
    string(8) "Thursday" 
    [10]=> 
    string(7) "Tuesday" 
    [15]=> 
    string(6) "Sunday" 
    [20]=> 
    string(6) "Friday" 
    [25]=> 
    string(9) "Wednesday" 
    [30]=> 
    string(6) "Monday" 
    [4]=> 
    string(6) "Monday" 
    [9]=> 
    string(8) "Saturday" 
    [14]=> 
    string(8) "Thursday" 
    [19]=> 
    string(7) "Tuesday" 
    [24]=> 
    string(6) "Sunday" 
    [29]=> 
    string(6) "Friday" 
    [3]=> 
    string(9) "Wednesday" 
    } 

現在,這是怎麼回事?

+1

什麼是'fTimestamp'類...? – deceze 2010-10-20 09:51:19

+0

這是由Flourish Unframework提供的:http://flourishlib.com/docs/fTimestamp#Formatting – 2010-10-20 09:57:22

回答

1

並不直接回答你的問題,但我會做了很多簡單的,像這樣:

$cursor = mktime(0, 0, 0, date('m'), 1); 
$end = strtotime('+1 year', $cursor); 

$out = array(); 

while ($cursor < $end) { 
    $out[date('F', $cursor)][date('j', $cursor)] = date('l', $cursor); 
    $cursor = strtotime('+1 day', $cursor); 
} 

var_dump($out); 

直接回答你的問題,你$then相對總是調整到自己與越來越大的數字,因此你正在跳過幾天,並且正在變得時髦。

for ($j = 1; $j < $thisMonthDays; $j++) { 
    $then = $then->adjust("+$i days"); 
} 
  1. $then是第一,添加1(第一次迭代)
  2. $then是第二,添加2(第二次迭代)
  3. $then是第四,添加3
  4. $then是第7位,你加4
  5. etc。
+0

哇,謝謝。兩個答案中的一個。我真的沒有看到算法錯誤,儘管現在看起來很明顯......但它經常發生,嘿? – 2010-10-20 10:16:49