×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Text
Posted by: bejoy balan
Added: Nov 8, 2017 7:34 AM
Views: 1
Tags: file sort time
_sortFilesByTime
  1.      */
  2.     protected function _sortFilesByTime($location, $regex = '[vV][oO][uU][cC][hH][eE][rR][fF][iI][lL][eE]*')
  3.     {
  4.         //Yes string matching, we need to trust the naming convention at some point
  5.         $files = glob($location . $regex);
  6.  
  7.         $times = array();
  8.         foreach ($files as $file) {
  9.             $timestr = str_ireplace($location . 'voucherfile_', '', $file);
  10.             $timestr = str_ireplace('.csv', '', $timestr);
  11.             $time_obj = DateTime::createFromFormat('Ymd_Gis', $timestr);
  12.             $times[] = array('date' => $time_obj, 'file' => $file);
  13.         }
  14.         usort($times, function ($a, $b) {
  15.             $aDate = $a['date'];
  16.             $bDate = $b['date'];
  17.             if ($aDate == $bDate) {
  18.                 return 0;
  19.             }
  20.             return $aDate < $bDate ? 1 : -1;
  21.         });
  22.         return $times;
  23.     }