Asked  7 Months ago    Answers:  5   Viewed   33 times

I currently have an index.php file which allows me to output the list of files inside the same directory, the output shows the names then I used filemtime() function to show the date when the file was modified. my problem now is, how will I sort the output to show the latest modified file ?, I've been thinking for awhile how to do this. if only I am doing it with mysql interaction there will be no problem at all. please show me an example how to sort and output the list of files starting from the latest modified one. this is what i have for now

if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
        $lastModified = date('F d Y, H:i:s',filemtime($file));
          if(strlen($file)-strpos($file,".swf")== 4){
            echo "<tr><td><input type="checkbox" name="box[]"></td><td><a href="$file" target="_blank">$file</a></td><td>$lastModified</td></tr>";
           }
       }
   }
   closedir($handle);
}

 Answers

94

You need to put the files into an array in order to sort and find the last modified file.

$files = array();
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
           $files[filemtime($file)] = $file;
        }
    }
    closedir($handle);

    // sort
    ksort($files);
    // find the last modification
    $reallyLastModified = end($files);

    foreach($files as $file) {
        $lastModified = date('F d Y, H:i:s',filemtime($file));
        if(strlen($file)-strpos($file,".swf")== 4){
           if ($file == $reallyLastModified) {
             // do stuff for the real last modified file
           }
           echo "<tr><td><input type="checkbox" name="box[]"></td><td><a href="$file" target="_blank">$file</a></td><td>$lastModified</td></tr>";
        }
    }
}

Not tested, but that's how to do it.

Tuesday, June 1, 2021
 
Tucker
answered 7 Months ago
20

Updated On July 2020

Here is the way to do it:

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items_by_sku', 10, 3 );
function filter_order_get_items_by_sku( $items, $order, $types ) {
    if( count($items) > 1 ) {
        $item_skus = $sorted_items = array();

        // Loop through order line items
        foreach( $items as $items_id => $item ){
            // Check items type: for versions before Woocommerce 3.3
            if( $item->is_type('line_item') && method_exists( $item, 'get_product' ) ){
                $product = $item->get_product(); // Get the product Object
                if( is_a( $product, 'WC_Product' ) ) {
                    $item_skus[$product->get_sku()] = $items_id;
                }
            }
        }

        // Only for line items when our sku array is not empty
        if( ! empty($item_skus) ) {
            // Sorting in ASC order based on SKUs;
            ksort($item_skus); // or use krsort() for DESC order

            // Loop through sorted $item_skus array
            foreach( $item_skus as $sku => $item_id ){
                // Set items in the correct order
                $sorted_items[$item_id] = $items[$item_id];
            }
            $items = $sorted_items;
        }
    }
    return $items;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

This will sort items by sku order ASC on backend and frontend orders and in email notifications


Also sorting items once the order is placed before data is saved to the database, could be a better way to do it.

Wednesday, March 31, 2021
 
astaykov
answered 9 Months ago
33

Use natsort():

$array[0]= '10. xyz';
$array[1]=  '13. xyz';
$array[2]=  '2. xyz';
$array[3]=  '1a. xyz';
$array[4]=  '6. xyz';
$array[5]=  '1b. xyz';
$array[6]=  '4a. xyz';
$array[7]=  '4b. xyz';

natsort($array);

print_r($array);
Saturday, May 29, 2021
 
insomiac
answered 7 Months ago
61

I don't know if you can sort thru the DirectoryIterator outright by you could gather the results first, get the time then sort that then present it. Example:

$display = array('php');
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($search_path));
$data = array();
foreach($files as $file) {
    $time = DateTime::createFromFormat('U', filemtime($file->getPathname()));
    // no need to explode the time, just make it a datetime object
    if(in_array($file->getExtension(), $display) && $time > new DateTime('2014-01-15')) { // is PHP and is greater than jan 15 2014
        $data[] = array('filename' => $file->getPathname(), 'time' => $time->getTimestamp()); // push inside
    }

}
usort($data, function($a, $b){ // sort by time latest
    return $b['time'] - $a['time'];
});


foreach ($data as $key => $value) {
    $time = date('Y-m-d', $value['time']);
    echo "
        <span style='color: red;'>
            <b style='color: green;'>$time</b>$value[filename]
        </span>
        <br/>
    ";
}
Saturday, May 29, 2021
 
rorymorris
answered 7 Months ago
61
added.sort(key=lambda x: os.stat(os.path.join(path_to_watch, x)).st_mtime)

Will sort the added list by the last modified time of the files

Use st_ctime instaed of st_mtime for creation time on Windows (it doesn't mean that on other platforms).

Tuesday, August 10, 2021
 
Denis
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share