glob() - sort array of files by last modified datetime stamp
Warning
create_function()
has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
For the sake of posterity, in case the forum post linked in the accepted answer is lost or unclear to some, the relevant code needed is:
<?php
$myarray = glob("*.*");
usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
?>
Tested this on my system and verified it does sort by file mtime as desired. I used a similar approach (written in Python) for determining the last updated files on my website as well.
How to order filenames on last modified date glob PHP
This works :)
$myFiles = glob("*files/*.html");
usort($myFiles, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
foreach ($myFiles as $filename) {
echo "<iframe src=$filename></iframe>";
}
Glob search files in date order?
To sort files by date:
import glob
import os
files = glob.glob("*cycle*.log")
files.sort(key=os.path.getmtime)
print("\n".join(files))
See also Sorting HOW TO.
How do you get a directory listing sorted by creation date in python?
Update: to sort dirpath
's entries by modification date in Python 3:
import os
from pathlib import Path
paths = sorted(Path(dirpath).iterdir(), key=os.path.getmtime)
(put @Pygirl's answer here for greater visibility)
If you already have a list of filenames files
, then to sort it inplace by creation time on Windows (make sure that list contains absolute path):
files.sort(key=os.path.getctime)
The list of files you could get, for example, using glob
as shown in @Jay's answer.
old answer
Here's a more verbose version of @Greg Hewgill
's answer. It is the most conforming to the question requirements. It makes a distinction between creation and modification dates (at least on Windows).
#!/usr/bin/env python
from stat import S_ISREG, ST_CTIME, ST_MODE
import os, sys, time
# path to the directory (relative or absolute)
dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.'
# get all entries in the directory w/ stats
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath))
entries = ((os.stat(path), path) for path in entries)
# leave only regular files, insert creation date
entries = ((stat[ST_CTIME], path)
for stat, path in entries if S_ISREG(stat[ST_MODE]))
#NOTE: on Windows `ST_CTIME` is a creation date
# but on Unix it could be something else
#NOTE: use `ST_MTIME` to sort by a modification date
for cdate, path in sorted(entries):
print time.ctime(cdate), os.path.basename(path)
Example:
$ python stat_creation_date.py
Thu Feb 11 13:31:07 2009 stat_creation_date.py
Related Topics
How to Solve "Fatal Error: Class 'MySQLi' Not Found"
List of Big-O For PHP Functions
Get Table Column Names in MySQL
How to Create an Array For Json Using PHP
Nginx Serves .PHP Files as Downloads, Instead of Executing Them
MySQLi Bind_Param() Expected to Be a Reference, Value Given
"The Page Has Expired Due to Inactivity" - Laravel 5.5
Why Do Some Scripts Omit the Closing PHP Tag, '≫'
Insert Blobs in MySQL Databases With PHP
How Safe Are PHP Session Variables
How to Pass Data to View in Laravel