简体   繁体   English

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

[英]Sort by Date (Month Year) in PHP or MySQL

I have checked everywhere on SOF, but unable to find solution for it. 我已经检查过SOF的所有内容,但无法找到解决方案。 I have date which is month and year like this coming from mysql. 我有日期是monthyear这样从MySQL的到来。 My sql query is using date format like this DATE_FORMAT( month, '%m %Y' ) 我的SQL查询使用的是日期格式,例如DATE_FORMAT( month, '%m %Y' )

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

[1] => Array
        (
            [month] => 07 2017
            [bundle_Price] => 1200
        )

Here is my sort function.. 这是我的排序功能。

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

It gives me sorting like this which is not correct.. 它给我这样的排序是不正确的。

print_r($MyResult );

Sort below: 排序如下:

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
        )

)

but i want it to sort like by MonthYear in DESC order... The order in above array is not correct after sorting.. 但我希望它按DEC顺序按MonthYear排序...排序后上述数组中的顺序不正确。

The below sort order is in Desc order of month and year. 下面的排序顺序是按月和年的Desc顺序。

.. 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...

I want to order by [month] field in array, not by [bundle_price ], But bundle_price should be there in array. 我想按数组中的[month]字段排序,而不[bundle_price ] [bundle_price ,但是bundle_price应该在数组中排序。

You need to do few steps to achieve this. 您需要执行一些步骤来实现此目的。

Make strtotime of each month variable and then do sort. 设置每个月的strtotime变量,然后进行排序。

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

In tempArray you will be having keys of only strtotime result of each month. tempArray您将只有每个月的strtotime结果的键。 Now you need to do sory. 现在,您需要做些抱歉。

$keys = sort(array_keys($tempArray));

Now you can print_r($myResult) and the result will be as per your expectation. 现在您可以使用print_r($myResult) ,结果将符合您的期望。

Is it too late to change your data format? 更改数据格式是否为时已晚?

YYYY MM would greatly simplify your task. YYYY MM将大大简化您的任务。

Then you can just ORDER BY DATE (DESC) 然后,您可以按ORDER BY DATE (DESC)

In fact, making it an INT, rather than a string would be more efficient & still easy to format in PHP for display. 实际上,将其设置为INT而不是字符串将更有效,并且仍易于在PHP中格式化以进行显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM