我无法对dataofbirth进行排序

时间:2016-04-15 05:27:53

标签: php arrays sorting

我无法对DateOfBirth进行排序。这是下面给出的数组。

Array ( [DateOfBirth] => 17/December/1973 ) 
Array ( [DateOfBirth] => 4/June/1981 )  
Array ( [DateOfBirth] => 2/March/1980 ) 
Array ( [DateOfBirth] => 27/April/1970 ) 
Array ( [DateOfBirth] => 9/October/1979 ) 
Array ( [DateOfBirth] => 6/June/1979 )  
Array ( [DateOfBirth] => 16/October/1991 )

请帮我解释如何对dateofbith细节进行排序。谢谢你。

3 个答案:

答案 0 :(得分:0)

您的查询应该像

"SELECT DateOfBirth FROM table_name ORDER BY DateOfBirth DESC"

这将按出生顺序对出生日期进行排序。

答案 1 :(得分:0)

嘿,如果您可以询问任何查询,可以使用以下方式对日期进行排序

<?php

$dates = array("17 December 1973", "16 October 1991", "4 June 1981");

$clength = count($dates);
for($x = 0; $x <  $clength; $x++) {

 $unix[]=strtotime($dates[$x]);

}

sort($unix);

$clength2 = count($unix);
for($n = 0; $n<  $clength2; $n++) {


  $sort_date[] = date('d-F-Y ', strtotime(gmdate("d-m-Y", $unix[$n]) . ' +1      day'));


 }

   echo "unsorted date  array ";print_r($dates);//this will give source array      date value

 echo "<br>";
  echo "<br>";
 echo "<br>"; 
echo "sorted date  array   ";print_r($sort_date); //this will give sorted    date


?>

答案 2 :(得分:0)

以下是使用完整月份时要使用的格式日期。将日期格式更改为d-M-Y。这是排序日期工作正常

<?php
$data = array(
array(
    "date"  => "17-December-1973"
),
array(
    "date"  => "4-June-1981"
),
array(
    "date"  => "2-March-1980"
),
array(
    "date"  => "27-April-1970"
),
array(
    "date"  => "9-October-1979"
)
);

function sortFunction( $a, $b ) {
 return strtotime($a["date"]) - strtotime($b["date"]);
}
usort($data, "sortFunction");

foreach($data as $key)
 echo $key["date"]."<br/>";
?>