Using Date as Array
Is there any ways to use DATE in PHP as array. I need to achieve something as below:
$dat开发者_Python百科e_array = (1-Jan => 'A', 2-Jan => 'B', 3-Jan => 'C', .... so on)
Is it possible?
Manually:
$array['1-Jan'] = 'A';
$array['2-Jan'] = 'B';
...
Or with a loop:
$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);
}
The PHP manual specifies that only string and integer can be used as keys for assoc. arrays, but date returns a string, so they should be fine, but to retrive the values back would be a bit messy.
What about something like:
$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);
That echos "1".
My mistake, you would need to use array_search() instead of in_array() so you get the array key:
echo array_search(date('Y-m-d'), $arr);
Ouputs "A".
精彩评论