我有一个数组:
[0] => Array
(
[id] => 1
[date] => 2014-07-26 10:36:52
[nr_ref] => DX35359GC
[nr_invoice] => 2014/07/359235ASF
)
[1] => Array
(
[id] => 2
[date] => 2014-07-27 11:58:47
[nr_ref] => PXI-s953953.35935.2
[nr_invoice] => 2014/35614/14014
)
[2] => Array
(
[id] => 3
[date] => 2014-07-27 11:59:19
[nr_ref] => R03835-3580533
[nr_invoice] => 2014/07/359235ASF
)
如何仅计算日期为>的这些项目date('Y-m-d H:i:s');
我最终得到了代码:
foreach($array as $k => $v) {
if( strtotime( $v['date'] ) > strtotime( date('Y-m-d H:i:s') ) ) {
echo count( $k );
}
}
但这不起作用。你能帮助我吗?
答案 0 :(得分:3)
你应该这样做。
$itmcount = 0; // initialize a counter variable
foreach($array as $k => $v) {
if( strtotime( $v['date'] ) > strtotime( date('Y-m-d H:i:s') ) ) {
$itmcount++; //increment counter
}
}
echo $itmcount;
答案 1 :(得分:3)
如果你有非常庞大的数组,我会优化bansi回答:
$itmcount = 0; // initialize a counter variable
$time = strtotime(date('Y-m-d H:i:s'));
foreach($array as $k => $v) {
if( strtotime( $v['date'] ) > $time) {
$itmcount++; //increment counter
}
}
echo $itmcount;
因为如果你对foreach的评估超过一秒,你将在条件语句中有两个不同的strtotime(date())值。例如:
echo strtotime(date('Y-m-d H:i:s')) . "\n"; // 1406465191
sleep(1);
echo strtotime(date('Y-m-d H:i:s')); // 1406465192