How do I display folders in date of creation order?

前端 未结 3 498
说谎
说谎 2021-01-22 12:41

I am new to PHP and developing a project called BaboonHut.com, I am coding it in PHP as the best way to learn is by just diving in. Anyway to the question, the snippet of code b

3条回答
  •  终归单人心
    2021-01-22 13:34

    Using Creation Date

    $files = glob(__DIR__ . '/*', GLOB_ONLYDIR);
    
    // Using Creation Date
    usort($files, function ($a, $b) {
        return filetime($a) - filetime($b);
    });
    
    foreach($files as $file) {
        printf("%s : %s\n", date("r", fileatime($file)), $file);
    }
    
    function filetime($file) {
        return PHP_OS == "win" ? fileatime($file) : filectime($file);
    }
    

    Using Modification Date

    $files = glob(__DIR__ . '/*', GLOB_ONLYDIR);
    
    // Using Modification Date
    usort($files, function ($a, $b) {
        return filemtime($a) - filemtime($b);
    });
    
    foreach($files as $file) {
        printf("%s : %s\n", date("r", fileatime($file)), $file);
    }
    

    Note : Using $a - $b in descending other but if you want descending please use $b - $a

提交回复
热议问题