我是 php 新手,我有 php 日期数组
[0] => 11-01-2012
[1] => 01-01-2014
[2] => 01-01-2015
[3] => 09-02-2013
[4] => 01-01-2013
我想这样排序:
[0] => 11-01-2012
[1] => 01-01-2013
[2] => 09-02-2013
[3] => 01-01-2014
[4] => 01-01-2015
我使用
asort
但不起作用。
如果日期是“Mysql”格式(
Y-m-d
或Y-m-d H:i:s
),那么您可以立即对数组进行排序,无需特殊操作:
$arr = ["2019-11-11", "2019-10-10","2019-11-11", "2019-09-08","2019-05-11"];
sort($arr);
如果日期已本地化或格式化(您应该避免,仅在输出之前格式化日期),您必须使用自定义排序函数,例如
usort()
,它将在比较之前将日期转换为可排序格式。
strtotime()
函数将其转换为 uninx 时间戳:
$arr = ['11/01/2012', '03/16/2022', '12/26/2021', '01/01/2014', '09/02/2013'];
usort($arr, function ($a, $b) {
return strtotime($a) - strtotime($b);
});
print_r($arr);
但是,可能存在陷阱,因为在不同的国家/地区,相同的日期格式可能意味着不同的日期。您的示例格式正是这种情况,如果日期为
['03-16-2022', '12-26-2021', '06-06-2022']
,则上述函数将返回错误的结果。因此,最好明确定义日期格式,如这个答案中所述
提供的示例日期数据不清楚它们是什么格式。可以肯定的是,您应该使用
DateTime::createFromFormat
,然后明确指定格式:
$dates = ['11-01-2012', '03-16-2022', '12-26-2021', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013'];
function sort_date($a, $b) {
return \DateTime::createFromFormat('m-d-Y', $a) <=> \DateTime::createFromFormat('m-d-Y', $b);
}
usort($dates, "sort_date");
尝试以下代码:
<?php
$array = array('11-01-2012','01-01-2011','09-02-2013','01-01-2014','01-01-2015');
function cmp($a, $b)
{
$a = date('Y-m-d', strtotime($a));
$b = date('Y-m-d', strtotime($b));
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($array, "cmp");
foreach ($array as $key => $value) {
echo "[$key]=> $value <br>";
}
?>
开箱即用的使用时间函数来生成 ts 并排序
<?php
$out = array();
// $your_array is the example obove
foreach($your_array as $time) {
$out[strtotime($time)] = $time;
}
// now $out has ts-keys and you can handle it
...
?>
试试这个,
<?php
$array = [ '11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013' ];
function sortFunction( $a, $b ) {
return strtotime($a) - strtotime($b);
}
usort($array, "sortFunction");
var_dump( $array );
?>
将按照您想要的顺序对日期进行排序。
我通过应用这条线解决了这个问题。
sort($imdarrayGG['newDate']);
添加值之前的输出
[Mon Jul 6 10:33:23 2020] Array
(
[coursedate] => 2020-07-17
[newDate] => Array
(
[0] => 2020-07-30
[1] => 2020-07-07
[2] => 2020-07-08
[3] => 2020-07-17
)
)
我放线后,输出会变成这样。
[Mon Jul 6 10:35:42 2020] Array
(
[coursedate] => 2020-07-17
[newDate] => Array
(
[0] => 2020-07-07
[1] => 2020-07-08
[2] => 2020-07-17
[3] => 2020-07-30
)
)
已排序的日期,最新日期将在最上面。
这是我们有对象的地方,然后我们想要对所有对象进行排序
示例:-
[status_custom] => Array
(
[0] => stdClass Object
(
[type] => LDL - B2
[date] => 23/10/2014
)
[1] => stdClass Object
(
[type] => LDL - D
[date] => 18/04/2015
)
)
首先我们必须进行转换。 参考此链接进行转换 然后我们对它们进行排序。 参考此链接对对象进行排序
我们开始:-
//At here we want to sort according to latest date. But this one involve with the object also.
usort($array_custom_object, function ($a, $b) {
$date1 = $a->date;
$date2 = $b->date;
//Since the income date is formatted like this d/m/Y , we have to change it
$date1 = date_format(date_create_from_format('d/m/Y', $date1), 'Y-m-d');
$date2 = date_format(date_create_from_format('d/m/Y', $date2), 'Y-m-d');
if ($date1 > $date2) {
return -1;
}
if ($date1 == $date2) {
return 0;
}
if ($date1 < $date2) {
return 1;
}
});
$dataFinal->status_custom = $array_custom_object; //Result
使用 DateTime 对日期进行排序:
$a = array(
new DateTime('2016-01-02'),
new DateTime('2016-05-01'),
new DateTime('2015-01-01'),
new DateTime('2016-01-01')
);
asort($a);
var_dump($a);
输出将是:
array(4) {
[2]=>
object(DateTime)#3 (3) {
["date"]=>
string(26) "2015-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
[3]=>
object(DateTime)#4 (3) {
["date"]=>
string(26) "2016-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
[0]=>
object(DateTime)#1 (3) {
["date"]=>
string(26) "2016-01-02 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
[1]=>
object(DateTime)#2 (3) {
["date"]=>
string(26) "2016-05-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
}
您绝对可以使用下面给定的代码将数组排序为时间戳,但我想请您注意我用来存储的时间格式。以这种格式存储时间有不同的好处。在这里阅读更多相关信息https://stackoverflow.com/a/59912185/7997043
function compareByTimeStamp($a, $b ) {
return strtotime($b) - strtotime($a);
}
$arr = array("2020-02-11 00:00:00", "2020-02-13 00:00:00", "2020-02-08 00:00:00");
usort($arr, "compareByTimeStamp");
echo json_encode($arr);
这里有一些方法:
$array_to_sort_1 = array(
"11-01-2012",
"01-01-2014",
"01-01-2015",
"09-02-2013",
"01-01-2013"
);
fnc_sort_1($array_to_sort_1,"asc","d-m-Y");
// var_dump($array_to_sort_1); // * uncomment to test
$array_to_sort_2 = array(
array( "user"=>"user1", "date_str"=>"11-01-2012", "comment"=>"1" ),
array( "user"=>"Donny", "date_str"=>"01-01-2014", "comment"=>"4" ),
array( "user"=>"Frenk", "date_str"=>"01-01-2015", "comment"=>"5" ),
array( "user"=>"Axvel", "date_str"=>"01-01-2013", "comment"=>"2" ),
array( "user"=>"Johny", "date_str"=>"09-02-2013", "comment"=>"3" )
);
fnc_sort_2($array_to_sort_2,"asc","d-m-Y");
// var_dump($array_to_sort_2); // * uncomment to test
$array_to_sort_3 = array(
array( "user"=>"user1", "date_str"=>"11.01.2012 12:54", "comment"=>"1" ),
array( "user"=>"Donny", "date_str"=>"01.01.2014 12:54", "comment"=>"4" ),
array( "user"=>"Frenk", "date_str"=>"01.01.2015 12:54", "comment"=>"5" ),
array( "user"=>"Johny", "date_str"=>"09.02.2013 12:53", "comment"=>"2" ),
array( "user"=>"Axvel", "date_str"=>"09.02.2013 12:54", "comment"=>"3" )
);
fnc_sort_2($array_to_sort_3,"asc","d.m.Y H:i");
// var_dump($array_to_sort_3); // * uncomment to test
/**
* * DateTime::createFromFormat("d.m.Y H:i", $date_str);
* * $in_direction ["asc","desc"]
*/
function fnc_sort_1(array &$in_arr, $in_direction = "asc", $in_dt_format = "Y-m-d H:i"){
usort($in_arr, function($a, $b) use($in_direction,$in_dt_format){
$v_desc = "desc";
$v_asc = "asc";
$dt_format = "Y-m-d H:i:s";
$a1 = DateTime::createFromFormat($in_dt_format, $a);
$b1 = DateTime::createFromFormat($in_dt_format, $b);
$a2 = $a1->format($dt_format) ;
$b2 = $b1->format($dt_format) ;
$a3 = strtotime($a2) ;
$b3 = strtotime($b2) ;
if($in_direction===$v_desc){
return $b3 - $a3; // * Desc
}else if ($in_direction===$v_asc){
return $a3 - $b3; // * Asc
}
return 0;
});
}
/**
* * DateTime::createFromFormat("d.m.Y H:i", $date_str);
* * $in_direction ["asc","desc"]
*/
function fnc_sort_2(array &$in_arr, $in_direction = "asc", $in_dt_format = "Y-m-d H:i"){
usort($in_arr, function($a, $b) use($in_direction,$in_dt_format){
$v_desc = "desc";
$v_asc = "asc";
$dt_format = "Y-m-d H:i";
$a1 = DateTime::createFromFormat($in_dt_format, $a["date_str"]);
$b1 = DateTime::createFromFormat($in_dt_format, $b["date_str"]);
$a2 = $a1->format($dt_format) ;
$b2 = $b1->format($dt_format) ;
$a3 = strtotime($a2) ;
$b3 = strtotime($b2) ;
if($in_direction===$v_desc){
return $b3 - $a3; // * Desc
}else if ($in_direction===$v_asc){
return $a3 - $b3; // * Asc
}
return 0;
});
}