3

I am unable to sort the DateOfBirth. This is the array in given below.

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 )

Please help me how to sort the dateofbith details. Advance thanks you.

Santosh Ram Kunjir
  • 1,074
  • 1
  • 12
  • 23
sreedhar
  • 31
  • 5

3 Answers3

0

Your query shgould be like

"SELECT DateOfBirth FROM table_name ORDER BY DateOfBirth DESC"

This will sort the date of birth is descending order.

aarju mishra
  • 710
  • 3
  • 10
0

hey you can use following way to sort date if any query you can ask

<?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


?>
ram singh
  • 117
  • 5
0

Here is the format date to be used if you are using Full month. Change date format to d-M-Y. Here is the sorting date is worked fine

<?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/>";
?>
Gopalakrishnan
  • 957
  • 8
  • 19