PHP:如何对包含日期的字符串进行排序?

huangapple go评论64阅读模式
英文:

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:

&lt;?php

usort(
      $videoFiles,
      create_function(
         &#39;$a,$b&#39;,
         &#39;return filemtime($a) - filemtime($b);&#39;
      )
   );

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 &amp;$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 &amp;$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, &#39;-&#39;) + 1, 15);
        return DateTime::createFromFormat(&#39;dmY_his&#39;, $dateString);
    }

     usort($files, function ($file1,$file2){

        $date1 = extractDate($file1);
        $date2 = extractDate($file2);

        if ($date1 == $date2) {
            return 0;
        }
        return ($date1 &lt; $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 = [
    &#39;/www/nginx/html/camera1/today/event120-27062023_115101.mp4&#39;,
    &#39;/www/nginx/html/camera1/today/event120-27062023_113101.mp4&#39;,
    &#39;/www/nginx/html/camera1/today/event69-27062023_115101.mp4&#39;,
    &#39;/www/nginx/html/camera1/today/event69-27062023_113101.mp4&#39;
];

usort($files, function ($a, $b) {
    $dateA = substr($a, strrpos($a, &#39;-&#39;) + 1, 15);
    $dateB = substr($b, strrpos($b, &#39;-&#39;) + 1, 15);

    $dateTimeA = DateTime::createFromFormat(&#39;dmY_His&#39;, $dateA);
    $dateTimeB = DateTime::createFromFormat(&#39;dmY_His&#39;, $dateB);

    return $dateTimeB &lt;=&gt; $dateTimeA;
});

// Output
Array
(
    [0] =&gt; /www/nginx/html/camera1/today/event120-27062023_115101.mp4
    [1] =&gt; /www/nginx/html/camera1/today/event69-27062023_115101.mp4
    [2] =&gt; /www/nginx/html/camera1/today/event120-27062023_113101.mp4
    [3] =&gt; /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 = [&#39;/www/nginx/html/camera1/today/event69-27062023_115101.mp4&#39;,
    &#39;/www/nginx/html/camera1/today/event69-30062023_115101.mp4&#39;,
    &#39;/www/nginx/html/camera1/today/event69-25062023_115101.mp4&#39;
    ];

function getDateFromFilename($filename) {
    preg_match(&#39;/\d{8}\_\d{6}/&#39;,$filename,$matches);
    list($day,$time) = explode(&#39;_&#39;,$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 &gt; $bTime)  return 1;
    return -1;
});

huangapple
  • 本文由 发表于 2023年6月27日 19:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定