像今天一样程序
问题描述:
我做了一个程序,找到当前的日期,然后在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找不到因为月份与当天混淆。 任何解决方案?
答
没有什么会让你感到“困惑”。 :)
事实上,问题不是几个月,而是几天。阵列的日期格式为j/m
(日期无前导零,月份没有前导零),但是您正在使用d/m
(导致零日期,月份没有前导零)进行查找。
+0
谢谢你的回答 –
'D'是 “与领先的零天” - 为什么你使用''1/01''而不是''01/01''? – Griwes
另外,为什么不使用另一个数组级来分隔月份和日期呢?那么'$周年[月] [日] [年]'或者什么? – poke
修复您的缩进。 –