php - Sorting an Array of Dates using asort() -


i'm confused, tried lot of related approaches sort array of dates may have different formats.

i have array of dates, example:

"0"=>"09.10.2012" "1"=>"02.10.12" "2"=>"27.09.15" "2.0"=>"28.09.2012" "2.1"=>"29.9.2012" "2.2"=>"29.09.2012" "3"=>"9.10.2012" "3.1"=>"23.4.10" "4"=>"28.09.2012" "5"=>"26.10.2012" "6"=>"12.09.98" "6.0"=>"05.03.2013" "6.1"=>"23.4.2013" 

(the keys strings reason)

now know in same format order - days, month, years . digits number can change can see in given array.

i parsed them day-month-year (european format strtotime() recognize according documentation) , changed them unix time-stamp, i'm sorting array using asort() , received bad results:

[6]->[] -- 12.09.98 [1]->[1034380800] -- 02.10.12 [2.0]->[1348790400] -- 28.09.2012 [4]->[1348790400] -- 28.09.2012 [2.2]->[1348876800] -- 29.09.2012 [2.1]->[1348876800] -- 29.9.2012 [3]->[1349740800] -- 9.10.2012 [0]->[1349740800] -- 09.10.2012 [5]->[1351209600] -- 26.10.2012 [6.0]->[1362441600] -- 05.03.2013 [6.1]->[1366675200] -- 23.4.2013 [3.1]->[1681084800] -- 23.4.10 [2]->[1820966400] -- 27.09.15 

as can see [6](unixtime) contains false , strtotime() failing converting dates.

here code:

function sortarrays_bydate($target){          foreach($target $key_s => $val_s) { $date_exp = preg_replace('#(\.|_)#','-',$val_s); $target[(string)$key_s] = $date_exp; }          foreach($target $key_s => $val_s) { $date_exp = strtotime($val_s); $target[(string)$key_s] = $date_exp; }          asort($target);          return $target;  } 

can 1 please explain me wrong...

thanks

for reason european - didn't worked 98 recommend edit year 4 digit number format:

    function sortarrays_bydate($target){          foreach($target $key_s => $val_s) {             $day = substr($val_s, 0, strpos($val_s, '.'));             $month = substr($val_s, strpos($val_s, '.')+1, strrpos($val_s, '.')-strpos($val_s, '.')-1);             $year = substr($val_s,  strrpos($val_s, '.')+1);              if($year > 79 && $year <= 99)                 $year = "19" . $year;             elseif($year >=00 && $year <= 79)                 $year = "20" . $year;              $target[(string)$key_s] = strtotime($day .'.'. $month .'.'. $year);         }          asort($target);          /*foreach($target $value)             echo date("d.m.y", $value) . '</br>';         changed edit time d.m.y ->         */          foreach($target $key_s => $val_s)         $target[(string)$key_s] = date("d.m.y", $target[(string)$key_s]);          return $target; } 

then worked me, , array sorted correctly.

edit //

result is:

array (     [0] => 09.10.2012     [1] => 02.10.12     [2] => 27.09.15     [2.0] => 28.09.2012     [2.1] => 29.9.2012     [2.2] => 29.09.2012     [3] => 9.10.2012     [3.1] => 23.4.10     [4] => 28.09.2012     [5] => 26.10.2012     [6] => 12.09.98     [6.0] => 05.03.2013     [6.1] => 23.4.2013 ) array (     [6] => 12.09.1998     [3.1] => 23.04.2010     [4] => 28.09.2012     [2.0] => 28.09.2012     [2.1] => 29.09.2012     [2.2] => 29.09.2012     [1] => 02.10.2012     [3] => 09.10.2012     [0] => 09.10.2012     [5] => 26.10.2012     [6.0] => 05.03.2013     [6.1] => 23.04.2013     [2] => 27.09.2015 ) 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -