當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP DirectoryIterator getMTime()用法及代碼示例


DirectoryIterator::getMTime()函數是PHP中的內置函數,用於返回當前DirectoryIterator項的最後修改時間。

用法:

int DirectoryIterator::getMTime( void )

參數:該函數不接受任何參數。


返回值:此函數以Unix時間戳格式返回文件的最後修改時間。

以下示例程序旨在說明PHP中的DirectoryIterator::getMTime()函數:

示例1:

<?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(); 
  
        // Displaty the filename and last modified time 
        echo $file->getFilename() . " | MTime: "
                . $directory->getMTime() . "<br>"; 
    } 
  
    // Move to the next element of directory 
    $directory->next(); 
} 
?>

輸出:

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

示例2:

<?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>"; 
} 
?> 

輸出:

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 => geeks.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

注意:此函數的輸出取決於服務器文件夾的內容。

參考: https://www.php.net/manual/en/directoryiterator.getmtime.php



相關用法


注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | DirectoryIterator getMTime() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。