Hello all.
I was looking for a way to make some grafical stats of recieved files on my honeypot.
After thinking on the way to do so, i understood that the easiest way would be parsing the filename with regullar expression or using dir() command.
But for parsing the data i didnt find a way to do it with shell or directly with php, and thought that "ls -l" outputs the creation date, all fine till that point, but the problem arises here, i was thinking on parsing the date as yyyy-mm-dd, but when i do "ls -l" in php the output is given as "Sep 19" for example.
So is there a way in php to see the creation date of the file or to output directly the command from the linux so it will show "yyyy-mm-dd" .
Thanks in advance.

Recommended Answers

All 4 Replies

It's all in the date command.

Just execute this from shell:

$ date +%Y-%m-%d

Or in your bash script:
FormattedDate=$(date +%Y-%m-%d)
echo $FormattedDate

of course.. the way to your holy grail is

$ man date

The ideia was to see the date of file created. And not the current date.

You can get the file modification time in PHP:

http://www.php.net/filemtime

$mtime = filemtime('/path/to/file');

To get the files in the directory, you can use glob()
http://us.php.net/manual/en/function.glob.php

Or with PHP5, use RecursiveDirectoryIterator class.

eg:

$dir_iterator = new RecursiveDirectoryIterator("/path");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wish

foreach ($iterator as $file) {
    echo $file->getMTime()."\n";
}

see: http://us.php.net/manual/en/function.glob.php#92710

When you use the RecursiveDirectoryIterator class, each file becomes an instance of: SplFileInfo class.

So you can access the file using the methods defined for SplFileInfo.
http://us3.php.net/manual/en/class.splfileinfo.php

It's all in the date command.

Just execute this from shell:

$ date +%Y-%m-%d

Or in your bash script:
FormattedDate=$(date +%Y-%m-%d)
echo $FormattedDate

of course.. the way to your holy grail is

$ man date

The date command also accepts a file path using the -r option, taking its last modification time as the input date.

eg:

FilePath=/path/to/file
FormattedDate=$(date -r $FilePath +%Y-%m-%d)
echo $FormattedDate

You could also parse the date out of the ls -l with sed, awk, grep etc. and pass that to: date -d

Ok, will use the date, and will try to read man's more intensivly.
Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.