php 如何根据日期排序数组?

dzhpxtsq  于 2024-01-05  发布在  PHP
关注(0)|答案(9)|浏览(201)

我有以下数组

  1. $dates = array(
  2. '2015-02-13',
  3. '2015-04-21',
  4. '2015-08-18',
  5. '2015-11-26',
  6. '2015-09-15',
  7. '2015-01-07',
  8. '2015-02-11',
  9. '2015-07-14',
  10. '2015-03-02',
  11. );

字符串
我想从最早到最近的日期对这个数组进行排序。

  1. foreach ($dates as $date) {
  2. $date = new DateTime($date);
  3. // The logic I need
  4. }


你能帮帮我吗?

编辑

我想指出的是,日期格式可以有所不同。为了详细说明,我在我的数据库中的日期格式为YYYY-MM-DDYYYY/MM/DDDD-MM-YYYYDD/MM/YYYY
但正如下面所建议的,我可以重新格式化日期并构建一个新数组。所以这可能不是问题。

wfauudbj

wfauudbj1#

php在MM DD YYYY下不能很好地工作,因为daymonth之间的混淆。
对于第一个和第二个条件YYYY-MM-DDYYYY/MM/DD,您可以遵循以下解决方案:

  1. $dates = array(
  2. '2015-02-13',
  3. '2015-04-21',
  4. '2015-08-18',
  5. '2015-11-26',
  6. '2015-09-15',
  7. '2015-01-07',
  8. '2015-02-11',
  9. '2015-07-14',
  10. '2015-03-02',
  11. );
  12. function date_sorter($a, $b)
  13. {
  14. $t1 = strtotime($a);
  15. $t2 = strtotime($b);
  16. return $t1 - $t2;
  17. }
  18. usort($dates, 'date_sorter');
  19. // check the output
  20. echo '<pre>'.print_r($dates,1).'</pre>'

字符串
输出值:

  1. Array
  2. (
  3. [0] => 2015-01-07
  4. [1] => 2015-02-11
  5. [2] => 2015-02-13
  6. [3] => 2015-03-02
  7. [4] => 2015-04-21
  8. [5] => 2015-07-14
  9. [6] => 2015-08-18
  10. [7] => 2015-09-15
  11. [8] => 2015-11-26
  12. )


FYI:您可以尝试将-更改为/
如果格式类似于YYYY DD MM(无论是/还是-),您可以看到以下解决方案:
样品输入:

  1. $dates = array(
  2. '13/02/2015',
  3. '21/04/2015',
  4. '18/08/2015',
  5. '26/11/2015',
  6. '15/09/2015',
  7. '07/01/2015',
  8. '11/02/2015',
  9. '14/07/2015',
  10. '02/03/2015',
  11. );


功能说明:

  1. function date_sorter($a, $b )
  2. {
  3. $da = DateTime::createFromFormat('d/m/Y', $a); // define date format d/m/Y
  4. $db = DateTime::createFromFormat('d/m/Y', $b); // define date format d/m/Y
  5. $t1 = strtotime($da->format('Y-m-d'));
  6. $t2 = strtotime($db->format('Y-m-d'));
  7. return $t1 - $t2;
  8. }
  9. usort($dates, 'date_sorter');
  10. echo '<pre>'.print_r($dates,1).'</pre>';


输出量:

  1. Array
  2. (
  3. [0] => 07/01/2015
  4. [1] => 11/02/2015
  5. [2] => 13/02/2015
  6. [3] => 02/03/2015
  7. [4] => 21/04/2015
  8. [5] => 14/07/2015
  9. [6] => 18/08/2015
  10. [7] => 15/09/2015
  11. [8] => 26/11/2015
  12. )

展开查看全部
wsewodh2

wsewodh22#

根据您的日期格式(在您的情况下,可以使用Y-m-d),只需执行排序

  1. sort($dates);

字符串

j91ykkif

j91ykkif3#

您拥有的日期是缩短的ISO 8601格式,即YYYY-MM-DD
它们有一个很好的特性,即词法排序顺序与日期顺序相同。所以,只需使用默认的排序函数对数组进行排序。它就可以工作了。
当在代码中处理日期字符串时,最好使用ISO 8601作为规范的内部日期格式。尽快将用户提供的格式转换为ISO 8601,如果用户需要在自己的语言环境中输出,则尽可能推迟。当然,使用“适当”的日期对象更好!

dgtucam1

dgtucam14#

只需尝试将所有日期格式转换为时间数字。

  1. <?php
  2. $finalArray = array();
  3. foreach ($datesInFormatA as $date) {
  4. $finalArray[] = strtotime($date);
  5. }
  6. foreach ($datesInFormatB as $date) {
  7. $finalArray[] = strtotime($date);
  8. }

字符串
对于所有的日期格式都这样做,然后你就有了一个数字数组,你可以很容易地对它们进行排序。

  1. $finalArray = sort($finalArray);

展开查看全部
vlurs2pr

vlurs2pr5#

您可以使用strtotime()进行转换以安全地进行日期操作,然后可以重新排序数组:

  1. $dates = array(
  2. '2015-02-13',
  3. '2015-04-21',
  4. '2015-08-18',
  5. '2015-11-26',
  6. '2015-09-15',
  7. '2015-01-07',
  8. '2015-02-11',
  9. '2015-07-14',
  10. '2015-03-02',
  11. );
  12. $newArray = array();
  13. foreach($dates as $value) {
  14. $newArray[] = strtotime($value);
  15. }
  16. sort($newArray);
  17. var_dump($newArray);

字符串

展开查看全部
ssgvzors

ssgvzors6#

使用PHP sort函数

  1. <?php
  2. $dates = array(
  3. '2015-02-13',
  4. '2015-04-21',
  5. '2015-08-18',
  6. '2015-11-26',
  7. '2015-09-15',
  8. '2015-01-07',
  9. '2015-02-11',
  10. '2015-07-14',
  11. '2015-03-02',
  12. );
  13. sort($dates);
  14. var_dump($dates);

字符串
输出量:

  1. array(9) {
  2. [0]=>
  3. string(10) "2015-01-07"
  4. [1]=>
  5. string(10) "2015-02-11"
  6. [2]=>
  7. string(10) "2015-02-13"
  8. [3]=>
  9. string(10) "2015-03-02"
  10. [4]=>
  11. string(10) "2015-04-21"
  12. [5]=>
  13. string(10) "2015-07-14"
  14. [6]=>
  15. string(10) "2015-08-18"
  16. [7]=>
  17. string(10) "2015-09-15"
  18. [8]=>
  19. string(10) "2015-11-26"
  20. }


请记住,这适用于'Y-m-d'格式。对于其他格式,您需要使用strtotime()函数转换为日期格式并继续进行。类似于这样的(标准格式):

  1. function sortFunction( $a, $b ) {
  2. return strtotime($a[0]) - strtotime($b[0]);
  3. }
  4. usort($dates, "sortFunction");


m/d/y或d-m-y格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜线(/),则假定为美国m/d/y格式;而如果分隔符是破折号(-)或点(.),则假定为欧洲d-m-y格式。

展开查看全部
6yjfywim

6yjfywim7#

