Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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或MySQL中按日期(月-年)排序_Php_Mysql_Sorting - Fatal编程技术网

在PHP或MySQL中按日期(月-年)排序

在PHP或MySQL中按日期(月-年)排序,php,mysql,sorting,Php,Mysql,Sorting,我在软件上到处都查过了,但找不到解决办法。我有一个日期,它是来自mysql的月和年。我的sql查询使用的日期格式如下date\u格式(月、%m%Y') 这是我的排序函数 $MyResult = Company::Sorting($Result,'month'); public static function vpbxSorting_2($a,$subkey) { foreach($a as $k=>$v) { $b[$k] = strtolower($v[$su

我在软件上到处都查过了,但找不到解决办法。我有一个日期,它是来自mysql的
。我的sql查询使用的日期格式如下
date\u格式(月、%m%Y')

这是我的排序函数

$MyResult = Company::Sorting($Result,'month');


public static function vpbxSorting_2($a,$subkey) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    asort($b, 1);
    foreach($b as $key=>$val) {
        $c[] = $a[$key];
    }
    return $c;
}
它给我这样的排序,这是不正确的

print_r($MyResult );
排序如下:

Array
(
    [0] => Array
        (
            [month] => 01 2017
            [bundle_Price] => 1200
        )

    [1] => Array
        (
            [month] => 02 2017
            [bundle_Price] => 7200
        )

    [2] => Array
        (
            [month] => 04 2017
            [bundle_Price] => 1200
        )

    [3] => Array
        (
            [month] => 04 2016
            [bundle_Price] => 7200
        )

    [4] => Array
        (
            [month] => 05 2017
            [bundle_Price] => 1200
        )

    [5] => Array
        (
            [month] => 05 2016
            [bundle_Price] => 13200
        )

    [6] => Array
        (
            [month] => 06 2017
            [bundle_Price] => 9600
        )

    [7] => Array
        (
            [month] => 06 2016
            [bundle_Price] => 6000
        )

    [8] => Array
        (
            [month] => 07 2017
            [bundle_Price] => 14400
        )

    [9] => Array
        (
            [month] => 07 2016
            [bundle_Price] => 6000
        )

    [10] => Array
        (
            [month] => 08 2017
            [bundle_Price] => 1200
        )

    [11] => Array
        (
            [month] => 08 2016
            [bundle_Price] => 6000
        )

    [12] => Array
        (
            [month] => 09 2016
            [bundle_Price] => 17500
        )

    [13] => Array
        (
            [month] => 10 2016
            [bundle_Price] => 1200
        )

    [14] => Array
        (
            [month] => 12 2016
            [bundle_Price] => 6000
        )

)
但我希望它能按顺序按月份排序。。。排序后,上述数组中的顺序不正确

下面的排序顺序是按月份和年份的顺序

.. so on....
[month] => 03 2017
[month] => 02 2017
[month] => 01 2017
[month] => 12 2016
[month] => 11 2016
[month] => 10 2016
[month] => 09 2016
... so on...

我想按数组中的
[month]
字段订购,而不是按
[bundle\u price
],但bundle\u price应该在数组中。

您需要执行一些步骤来实现这一点

对每个月变量进行
strotime
,然后进行排序

$tempArray = [];
for($x = 0; $x < count($myResult); $x++){
    $tempArray[strtotime($myResult['month])] = $myResult[$x];
}

现在您可以打印($myResult),结果将符合您的期望。

更改数据格式是否为时已晚

YYYY-MM将大大简化您的任务

然后您就可以按日期订购(DESC)


事实上,将其设置为INT而不是字符串将更有效&仍然很容易在PHP中格式化以显示。

我不明白。输出是否为预期结果?另外,您希望先按月订购,然后按年订购,但捆绑价格是不相关的?我希望按数组中的
[month]
字段订购,而不是按
[bundle\u price
],但捆绑价格应该在数组中。那么年份呢?它应该是月份,然后是年份,否则会出现类似这样的情况
03 2017、04 2016、05 2017…
是否可以更改您的数据库,以便将月份和年份存储在单独的字段中?这将使排序更容易。更改数据格式是否太晚?YYYY-MM将大大简化您的任务。然后您可以只使用
orderbydate(DESC)
事实上,将其设置为INT而不是字符串将更有效,而且在PHP中仍然很容易设置显示格式。我已将日期格式更改为
DATE\u格式(月、%Y%m')
而不是
DATE\u格式(月、%m%Y')
,效果良好。非常感谢
$tempArray = [];
for($x = 0; $x < count($myResult); $x++){
    $tempArray[strtotime($myResult['month])] = $myResult[$x];
}
$keys = sort(array_keys($tempArray));