2011-10-01 43 views
0

我做了一個程序,找到當前的日期,然後在php數組中進行搜索,並顯示在今天這樣的一天發生了什麼。唯一的問題是程序無法正確讀取月份10-12。這裏是我的代碼:像今天一樣程序

的PHP數組:

$anniversary = array(
    '1/01' => array (
     '1813' => 'something here', 
     '1824' => 'something here', 
     '2001' => 'something here' 
    ), 
    '31/12' => array(
     '-450' => 'something here', 
     '-168' => 'something here', 
     '1942' => 'something here' 
    ) 
); 

而且程序:

<?php 
include 'array.php'; 
$today = date('d/m'); 

foreach ($anniversary[$today] as $hdate => $event) { 
    $table[0][] = $hdate; 
    $table[0][] = $event; 
    $counter++; 
} 

do { 
    $random = rand(0, $counter * 3); 
} while($random % 2 == 0); 

echo '<h2>'.$table[0][$random-1].": ".'</h2>'. 
    '<p>'.$table[0][$random].'</p>'; 
?> 

的問題是,數月01-09找出並正確顯示和10個月-12找不到因爲月份與當天混淆。 任何解決方案?

+2

'D'是 「與領先的零天」 - 爲什麼你使用''1/01''而不是''01/01''? – Griwes

+0

另外,爲什麼不使用另一個數組級來分隔月份和日期呢?那麼'$週年[月] [日] [年]'或者什麼? – poke

+0

修復您的縮進。 –

回答

0

沒有什麼會讓你感到「困惑」。 :)

事實上,問題不是幾個月,而是幾天。陣列的日期格式爲j/m(日期無前導零,月份沒有前導零),但是您正在使用d/m(導致零日期,月份沒有前導零)進行查找。

When I fix your date format, the program works well.

+0

謝謝你的回答 –