给出PHP的时间和日期函数中使用的各种字符串数组
[ '+7 day', '+2 week', '+5 month', '+1 week', '+10 day', '+2 year', '+1 day', ... ]
您如何将这些分类为:
[ '+1 day', '+7 day', '+1 week', '+10 day', '+2 week', '+5 month', '+2 year', ... ]
答案 0 :(得分:1)
效率不高且非常难看:
<?php
define('DATEINTERVAL_FORMAT', '%R%Y%M%D%H%I%S');
$intervals = [ '+7 day', '+2 week', '+5 month', '+1 week', '+10 day', '+2 year', '+1 day', /*...*/ ];
usort(
$intervals,
function ($a, $b) {
$da = DateInterval::createFromDateString($a);
$db = DateInterval::createFromDateString($b);
return strcmp($da->format(DATEINTERVAL_FORMAT), $db->format(DATEINTERVAL_FORMAT));
}
);
var_dump($intervals);
结果:
array(7) {
[0]=>
string(6) "+1 day"
[1]=>
string(6) "+7 day"
[2]=>
string(7) "+1 week"
[3]=>
string(7) "+10 day"
[4]=>
string(7) "+2 week"
[5]=>
string(8) "+5 month"
[6]=>
string(7) "+2 year"
}