使用PHP从数组中删除过期日期 [英] Removing expired dates from an array with PHP
问题描述
我有一系列以"m/d/Y"(月/日/年)为格式的日期:
I have an array of dates formatted in 'm/d/Y' (month/day/year):
$array = array(
'1/10/2014',
'1/11/2014',
'1/12/2014',
'1/13/2014',
'1/14/2014'
);
,我想只输出今天或将来的日期的数组.因此,如果今天是2014年1月12日,它将消除2014年1月10日和2014年1月11日.
and I'd like to output the array with only dates of today or in the future. So if today is 1/12/2014 it would eliminate the 1/10/2014 and 1/11/2014.
这是我无法使用的内容:
Here is what I have that isn't working:
foreach ($array as $date) {
$unixdate = strtotime($date);
if ($unixdate < time()) {
unset($array[$date]);
}
}
print_r($array);
我猜我没有正确使用 unset
,因为它会打印整个数组?
I guess I'm not using unset
correctly because it prints the entire array?
推荐答案
我建议您研究 array_filter()
.另外,您还想将今天的日期12:00:00用于比较,而不是当前时间.
I suggest you look into array_filter()
. Also you want to use today's date at 12:00:00 for the comparisson, not the current time.
$array = array(
'1/10/2014',
'1/11/2014',
'1/12/2014',
'1/13/2014',
'1/14/2014'
);
$array = array_filter($array,function($date){
return strtotime($date) >= strtotime('today');
});
print_r($array); //Array ( [2] => 1/12/2014 [3] => 1/13/2014 [4] => 1/14/2014 )
*注意:匿名功能需要PHP 5.3
*Note: PHP 5.3 needed for anonymous functions
这篇关于使用PHP从数组中删除过期日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!