英文:
PHP: How do I sort strings which contain a date?
问题
使用PHP 7.4,我有一个名为$videoFiles的数组,其中包含视频文件的路径,格式如下:
/www/nginx/html/camera1/today/event69-27062023_115101.mp4
文件名的格式是"event(event no)-(DDMMYY_HHMMSS).mp4"。
我试图对这个数组进行排序,使最早的文件排在前面,最晚的文件排在后面。我尝试使用以下函数来实现我的需求:
<?php
usort(
      $videoFiles,
      create_function(
         '$a,$b',
         'return filemtime($a) - filemtime($b);'
      )
   );
但这并没有得到正确的结果。文件在数组中仍然是未排序的。如何正确地进行排序?
英文:
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?
答案1
得分: 1
PHP usort(array &$array, callable $callback): true 需要一个回调函数来执行实际的比较。
要比较文件名中的日期,首先需要分离表示日期和时间的字符串,然后将其转换为 DateTime 对象。然后可以进行 DateTime 比较。
以下是代码示例:
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;
});
英文:
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;
    });
答案2
得分: 0
以下是翻译好的部分:
$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
)
英文:
$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
)
答案3
得分: 0
尝试这个。它解析时间戳,将其转换为可排序的格式并进行比较:
 $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;
});
英文:
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;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论