Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果日期的键小于/大于,则从数组中获取所有元素_Php_Count - Fatal编程技术网

Php 如果日期的键小于/大于,则从数组中获取所有元素

Php 如果日期的键小于/大于,则从数组中获取所有元素,php,count,Php,Count,我有一个数组: [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-2

我有一个数组:

[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 );
    }
}

但这是行不通的。你们能帮帮我吗?

你们应该这样做

$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;

你应该这样做

$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;

如果你有非常庞大的阵列,我会优化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求值超过1秒,则条件语句中会有两个不同的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

如果你有非常庞大的阵列,我会优化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求值超过1秒,则条件语句中会有两个不同的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

@RafcioKowalsky,如果这已经回答了你的问题,你能标记它的答案吗?:)@RafcioKowalsky,如果这已经回答了你的问题,你能标记它的答案吗?:)谢谢你的努力,我将使用var缓存。谢谢你的努力,我将使用var缓存。