试试这个快速排序算法。它所做的是,它将日期与从Jan 01 1970 (UTC))开始的Unix Timestamp进行比较,然后将其排序并放入数组中。
转换为Unix Timestamp确保ISO8601日期格式。

  1. $dates = array(
  2. '2015-02-13',
  3. '2015-04-21',
  4. '2015-08-18',
  5. '2015-11-26',
  6. '2015-09-15',
  7. '2015-01-07',
  8. '2015-02-11',
  9. '2015-07-14',
  10. '2015-03-02',
  11. );
  12. function quick_sort($array)
  13. {
  14. // find array size
  15. $length = count($array);
  16. // base case test, if array of length 0 then just return array to caller
  17. if($length <= 1){
  18. return $array;
  19. }
  20. else{
  21. // select an item to act as our pivot point, since list is unsorted first position is easiest
  22. $pivot = $array[0];
  23. // declare our two arrays to act as partitions
  24. $left = $right = array();
  25. // loop and compare each item in the array to the pivot value, place item in appropriate partition
  26. for($i = 1; $i < count($array); $i++)
  27. {
  28. if(strtotime($array[$i]) < strtotime($pivot)){
  29. $left[] = $array[$i];
  30. }
  31. else{
  32. $right[] = $array[$i];
  33. }
  34. }
  35. // use recursion to now sort the left and right lists
  36. return array_merge(quick_sort($left), array($pivot), quick_sort($right));
  37. }
  38. }
  39. $sorted = quick_sort($dates);
  40. print_r($sorted);

字符串
//输出

  1. Array
  2. (
  3. [0] 2015-01-07
  4. [1] 2015-02-11
  5. [2] 2015-02-13
  6. [3] 2015-03-02
  7. [4] 2015-04-21
  8. [5] 2015-07-14
  9. [6] 2015-08-18
  10. [7] 2015-09-15
  11. [8] 2015-11-26
  12. )

展开查看全部
uemypmqf

uemypmqf8#

我有一个解决方案给你,希望这将有助于你排序,因为这是为我工作。
我建议你使用usort()与自定义函数:

  1. $dates = array(
  2. '2015-02-13',
  3. '2015-04-21',
  4. '2015-08-18',
  5. '2015-11-26',
  6. '2015-09-15',
  7. '2015-01-07',
  8. '2015-02-11',
  9. '2015-07-14',
  10. '2015-03-02',
  11. );
  12. function date_fun_compare($a, $b)
  13. {
  14. $t1 = strtotime($a['datetime']);
  15. $t2 = strtotime($b['datetime']);
  16. return $t1 - $t2;
  17. }
  18. usort($dates, 'date_fun_compare');

字符串
希望这对你有帮助。

展开查看全部
knpiaxh1

knpiaxh19#

好吧,既然你提到你所有的日期都有以下格式:YYYY-MM-DD,YYYY/MM/DD,DD-MM-YYYY和DD/MM/YYYY
首先,你需要将它们转换成一种格式,让我们使用YYYY-MM-DD。然后,正如已经提到的,你只需要常规排序。
包括如何重新格式化字符串,而不必为每个字符串创建一个日期对象。

  1. $dates = array(
  2. '2015-02-13',
  3. '2015-04-21',
  4. '2015-08-18',
  5. '2015-11-26',
  6. '2015-09-15',
  7. '2015/01/07',
  8. '2015-02-11',
  9. '2015/07/14',
  10. '2015-03-02',
  11. '08/01/2015',
  12. '08-04-2015',
  13. );
  14. array_walk($dates, function(&$el){
  15. if($el[4] !== '-' && $el[4] !== '/'){
  16. // Ugly, I know, but is the fastest
  17. $el = $el[6] . $el[7] .$el[8] . $el[9] . '-' . $el[3] . $el[4] . '-' . $el[0] . $el[1];
  18. // Or just explode, revert array and implode
  19. }elseif($el[4] !== '-'){
  20. $el = str_replace('/', '-', $el);
  21. }
  22. });
  23. sort($dates);
  24. print_r($dates);

字符串
将打印:

  1. Array
  2. (
  3. [0] => 2015-01-07
  4. [1] => 2015-01-08
  5. [2] => 2015-02-11
  6. [3] => 2015-02-13
  7. [4] => 2015-03-02
  8. [5] => 2015-04-08
  9. [6] => 2015-04-21
  10. [7] => 2015-07-14
  11. [8] => 2015-08-18
  12. [9] => 2015-09-15
  13. [10] => 2015-11-26
  14. )


如果你想换个方式,就用rsort($dates)吧。

展开查看全部

相关问题