Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get actual creation time for a file in PHP on a Mac?

Tags:

file

php

time

macos

When you choose a file in Finder and hit cmd+i on a Mac, you get the time the file was (actually) created, and the time it was last modified.

My question is simply: How can I get the actual time of creation from an already existing Mac file with PHP?

Now, having researched the topic, I have read posts that say it is impossible, but in my world "impossible" only means that a thing takes a bit longer to accomplish. Workarounds and hacks are welcomed.

I do not want mtime or ctime related advice, as these only access the last time the file was updated or modified.

Also we're probably talking Mac only here, but OS independent solutions are also welcome - if they really work on all systems.

like image 883
Kebman Avatar asked May 30 '11 12:05

Kebman


People also ask

How do I find the original file creation date of my Mac?

Control-click on the file's icon and pick “Get Info” from the menu that pops up. Near the top of the Info window, you'll see two dates labelled “Created” and “Modified.” The “created” date is when the file was first made.

How do I change the creation date of a file Mac?

To change the Modified timestamp to the current date and time, type "touch -m" in Terminal, followed by one space. Then drag the file from Finder into Terminal and press "Enter." Enter a space after the time and drag the file into the Terminal window. Press "Enter" to make the change.


1 Answers

This script is the best I've managed, which wraps the command-line stat tool available on BSD to come up with the inode birthtime attribute.

// stat.php
$filename = 'test';

$stat = stat($filename);
date_default_timezone_set('America/Denver');
echo strftime("atime: %H:%M:%S\n", $stat['atime']);
echo strftime("mtime: %H:%M:%S\n", $stat['mtime']);
echo strftime("ctime: %H:%M:%S\n", $stat['ctime']);

if ($handle = popen('stat -f %B ' . escapeshellarg($filename), 'r')) {
    $btime = trim(fread($handle, 100));
    echo strftime("btime: %H:%M:%S\n", $btime);
    pclose($handle);
}

The command-line stat tool reads atime, ctime, mtime exactly like PHP's stat, but comes up with a fourth "inode birth time" parameter. The BSD stat() system call returns st_birthtime when available, but I haven't found a way to natively expose this to PHP.

$ touch test # create a file
$ stat test
..."May 30 06:16:22 2011" "May 30 06:16:22 2011" "May 30 06:16:22 2011" "May 30 06:16:11 2011"...
$ open .
$ touch test # about one minute later
$ stat test
..."May 30 06:17:04 2011" "May 30 06:17:04 2011" "May 30 06:17:04 2011" "May 30 06:16:11 2011"...

$ php stat.php
atime: 06:52:48
mtime: 06:17:04
ctime: 06:17:04
btime: 06:16:11

The following command returns a unix timestamp of only the inode birthtime, which is the best I've found so far. You can run it with popen() or proc_open()

$ stat -f %B test
1306757771
like image 102
lunixbochs Avatar answered Sep 25 '22 06:09

lunixbochs