有什么方法可以将PHP中的DATE用作数组。我需要做到以下几点:
$date_array = (1-Jan => 'A', 2-Jan => 'B', 3-Jan => 'C', .... so on)
将日期用作数组
这可能吗?
有什么方法可以将PHP中的DATE用作数组。我需要做到以下几点:
$date_array = (1-Jan => 'A', 2-Jan => 'B', 3-Jan => 'C', .... so on)
将日期用作数组
这可能吗?
手动:
$array['1-Jan'] = 'A';
$array['2-Jan'] = 'B';
...
或用一个循环:
$array = array();
$currentDate = strtotime('2010-01-01');
$totalDays = 365;
for ($i=0; $i<$totalDays; $i++) {
$array[date('j-M', $currentDate)] = $i;
$currentDate = strtotime("+1 day", $currentDate);
}
PHP手册指定只有字符串和整数可以被用作用于ASSOC密钥。数组,但日期返回一个字符串,所以他们应该没问题,但是回想一下数值会有点混乱。
什么是这样的:
$arr = Array(
'A'=>date('Y-m-d', time()),
'B'=>date('Y-m-d', time()-(7 * 24 * 60 * 60))
);
echo in_array(date('Y-m-d'), $arr);
那回声 “1”。
我的错误,你就需要使用array_search()而不是in_array()的,所以你得到的数组键:
echo array_search(date('Y-m-d'), $arr);
。OUPUTS “A”。
你想用什么类型的日期作为索引?日期时间对象?日期字符串? – joni 2010-12-15 09:26:17
日期字符串将用作索引。 – cys 2010-12-15 10:53:28