Core\IO\Metadata::modifiedAt
$metadata->modifiedAt(): Core\Time\InstantReturns when a file was last changed.
$info->modifiedAt() works on the Core\IO\Metadata value that Core\IO::metadata returns. It returns a Core\Time\Instant: the moment the file was last written, as the operating system recorded it. A folder has a time too.
The value is the time from the moment metadata was called. If the file is written again later, this time stays the same. Call metadata again to get the new time.
A Core\Time\Instant has no time zone. You can compare two of them with compareTo, and Core\Time::now()->since(...) returns how long ago the file was changed.
Return value
Returns Core\Time\Instant. The modification time as an exact point in time, with no time zone.
Examples
When the file was changed
<?nvs// Shows how to read when a file was last changed from the result of `Core\IO::metadata`.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'settings.json');Core\IO::write($path, "{\"theme\": \"dark\"}\n");
// `modifiedAt` returns a `Core\Time\Instant`.// `since` returns the time from that moment to now, as a `Core\Time\Duration`.Core\Time\Instant $changed = Core\IO::metadata($path)->modifiedAt();Core\Time\Duration $age = Core\Time::now()->since($changed);
// The file was written a moment ago, so this prints "changed less than a minute ago".if ($age->toSeconds() < 60) { echo 'changed less than a minute ago', "\n";} else { echo 'changed ', $age->toString(), ' ago', "\n";}changed less than a minute agoThe time is kept
<?nvs// Shows that `modifiedAt` returns the time from the moment `Core\IO::metadata` was called.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'draft.txt');Core\IO::write($path, "first version\n");var $before = Core\IO::metadata($path);Core\Time\Instant $first = $before->modifiedAt();
// The program waits a moment and then changes the file.Core\Time::sleep(Core\Time\Duration::milliseconds(50));Core\IO::write($path, "second version\n");var $after = Core\IO::metadata($path);
// `$before` still has the old time. This prints "the old result did not change".if ($before->modifiedAt()->compareTo($first) == 0) { echo 'the old result did not change', "\n";}
// A new call to `metadata` has the new time. This prints "the new result is later".if ($after->modifiedAt()->compareTo($first) > 0) { echo 'the new result is later', "\n";}the old result did not changethe new result is laterFind the newest backup
<?nvs// Shows how a program finds the newest file in a folder of backups.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'backups');Core\IO::makeDirectory($dir);
// The program writes three backups, with a short wait between them.// The last one written is "monday.sql".array<string> $names = ['friday.sql', 'sunday.sql', 'monday.sql'];foreach ($names as string $name) { Core\IO::write(Core\Path::join($dir, $name), "-- backup\n"); Core\Time::sleep(Core\Time\Duration::milliseconds(50));}
// The program starts with the first file. The loop calls `metadata` once for each// file and keeps the name with the latest time.array<string> $files = Core\IO::list($dir);string $newest = $files[0];Core\Time\Instant $latest = Core\IO::metadata(Core\Path::join($dir, $newest))->modifiedAt();foreach ($files as string $name) { Core\Time\Instant $changed = Core\IO::metadata(Core\Path::join($dir, $name))->modifiedAt(); if ($changed->compareTo($latest) > 0) { $latest = $changed; $newest = $name; }}
// This prints "newest backup: monday.sql".echo 'newest backup: ', $newest, "\n";newest backup: monday.sql