skip to Main Content

Using PHP 7.4 I have an array called $videoFiles which contains paths to video files like this

/www/nginx/html/camera1/today/event69-27062023_115101.mp4

The file name format is "event(event no)-(DDMMYY_HHMMSS).mp4"

I’m trying to sort this array so I have earliest first and latest last. I tried using this function to do what I need:

<?php

usort(
      $videoFiles,
      create_function(
         '$a,$b',
         'return filemtime($a) - filemtime($b);'
      )
   );

But this didn’t give the right result. The files are still unsorted in the array. How do I get the sort done correctly?

3

Answers


  1. $files = [
        '/www/nginx/html/camera1/today/event120-27062023_115101.mp4',
        '/www/nginx/html/camera1/today/event120-27062023_113101.mp4',
        '/www/nginx/html/camera1/today/event69-27062023_115101.mp4',
        '/www/nginx/html/camera1/today/event69-27062023_113101.mp4'
    ];
    
    usort($files, function ($a, $b) {
        $dateA = substr($a, strrpos($a, '-') + 1, 15);
        $dateB = substr($b, strrpos($b, '-') + 1, 15);
    
        $dateTimeA = DateTime::createFromFormat('dmY_His', $dateA);
        $dateTimeB = DateTime::createFromFormat('dmY_His', $dateB);
    
        return $dateTimeB <=> $dateTimeA;
    });
    
    // Output
    Array
    (
        [0] => /www/nginx/html/camera1/today/event120-27062023_115101.mp4
        [1] => /www/nginx/html/camera1/today/event69-27062023_115101.mp4
        [2] => /www/nginx/html/camera1/today/event120-27062023_113101.mp4
        [3] => /www/nginx/html/camera1/today/event69-27062023_113101.mp4
    )
    
    Login or Signup to reply.
  2. Try this one. It parses the timestamps, converts them into sort-ready format and compares them:

     $videoFiles = ['/www/nginx/html/camera1/today/event69-27062023_115101.mp4',
        '/www/nginx/html/camera1/today/event69-30062023_115101.mp4',
        '/www/nginx/html/camera1/today/event69-25062023_115101.mp4'
        ];
    
    function getDateFromFilename($filename) {
        preg_match('/d{8}_d{6}/',$filename,$matches);
        list($day,$time) = explode('_',$matches[0]);
    
        $dd = substr($day,0,2);
        $mm = substr($day,2,2);
        $yy = substr($day,4,4);
    
        return $yy.$mm.$dd.$time;
    }
    
    usort($videoFiles, function ($a,$b){
    
        $aTime = getDateFromFilename($a);
        $bTime = getDateFromFilename($b);
        if ($aTime > $bTime)  return 1;
        return -1;
    });
    
    Login or Signup to reply.
  3. PHP usort(array &$array, callable $callback): true needs a callback to a function that carries out the actual comparison.

    To compare dates in your file names you first need to isolate the string that represents a date plus time, and then convert it to a DateTime object. Then a DateTime comparison can happen.

    Here is the code sample:

        function extractDate($string){
            $dateString = substr($string, strrpos($string, '-') + 1, 15);
            return DateTime::createFromFormat('dmY_his', $dateString);
        }
    
         usort($files, function ($file1,$file2){
    
            $date1 = extractDate($file1);
            $date2 = extractDate($file2);
    
            if ($date1 == $date2) {
                return 0;
            }
            return ($date1 < $date2) ? -1 : 1;
    
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search