Ember_Celica

Date sorting ascending

Nov 17th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1.   /*
  2.    * Function for sorting time by converting each time type (Seconds, Hours, Days, Weeks, Months, Years) to Hours
  3.    * Then stores the original value in a new temp array as a key, and the hour value as the actual value.
  4.    * Next calls the asort() function to sort it low->high, clears the original array, and updates it with the sorted info.
  5.    */
  6.     function SortTime($a){
  7.         $a_temp = [];
  8.         $second = '/\d+\s+second(\s|s|)/i';
  9.         $hour = '/\d+\s+hour(\s|s|)/i';
  10.         $day = '/\d+\s+day(\s|s|)/i';
  11.         $week = '/\d+\s+week(\s|s|)/i';
  12.         $month = '/\d+\s+month(\s|s|)/i';
  13.         $year = '/\d+\s+year(\s|s|)/i';
  14.         $digit = '/\d+/i';
  15.         foreach($a as $a_item){
  16.             $valueTotal = 0;
  17.                 if(preg_match($year, $a_item,$yearARR)){
  18.                     preg_match($digit,$yearARR[0],$yearNum);
  19.                     $valueTotal += ($yearNum[0]*365*24);
  20.                 }
  21.                 if(preg_match($month, $a_item,$monthARR)){
  22.                     preg_match($digit,$monthARR[0],$monthNum);
  23.                     $valueTotal += ($monthNum[0]*30*24);
  24.                 }
  25.                 if(preg_match($week, $a_item,$weekARR)){
  26.                     preg_match($digit,$weekARR[0],$weekNum);
  27.                     $valueTotal += ($weekNum[0]*7*24);
  28.                 }
  29.                 if(preg_match($day, $a_item,$dayARR)){
  30.                     preg_match($digit,$dayARR[0],$dayNum);
  31.                     $valueTotal += ($dayNum[0]*24);
  32.                 }
  33.                 if(preg_match($hour, $a_item,$hourARR)){
  34.                     preg_match($digit,$hourARR[0],$hourNum);
  35.                     $valueTotal += $hourNum[0];
  36.                 }
  37.                 if(preg_match($second, $a_item,$secondARR)){
  38.                     preg_match($digit,$secondARR[0],$secondNum);
  39.                     $valueTotal += ($secondNum[0]/60/60);
  40.                 }
  41.             $a_temp[$a_item] = $valueTotal;
  42.         }
  43.         asort($a_temp);
  44.         $a=[];
  45.         return($a_temp);
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment