Get File Last Updated Date

/*
 * @param string $file Filepath
 * @param string $format dateformat
 * @link http://www.php.net/manual/de/function.date.php
 * @link http://www.php.net/manual/de/function.filemtime.php
 * @return string|bool Date or Boolean
 */

function getFiledate($file, $format) {
       if (is_file($file)) {
               $filePath = $file;
               if (!realpath($filePath)) {
                       $filePath = $_SERVER["DOCUMENT_ROOT"].$filePath;
       }
               $fileDate = filemtime($filePath);
               if ($fileDate) {
                       $fileDate = date("$format",$fileDate);
                       return $fileDate;
               }
               return false;
       }
       return false;
}

Comments

  1. User Avatar
    Ibrahim
    Permalink to comment#

    Nice little helpful function. Thank you for sharing.

  2. User Avatar
    burn
    Permalink to comment#

    Just to let you know that I modified your function to check if the file date is within a given date interval:

    
    public function doFileDatesComparison($file) {
    	   if (is_file($file)) {
    		   $filePath = $file;
    		   if (!realpath($filePath)) {
    			   $filePath = $_SERVER["DOCUMENT_ROOT"] . $filePath;
    		   }
    		   $fileDate = filemtime($filePath);
    		   if ($fileDate) {
    			   $fileDate = date('U', $fileDate);
    
    			   if ($fileDate > $this->downBound && $fileDate upBound) {
    			   		return date('Y/m/d H:i:s', $fileDate);
    			   }
    		   }
    			return false;
    	   }
    	   return false;
    	}
    
    • User Avatar
      burn
      Permalink to comment#

      Whops my fault, I’m posting the method again after turning < into <

      
      public function doFileDatesComparison($file) {
      	   if (is_file($file)) {
      		   $filePath = $file;
      		   if (!realpath($filePath)) {
      			   $filePath = $_SERVER["DOCUMENT_ROOT"] . $filePath;
      		   }
      		   $fileDate = filemtime($filePath);
      		   if ($fileDate) {
      			   $fileDate = date('U', $fileDate);
      
      			   if ($fileDate > $this->downBound && $fileDate < $this->upBound) {
      			   		return date('Y/m/d H:i:s', $fileDate);
      			   }
      		   }
      			return false;
      	   }
      	   return false;
      	}
      

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag