回显文件顶部修改的日期


echoing the date modified on top of the files

我可以回显日期或修改的日期吗?在每个csv文件的顶部?我有一个如何做到这一点的想法,但没有成功。

<?php
$arrFiles = glob("../Csv_folder/*.csv");
$arrSortedFiles = array();
foreach($arrFiles as $strFileName) {
   $arrSortedFiles[$strFileName] = filemtime($strFileName);
}
arsort($arrSortedFiles);
foreach(array_keys($arrSortedFiles) as $strFileName)
{
   $file_handle = fopen($strFileName, "r");  
   while (!feof($file_handle) ) {  
      $line_of_text = fgetcsv($file_handle, 1024);  
      echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>'; 
   }  
   fclose($file_handle);  
}
?>  

使用文件时间

返回上次修改文件的时间,失败时返回FALSE。时间作为Unix时间戳返回,这适用于date()函数。

就在你开始回显你的文件之前,你可以做

echo "Time: " . date ("F d Y H:i:s.", filemtime($strFileName));

并根据需要添加换行符或表格行等。

编辑

用这个替换你的回波

echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td><td>Time: '.date ("F d Y H:i:s.", filemtime($strFileName)).'</td></tr>';

编辑2

用这个替换你的环路

foreach(array_keys($arrSortedFiles) as $strFileName)
{
   $file_handle = fopen($strFileName, "r");  
   echo "<tr><td colspan='5'></td><td>".date ("F d Y H:i:s.", filemtime($strFileName))."</td></tr>";
   while (!feof($file_handle) ) {  
      $line_of_text = fgetcsv($file_handle, 1024);  
      echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>'; 
   }  
   fclose($file_handle);  
}