2 回答

TA贡献1829条经验 获得超7个赞
仔细看字符串 $a = date('dmY H:i', strtotime($a));
strtotime($a)正在尝试将字符串转换为时间戳。由于您有自定义日期格式,因此该字符串09082020 00:00将转换为false.
之后,date('dmY H:i', false)就会返回01011970 00:00。这就是排序不起作用的原因。
我会建议使用DateTime::createFromFormat.
usort($items, function ($a, $b) {
$a = DateTime::createFromFormat('dmY H:i', $a);
$b = DateTime::createFromFormat('dmY H:i', $b);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
});

TA贡献1719条经验 获得超6个赞
在这一部分
date('dmY H:i', strtotime($a));
date您正在尝试使用格式创建,但您设置了strtotime()返回 Unix 时间戳 (int) 的值。所以你可能正在寻找类似的东西:
\DateTime::createFromFormat('dmY H:i', $a);
所以它可能是这样的:
function orderDates($items) {
//Sort them. Latest one first
usort($items, function ($a, $b) {
$a = \DateTime::createFromFormat('dmY H:i', $a);
$b = \DateTime::createFromFormat('dmY H:i', $b);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
});
return $items;
}
- 2 回答
- 0 关注
- 144 浏览
添加回答
举报