PHP if:查找六月的日期,但不查找八月的日期

PHP if:查找六月的日期,但不查找八月的日期,php,Php,这太奇怪了,一定是我找不到的打字错误 首先,我从mySql获取数据并将其放入php数组中。 然后我做一个var转储,看看我得到了什么: var_dump($stmt->fetchAll()); 我现在宣布结果: [5341]=> array(2) { ["datum"]=> string(19) "2015-07-04 06:47:50" ["usag"]=> string(3) "avf" } [5342]=> array(2) { ["

这太奇怪了,一定是我找不到的打字错误

首先,我从mySql获取数据并将其放入php数组中。
然后我做一个var转储,看看我得到了什么:

var_dump($stmt->fetchAll());
我现在宣布结果:

[5341]=> array(2) {
    ["datum"]=> string(19) "2015-07-04 06:47:50"
    ["usag"]=> string(3) "avf"
}
[5342]=> array(2) {
    ["datum"]=> string(19) "2015-08-03 13:42:15"
    ["usag"]=> string(3) "avf"
}
所以,我得到了07和08两个月的结果
现在我想循环抛出所有结果,并将其放入一个排列的数组中。
我希望它是按年、按月进行的,只需计算结果即可。

while($row = $stmt->fetch()){
    //Check if thisYear
    if(substr($row['datum'], 0, 4) == $thisYear){
        //Check if Jan
        if(substr($row['datum'], 5, 2) == 01){ 
            $totalElementCount[$thisYear]['jan']++;
        }
        //Check if Feb
        elseif(substr($row['datum'], 5, 2) == 02){
            $totalElementCount[$thisYear]['feb']++;
        }
        //Check if Mar
        elseif(substr($row['datum'], 5, 2) == 03){
            $totalElementCount[$thisYear]['mar']++;
        }
        //Check if Apr
        elseif(substr($row['datum'], 5, 2) == 04){
            $totalElementCount[$thisYear]['apr']++;
        }
        //Check if Maj
        elseif(substr($row['datum'], 5, 2) == 05){
            $totalElementCount[$thisYear]['maj']++;
        }
        //Check if Jun
        elseif(substr($row['datum'], 5, 2) == 06){
            $totalElementCount[$thisYear]['jun']++;
        }
        //Check if Jul
        elseif(substr($row['datum'], 5, 2) == 07){
            $totalElementCount[$thisYear]['jul']++;
        }
        //Check if Aug
        elseif(substr($row['datum'], 5, 2) == 08){
            $totalElementCount[$thisYear]['aug']++;
        }
        //Check if Sep
        elseif(substr($row['datum'], 5, 2) == 09){
            $totalElementCount[$thisYear]['sep']++;
        }
        //Check if Okt
        elseif(substr($row['datum'], 5, 2) == 10){
            $totalElementCount[$thisYear]['okt']++;
        }
        //Check if Nov
        elseif(substr($row['datum'], 5, 2) == 11){
            $totalElementCount[$thisYear]['nov']++;
        }
        //Check if Dec
        elseif(substr($row['datum'], 5, 2) == 12){
            $totalElementCount[$thisYear]['dec']++;
        }
    }//if thisYear

    //Check if prevYear
    elseif(substr($row['datum'], 0, 4) == $prevYear){
        //Check if Jan
        if(substr($row['datum'], 5, 2) == 01){
            $totalElementCount[$prevYear]['jan']++;
        }
        //Check if Feb
        elseif(substr($row['datum'], 5, 2) == 02){
            $totalElementCount[$prevYear]['feb']++;
        }
        //Check if Mar
        elseif(substr($row['datum'], 5, 2) == 03){
            $totalElementCount[$prevYear]['mar']++;
        }
        //Check if Apr
        elseif(substr($row['datum'], 5, 2) == 04){
            $totalElementCount[$prevYear]['apr']++;
        }
        //Check if Maj
        elseif(substr($row['datum'], 5, 2) == 05){
            $totalElementCount[$prevYear]['maj']++;
        }
        //Check if Jun
        elseif(substr($row['datum'], 5, 2) == 06){
            $totalElementCount[$prevYear]['jun']++;
        }
        //Check if Jul
        elseif(substr($row['datum'], 5, 2) == 07){ 
            $totalElementCount[$prevYear]['jul']++;
        }
        //Check if Aug
        elseif(substr($row['datum'], 5, 2) == 08){
            $totalElementCount[$prevYear]['aug']++;
        }
        //Check if Sep
        elseif(substr($row['datum'], 5, 2) == 09){
            $totalElementCount[$prevYear]['sep']++;
        }
        //Check if Okt
        elseif(substr($row['datum'], 5, 2) == 10){
            $totalElementCount[$prevYear]['okt']++;
        }
        //Check if Nov
        elseif(substr($row['datum'], 5, 2) == 11){
            $totalElementCount[$prevYear]['nov']++;
        }
        //Check if Dec
        elseif(substr($row['datum'], 5, 2) == 12){
            $totalElementCount[$prevYear]['dec']++;
        }
    }//if prevYear
} //while
当循环结束时,我做另一个:
var_dump($totalElementCount)切换至se。但不知何故,2015-08和2015-09不见了
为什么?


解决问题的简单方法如下

  • 迭代查询结果
  • 不要使用数以百万计的elseif/if条件。如有必要,取下开关/外壳。在这种情况下,不需要if条件,因为您可以使用
    date()
    解析月份
  • 解析日期,而不是比较日期。您当前的代码将月份与前导0(而不是小数)的八进制数进行比较

  • 如果你想坚持你的代码(这是很难维护的,不需要),只要在你比较日期的每个数字周围加上引号,就可以进行字符串比较

    elseif(substr($row['datum'], 5, 2) == 02){
    


    亲爱的我。。。。使用类似于
    $totalElementCount[$thisYear][strtolower(date('M'),(strtotime('row['datum')))]
    的方法来消除所有这些
    ,如果你的问题是
    08
    09
    在你的比较中……数字(如果您使用
    '08'
    '09'
    进行字符串比较,则可以)带前导的
    0
    是八进制,
    08
    09
    是无效的八进制值,并被视为
    0
    @MarkBaker我知道一定有更好的方法,比如for循环。但这超出了我的认知范围:欢迎你给我一个更好的方法:)@MarkBaker很有趣。但为什么它适用于所有数字,而不是“08”&“09”@MarkBaker它通过添加引号工作得非常完美。谢谢!请将其写为answear!
    while($row = $stmt->fetch()){
        $year = substr($row['datum'],0,4); //Define year of the data record
        $month = strtolower(date('M', (strtotime($date)))); //parse month to 3 letter short form, e.g. "jan"
        $totalElementCount[$year][$month]++; //increment totalElemCount of the defined year and month
    }
    
    elseif(substr($row['datum'], 5, 2) == 02){
    
    elseif(substr($row['datum'], 5, 2) == "02"){