在像今天这样的一天节目
我编写了一个程序,可以查找当前日期,然后在 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 月份则无法找到,因为月份与日期混淆了。 有什么解决办法吗?
I have made a program that finds the current date and then do a search in a php array and shows what happens On a Day Like Today. The only problem is that the program can't read the months 10-12 correctly. Here is my code:
The php array:
$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'
)
);
And the program is:
<?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>';
?>
The Problem is that the months 01-09 finds out and shows correctly and the months 10-12 can't finds because confuse the month with the day.
Any solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了你之外,没有什么会变得“困惑”。 :)
事实上问题不是几个月,而是几天。您的数组的日期格式为
j/m
(日期上没有前导零,月份上没有前导零),但您正在使用d/m
进行查找(日期上没有前导零)日期,月份没有前导零)。当我修复您的日期格式后,程序运行良好。
Nothing's getting "confused" but you. :)
In fact the problem is not months, but days. Your array's date format is
j/m
(no leading zeroes on date, no leading zeroes on month), but you're doing lookup withd/m
(leading zeroes on date, no leading zeroes on month).When I fix your date format, the program works well.