Thursday, June 18, 2026
HomeLanguagesPHP | DirectoryIterator getMTime() Function

PHP | DirectoryIterator getMTime() Function

The DirectoryIterator::getMTime() function is an inbuilt function in PHP which is used to returns the last modification time of the current DirectoryIterator item.

Syntax:

int DirectoryIterator::getMTime( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns the last modification time of the file in Unix timestamp format.

Below programs illustrate the DirectoryIterator::getMTime() function in PHP:

Program 1:

php




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
  
    // Check it is directory or not
    if ($directory->isDir()) {
  
        $file = $directory->current();
  
        // Display the filename and last modified time
        echo $file->getFilename() . " | MTime: "
                . $directory->getMTime() . "<br>";
    }
  
    // Move to the next element of directory
    $directory->next();
}
?>


Output:

. | MTime: 1574616421
.. | MTime: 1574540515
dashboard | MTime: 1574350724
img | MTime: 1574350724
webalizer | MTime: 1574350718
xampp | MTime: 1574350724

Program 2:

php




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs for each element of directory
foreach($directory as $dir) {
      
    // Store the current element of directory
    $file = $directory->current();
      
    // Display the key, filename and last modified time
    echo $dir->key() . " => " . 
        $file->getFilename() . " | MTime: " .
        $dir->getMTime() . "<br>";
}
?> 


Output:

0 => . | MTime: 1574616421
1 => .. | MTime: 1574540515
2 => applications.html | MTime: 1566914572
3 => bitnami.css | MTime: 1566914572
4 => dashboard | MTime: 1574350724
5 => favicon.ico | MTime: 1437060752
6 => neveropen.PNG | MTime: 1573806007
7 => gfg.php | MTime: 1574653649
8 => img | MTime: 1574350724
9 => index.php | MTime: 1437060752
10 => webalizer | MTime: 1574350718
11 => xampp | MTime: 1574350724

Note: The output of this function depends on the content of server folder.

Reference: https://www.php.net/manual/en/directoryiterator.getmtime.php

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6965 POSTS0 COMMENTS