CINXE.COM

PHP: filemtime - Manual

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP: filemtime - Manual </title> <link rel="icon" type="image/svg+xml" sizes="any" href="https://www.php.net/favicon.svg?v=2"> <link rel="icon" type="image/png" sizes="196x196" href="https://www.php.net/favicon-196x196.png?v=2"> <link rel="icon" type="image/png" sizes="32x32" href="https://www.php.net/favicon-32x32.png?v=2"> <link rel="icon" type="image/png" sizes="16x16" href="https://www.php.net/favicon-16x16.png?v=2"> <link rel="shortcut icon" href="https://www.php.net/favicon.ico?v=2"> <link rel="search" type="application/opensearchdescription+xml" href="http://php.net/phpnetimprovedsearch.src" title="Add PHP.net search"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/releases/feed.php" title="PHP Release feed"> <link rel="alternate" type="application/atom+xml" href="https://www.php.net/feed.atom" title="PHP: Hypertext Preprocessor"> <link rel="canonical" href="https://www.php.net/manual/en/function.filemtime.php"> <link rel="shorturl" href="https://www.php.net/filemtime"> <link rel="alternate" href="https://www.php.net/filemtime" hreflang="x-default"> <link rel="contents" href="https://www.php.net/manual/en/index.php"> <link rel="index" href="https://www.php.net/manual/en/ref.filesystem.php"> <link rel="prev" href="https://www.php.net/manual/en/function.fileinode.php"> <link rel="next" href="https://www.php.net/manual/en/function.fileowner.php"> <link rel="alternate" href="https://www.php.net/manual/en/function.filemtime.php" hreflang="en"> <link rel="alternate" href="https://www.php.net/manual/de/function.filemtime.php" hreflang="de"> <link rel="alternate" href="https://www.php.net/manual/es/function.filemtime.php" hreflang="es"> <link rel="alternate" href="https://www.php.net/manual/fr/function.filemtime.php" hreflang="fr"> <link rel="alternate" href="https://www.php.net/manual/it/function.filemtime.php" hreflang="it"> <link rel="alternate" href="https://www.php.net/manual/ja/function.filemtime.php" hreflang="ja"> <link rel="alternate" href="https://www.php.net/manual/pt_BR/function.filemtime.php" hreflang="pt_BR"> <link rel="alternate" href="https://www.php.net/manual/ru/function.filemtime.php" hreflang="ru"> <link rel="alternate" href="https://www.php.net/manual/tr/function.filemtime.php" hreflang="tr"> <link rel="alternate" href="https://www.php.net/manual/uk/function.filemtime.php" hreflang="uk"> <link rel="alternate" href="https://www.php.net/manual/zh/function.filemtime.php" hreflang="zh"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&amp;f=/fonts/Fira/fira.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1707321815&amp;f=/fonts/Font-Awesome/css/fontello.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1738926002&amp;f=/styles/theme-base.css" media="screen"> <link rel="stylesheet" type="text/css" href="/cached.php?t=1730558402&amp;f=/styles/theme-medium.css" media="screen"> <base href="https://www.php.net/manual/en/function.filemtime.php"> <meta name="Description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:site" content="@official_php" /> <meta name="twitter:title" content="PHP: Hypertext Preprocessor" /> <meta name="twitter:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta name="twitter:creator" content="@official_php" /> <meta name="twitter:image:src" content="https://www.php.net/images/meta-image.png" /> <meta itemprop="name" content="PHP: Hypertext Preprocessor" /> <meta itemprop="description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <meta itemprop="image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:image" content="https://www.php.net/images/meta-image.png" /> <meta property="og:description" content="PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world." /> <link href="https://fosstodon.org/@php" rel="me" /> <!-- Matomo --> <script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://analytics.php.net/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code --> </head> <body class="docs "> <nav class="navbar navbar-fixed-top"> <div class="navbar__inner"> <a href="/" aria-label="PHP Home" class="navbar__brand"> <img src="/images/logos/php-logo-white.svg" aria-hidden="true" width="80" height="40" > </a> <div id="navbar__offcanvas" tabindex="-1" class="navbar__offcanvas" aria-label="Menu" > <button id="navbar__close-button" class="navbar__icon-item navbar_icon-item--visually-aligned navbar__close-button" > <svg xmlns="http://www.w3.org/2000/svg" width="24" viewBox="0 0 24 24" fill="currentColor"><path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" /></svg> </button> <ul class="navbar__nav"> <li class="navbar__item"> <a href="/downloads.php" class="navbar__link " > Downloads </a> </li> <li class="navbar__item"> <a href="/docs.php" aria-current="page" class="navbar__link navbar__link--active " > Documentation </a> </li> <li class="navbar__item"> <a href="/get-involved.php" class="navbar__link " > Get Involved </a> </li> <li class="navbar__item"> <a href="/support.php" class="navbar__link " > Help </a> </li> <li class="navbar__item"> <a href="/releases/8.4/index.php" class="navbar__link navbar__release" > <img src="/images/php8/logo_php8_4.svg" alt="PHP 8.4"> </a> </li> </ul> </div> <div class="navbar__right"> <!-- Desktop default search --> <form action="/manual-lookup.php" class="navbar__search-form" > <label for="navbar__search-input" aria-label="Search docs"> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </label> <input type="search" name="pattern" id="navbar__search-input" class="navbar__search-input" placeholder="Search docs" accesskey="s" > <input type="hidden" name="scope" value="quickref"> </form> <!-- Desktop encanced search --> <button id="navbar__search-button" class="navbar__search-button" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> Search docs </button> <!-- Mobile default items --> <a id="navbar__search-link" href="/lookup-form.php" aria-label="Search docs" class="navbar__icon-item navbar__search-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </a> <a id="navbar__menu-link" href="/menu.php" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned navbar_menu-link" > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </a> <!-- Mobile enhanced items --> <button id="navbar__search-button-mobile" aria-label="Search docs" class="navbar__icon-item navbar__search-button-mobile" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </button> <button id="navbar__menu-button" aria-label="Menu" class="navbar__icon-item navbar_icon-item--visually-aligned" hidden > <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="currentColor" > <path d="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" /> </svg> </button> </div> <div id="navbar__backdrop" class="navbar__backdrop" ></div> </div> <div id="flash-message"></div> </nav> <nav id="trick"><div><dl> <dt><a href='/manual/en/getting-started.php'>Getting Started</a></dt> <dd><a href='/manual/en/introduction.php'>Introduction</a></dd> <dd><a href='/manual/en/tutorial.php'>A simple tutorial</a></dd> <dt><a href='/manual/en/langref.php'>Language Reference</a></dt> <dd><a href='/manual/en/language.basic-syntax.php'>Basic syntax</a></dd> <dd><a href='/manual/en/language.types.php'>Types</a></dd> <dd><a href='/manual/en/language.variables.php'>Variables</a></dd> <dd><a href='/manual/en/language.constants.php'>Constants</a></dd> <dd><a href='/manual/en/language.expressions.php'>Expressions</a></dd> <dd><a href='/manual/en/language.operators.php'>Operators</a></dd> <dd><a href='/manual/en/language.control-structures.php'>Control Structures</a></dd> <dd><a href='/manual/en/language.functions.php'>Functions</a></dd> <dd><a href='/manual/en/language.oop5.php'>Classes and Objects</a></dd> <dd><a href='/manual/en/language.namespaces.php'>Namespaces</a></dd> <dd><a href='/manual/en/language.enumerations.php'>Enumerations</a></dd> <dd><a href='/manual/en/language.errors.php'>Errors</a></dd> <dd><a href='/manual/en/language.exceptions.php'>Exceptions</a></dd> <dd><a href='/manual/en/language.fibers.php'>Fibers</a></dd> <dd><a href='/manual/en/language.generators.php'>Generators</a></dd> <dd><a href='/manual/en/language.attributes.php'>Attributes</a></dd> <dd><a href='/manual/en/language.references.php'>References Explained</a></dd> <dd><a href='/manual/en/reserved.variables.php'>Predefined Variables</a></dd> <dd><a href='/manual/en/reserved.exceptions.php'>Predefined Exceptions</a></dd> <dd><a href='/manual/en/reserved.interfaces.php'>Predefined Interfaces and Classes</a></dd> <dd><a href='/manual/en/reserved.attributes.php'>Predefined Attributes</a></dd> <dd><a href='/manual/en/context.php'>Context options and parameters</a></dd> <dd><a href='/manual/en/wrappers.php'>Supported Protocols and Wrappers</a></dd> </dl> <dl> <dt><a href='/manual/en/security.php'>Security</a></dt> <dd><a href='/manual/en/security.intro.php'>Introduction</a></dd> <dd><a href='/manual/en/security.general.php'>General considerations</a></dd> <dd><a href='/manual/en/security.cgi-bin.php'>Installed as CGI binary</a></dd> <dd><a href='/manual/en/security.apache.php'>Installed as an Apache module</a></dd> <dd><a href='/manual/en/security.sessions.php'>Session Security</a></dd> <dd><a href='/manual/en/security.filesystem.php'>Filesystem Security</a></dd> <dd><a href='/manual/en/security.database.php'>Database Security</a></dd> <dd><a href='/manual/en/security.errors.php'>Error Reporting</a></dd> <dd><a href='/manual/en/security.variables.php'>User Submitted Data</a></dd> <dd><a href='/manual/en/security.hiding.php'>Hiding PHP</a></dd> <dd><a href='/manual/en/security.current.php'>Keeping Current</a></dd> <dt><a href='/manual/en/features.php'>Features</a></dt> <dd><a href='/manual/en/features.http-auth.php'>HTTP authentication with PHP</a></dd> <dd><a href='/manual/en/features.cookies.php'>Cookies</a></dd> <dd><a href='/manual/en/features.sessions.php'>Sessions</a></dd> <dd><a href='/manual/en/features.file-upload.php'>Handling file uploads</a></dd> <dd><a href='/manual/en/features.remote-files.php'>Using remote files</a></dd> <dd><a href='/manual/en/features.connection-handling.php'>Connection handling</a></dd> <dd><a href='/manual/en/features.persistent-connections.php'>Persistent Database Connections</a></dd> <dd><a href='/manual/en/features.commandline.php'>Command line usage</a></dd> <dd><a href='/manual/en/features.gc.php'>Garbage Collection</a></dd> <dd><a href='/manual/en/features.dtrace.php'>DTrace Dynamic Tracing</a></dd> </dl> <dl> <dt><a href='/manual/en/funcref.php'>Function Reference</a></dt> <dd><a href='/manual/en/refs.basic.php.php'>Affecting PHP's Behaviour</a></dd> <dd><a href='/manual/en/refs.utilspec.audio.php'>Audio Formats Manipulation</a></dd> <dd><a href='/manual/en/refs.remote.auth.php'>Authentication Services</a></dd> <dd><a href='/manual/en/refs.utilspec.cmdline.php'>Command Line Specific Extensions</a></dd> <dd><a href='/manual/en/refs.compression.php'>Compression and Archive Extensions</a></dd> <dd><a href='/manual/en/refs.crypto.php'>Cryptography Extensions</a></dd> <dd><a href='/manual/en/refs.database.php'>Database Extensions</a></dd> <dd><a href='/manual/en/refs.calendar.php'>Date and Time Related Extensions</a></dd> <dd><a href='/manual/en/refs.fileprocess.file.php'>File System Related Extensions</a></dd> <dd><a href='/manual/en/refs.international.php'>Human Language and Character Encoding Support</a></dd> <dd><a href='/manual/en/refs.utilspec.image.php'>Image Processing and Generation</a></dd> <dd><a href='/manual/en/refs.remote.mail.php'>Mail Related Extensions</a></dd> <dd><a href='/manual/en/refs.math.php'>Mathematical Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.nontext.php'>Non-Text MIME Output</a></dd> <dd><a href='/manual/en/refs.fileprocess.process.php'>Process Control Extensions</a></dd> <dd><a href='/manual/en/refs.basic.other.php'>Other Basic Extensions</a></dd> <dd><a href='/manual/en/refs.remote.other.php'>Other Services</a></dd> <dd><a href='/manual/en/refs.search.php'>Search Engine Extensions</a></dd> <dd><a href='/manual/en/refs.utilspec.server.php'>Server Specific Extensions</a></dd> <dd><a href='/manual/en/refs.basic.session.php'>Session Extensions</a></dd> <dd><a href='/manual/en/refs.basic.text.php'>Text Processing</a></dd> <dd><a href='/manual/en/refs.basic.vartype.php'>Variable and Type Related Extensions</a></dd> <dd><a href='/manual/en/refs.webservice.php'>Web Services</a></dd> <dd><a href='/manual/en/refs.utilspec.windows.php'>Windows Only Extensions</a></dd> <dd><a href='/manual/en/refs.xml.php'>XML Manipulation</a></dd> <dd><a href='/manual/en/refs.ui.php'>GUI Extensions</a></dd> </dl> <dl> <dt>Keyboard Shortcuts</dt><dt>?</dt> <dd>This help</dd> <dt>j</dt> <dd>Next menu item</dd> <dt>k</dt> <dd>Previous menu item</dd> <dt>g p</dt> <dd>Previous man page</dd> <dt>g n</dt> <dd>Next man page</dd> <dt>G</dt> <dd>Scroll to bottom</dd> <dt>g g</dt> <dd>Scroll to top</dd> <dt>g h</dt> <dd>Goto homepage</dd> <dt>g s</dt> <dd>Goto search<br>(current page)</dd> <dt>/</dt> <dd>Focus search box</dd> </dl></div></nav> <div id="goto"> <div class="search"> <div class="text"></div> <div class="results"><ul></ul></div> </div> </div> <div id="breadcrumbs" class="clearfix"> <div id="breadcrumbs-inner"> <div class="next"> <a href="function.fileowner.php"> fileowner &raquo; </a> </div> <div class="prev"> <a href="function.fileinode.php"> &laquo; fileinode </a> </div> <ul> <li><a href='index.php'>PHP Manual</a></li> <li><a href='funcref.php'>Function Reference</a></li> <li><a href='refs.fileprocess.file.php'>File System Related Extensions</a></li> <li><a href='book.filesystem.php'>Filesystem</a></li> <li><a href='ref.filesystem.php'>Filesystem Functions</a></li> </ul> </div> </div> <div id="layout" class="clearfix"> <section id="layout-content"> <div class="page-tools"> <div class="change-language"> <form action="/manual/change.php" method="get" id="changelang" name="changelang"> <fieldset> <label for="changelang-langs">Change language:</label> <select onchange="document.changelang.submit()" name="page" id="changelang-langs"> <option value='en/function.filemtime.php' selected="selected">English</option> <option value='de/function.filemtime.php'>German</option> <option value='es/function.filemtime.php'>Spanish</option> <option value='fr/function.filemtime.php'>French</option> <option value='it/function.filemtime.php'>Italian</option> <option value='ja/function.filemtime.php'>Japanese</option> <option value='pt_BR/function.filemtime.php'>Brazilian Portuguese</option> <option value='ru/function.filemtime.php'>Russian</option> <option value='tr/function.filemtime.php'>Turkish</option> <option value='uk/function.filemtime.php'>Ukrainian</option> <option value='zh/function.filemtime.php'>Chinese (Simplified)</option> <option value='help-translate.php'>Other</option> </select> </fieldset> </form> </div> </div><div id="function.filemtime" class="refentry"> <div class="refnamediv"> <h1 class="refname">filemtime</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">filemtime</span> &mdash; <span class="dc-title">Gets file modification time</span></p> </div> <div class="refsect1 description" id="refsect1-function.filemtime-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="methodname"><strong>filemtime</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.value.php" class="type false">false</a></span></span></div> <p class="para rdfs-comment"> This function returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed. </p> </div> <div class="refsect1 parameters" id="refsect1-function.filemtime-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt><code class="parameter">filename</code></dt> <dd> <p class="para"> Path to the file. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.filemtime-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the time the file was last modified, or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure. The time is returned as a Unix timestamp, which is suitable for the <span class="function"><a href="function.date.php" class="function">date()</a></span> function. </p> </div> <div class="refsect1 errors" id="refsect1-function.filemtime-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Upon failure, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted. </p> </div> <div class="refsect1 examples" id="refsect1-function.filemtime-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2281"> <p><strong>Example #1 <span class="function"><strong>filemtime()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.<br /><br /></span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">'somefile.txt'</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000"> was last modified: " </span><span style="color: #007700">. </span><span style="color: #0000BB">date </span><span style="color: #007700">(</span><span style="color: #DD0000">"F d Y H:i:s."</span><span style="color: #007700">, </span><span style="color: #0000BB">filemtime</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.filemtime-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">Note that time resolution may differ from one file system to another.</p></p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">The results of this function are cached. See <span class="function"><a href="function.clearstatcache.php" class="function">clearstatcache()</a></span> for more details.</span></p></blockquote> <div class="tip"><strong class="tip">Tip</strong><p class="simpara">As of PHP 5.0.0, this function can also be used with <em>some</em> URL wrappers. Refer to <a href="wrappers.php" class="xref">Supported Protocols and Wrappers</a> to determine which wrappers support <span class="function"><a href="function.stat.php" class="function">stat()</a></span> family of functionality.</p></div> </div> <div class="refsect1 seealso" id="refsect1-function.filemtime-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li><span class="function"><a href="function.filectime.php" class="function" rel="rdfs-seeAlso">filectime()</a> - Gets inode change time of file</span></li> <li><span class="function"><a href="function.stat.php" class="function" rel="rdfs-seeAlso">stat()</a> - Gives information about a file</span></li> <li><span class="function"><a href="function.touch.php" class="function" rel="rdfs-seeAlso">touch()</a> - Sets access and modification time of file</span></li> <li><span class="function"><a href="function.getlastmod.php" class="function" rel="rdfs-seeAlso">getlastmod()</a> - Gets time of last page modification</span></li> </ul> </p> </div> </div> <div class="contribute"> <h3 class="title">Found A Problem?</h3> <div> </div> <div class="edit-bug"> <a href="https://github.com/php/doc-base/blob/master/README.md" title="This will take you to our contribution guidelines on GitHub" target="_blank" rel="noopener noreferrer">Learn How To Improve This Page</a> • <a href="https://github.com/php/doc-en/blob/master/reference/filesystem/functions/filemtime.xml">Submit a Pull Request</a> • <a href="https://github.com/php/doc-en/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2Ffunction.filemtime%0A%0A---">Report a Bug</a> </div> </div><section id="usernotes"> <div class="head"> <span class="action"><a href="/manual/add-note.php?sect=function.filemtime&amp;repo=en&amp;redirect=https://www.php.net/manual/en/function.filemtime.php">+<small>add a note</small></a></span> <h3 class="title">User Contributed Notes <span class="count">30 notes</span></h3> </div><div id="allnotes"> <div class="note" id="81194"> <div class="votes"> <div id="Vu81194"> <a href="/manual/vote-note.php?id=81194&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd81194"> <a href="/manual/vote-note.php?id=81194&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V81194" title="91% like this..."> 259 </div> </div> <a href="#81194" class="name"> <strong class="user"><em>geeks at geekman dot info</em></strong></a><a class="genanchor" href="#81194"> &para;</a><div class="date" title="2008-02-17 09:01"><strong>17 years ago</strong></div> <div class="text" id="Hcom81194"> <div class="phpcode"><code><span class="html">This is a very handy function for dealing with browser caching. For example, say you have a stylesheet and you want to make sure everyone has the most recent version. You could rename it every time you edit it, but that would be a pain in the ass. Instead, you can do this: <br /> <br /><span class="default">&lt;?php <br /></span><span class="keyword">echo </span><span class="string">'&lt;link rel="stylesheet" type="text/css" href="style.css?' </span><span class="keyword">. </span><span class="default">filemtime</span><span class="keyword">(</span><span class="string">'style.css'</span><span class="keyword">) . </span><span class="string">'" /&gt;'</span><span class="keyword">; <br /></span><span class="default">?&gt; <br /></span> <br />Sample output: <br /> <br />&lt;link rel="stylesheet" type="text/css" href="style.css?1203291283" /&gt; <br /> <br />By appending a GET value (the UNIX timestamp) to the stylesheet URL, you make the browser think the stylesheet is dynamic, so it'll reload the stylesheet every time the modification date changes.</span></code></div> </div> </div> <div class="note" id="32728"> <div class="votes"> <div id="Vu32728"> <a href="/manual/vote-note.php?id=32728&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd32728"> <a href="/manual/vote-note.php?id=32728&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V32728" title="82% like this..."> 68 </div> </div> <a href="#32728" class="name"> <strong class="user"><em>paranoid at dds dot nl</em></strong></a><a class="genanchor" href="#32728"> &para;</a><div class="date" title="2003-06-05 05:43"><strong>21 years ago</strong></div> <div class="text" id="Hcom32728"> <div class="phpcode"><code><span class="html">To get the last modification time of a directory, you can use this:<br /><br />&lt;pre&gt;<br />$getLastModDir = filemtime("/path/to/directory/.");<br />&lt;/pre&gt;<br /><br />Take note on the last dot which is needed to see the directory as a file and to actually get a last modification date of it.<br /><br />This comes in handy when you want just one 'last updated' message on the frontpage of your website and still taking all files of your website into account.<br /><br />Regards,<br />Frank Keijzers</span></code></div> </div> </div> <div class="note" id="50195"> <div class="votes"> <div id="Vu50195"> <a href="/manual/vote-note.php?id=50195&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd50195"> <a href="/manual/vote-note.php?id=50195&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V50195" title="88% like this..."> 7 </div> </div> <a href="#50195" class="name"> <strong class="user"><em>habazi at yahoo dot com</em></strong></a><a class="genanchor" href="#50195"> &para;</a><div class="date" title="2005-02-21 10:13"><strong>20 years ago</strong></div> <div class="text" id="Hcom50195"> <div class="phpcode"><code><span class="html">"this is not (necessarily) correct, the modification time of a directory will be the time of the last file *creation* in a directory (and not in it's sub directories)."<br /><br />This is not (necessarily) correct either. In *nix the timestamp can be independently set. For example the command "touch directory" updates the timestamp of a directory without file creation.<br /><br />Also file removal will update the timestamp of a directory.</span></code></div> </div> </div> <div class="note" id="73747"> <div class="votes"> <div id="Vu73747"> <a href="/manual/vote-note.php?id=73747&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd73747"> <a href="/manual/vote-note.php?id=73747&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V73747" title="83% like this..."> 4 </div> </div> <a href="#73747" class="name"> <strong class="user"><em>solarijj at gmail dot com</em></strong></a><a class="genanchor" href="#73747"> &para;</a><div class="date" title="2007-03-08 12:02"><strong>18 years ago</strong></div> <div class="text" id="Hcom73747"> <div class="phpcode"><code><span class="html">To get the modification date of some remote file, you can use the fine function by notepad at codewalker dot com (with improvements by dma05 at web dot de and madsen at lillesvin dot net).<br /><br />But you can achieve the same result more easily now with stream_get_meta_data (PHP&gt;4.3.0).<br /><br />However a problem may arise if some redirection occurs. In such a case, the server HTTP response contains no Last-Modified header, but there is a Location header indicating where to find the file. The function below takes care of any redirections, even multiple redirections, so that you reach the real file of which you want the last modification date.<br /><br />hih,<br />JJS.<br /><br /><span class="default">&lt;?php<br /><br /></span><span class="comment">// get remote file last modification date (returns unix timestamp)<br /></span><span class="keyword">function </span><span class="default">GetRemoteLastModified</span><span class="keyword">( </span><span class="default">$uri </span><span class="keyword">)<br />{<br /> </span><span class="comment">// default<br /> </span><span class="default">$unixtime </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">;<br /> <br /> </span><span class="default">$fp </span><span class="keyword">= </span><span class="default">fopen</span><span class="keyword">( </span><span class="default">$uri</span><span class="keyword">, </span><span class="string">"r" </span><span class="keyword">);<br /> if( !</span><span class="default">$fp </span><span class="keyword">) {return;}<br /> <br /> </span><span class="default">$MetaData </span><span class="keyword">= </span><span class="default">stream_get_meta_data</span><span class="keyword">( </span><span class="default">$fp </span><span class="keyword">);<br /> <br /> foreach( </span><span class="default">$MetaData</span><span class="keyword">[</span><span class="string">'wrapper_data'</span><span class="keyword">] as </span><span class="default">$response </span><span class="keyword">)<br /> {<br /> </span><span class="comment">// case: redirection<br /> </span><span class="keyword">if( </span><span class="default">substr</span><span class="keyword">( </span><span class="default">strtolower</span><span class="keyword">(</span><span class="default">$response</span><span class="keyword">), </span><span class="default">0</span><span class="keyword">, </span><span class="default">10 </span><span class="keyword">) == </span><span class="string">'location: ' </span><span class="keyword">)<br /> {<br /> </span><span class="default">$newUri </span><span class="keyword">= </span><span class="default">substr</span><span class="keyword">( </span><span class="default">$response</span><span class="keyword">, </span><span class="default">10 </span><span class="keyword">);<br /> </span><span class="default">fclose</span><span class="keyword">( </span><span class="default">$fp </span><span class="keyword">);<br /> return </span><span class="default">GetRemoteLastModified</span><span class="keyword">( </span><span class="default">$newUri </span><span class="keyword">);<br /> }<br /> </span><span class="comment">// case: last-modified<br /> </span><span class="keyword">elseif( </span><span class="default">substr</span><span class="keyword">( </span><span class="default">strtolower</span><span class="keyword">(</span><span class="default">$response</span><span class="keyword">), </span><span class="default">0</span><span class="keyword">, </span><span class="default">15 </span><span class="keyword">) == </span><span class="string">'last-modified: ' </span><span class="keyword">)<br /> {<br /> </span><span class="default">$unixtime </span><span class="keyword">= </span><span class="default">strtotime</span><span class="keyword">( </span><span class="default">substr</span><span class="keyword">(</span><span class="default">$response</span><span class="keyword">, </span><span class="default">15</span><span class="keyword">) );<br /> break;<br /> }<br /> }<br /> </span><span class="default">fclose</span><span class="keyword">( </span><span class="default">$fp </span><span class="keyword">);<br /> return </span><span class="default">$unixtime</span><span class="keyword">;<br />}<br /></span><span class="default">?&gt;</span></span></code></div> </div> </div> <div class="note" id="126358"> <div class="votes"> <div id="Vu126358"> <a href="/manual/vote-note.php?id=126358&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd126358"> <a href="/manual/vote-note.php?id=126358&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V126358" title="75% like this..."> 2 </div> </div> <a href="#126358" class="name"> <strong class="user"><em>MarkAgius at markagius dot co dot uk</em></strong></a><a class="genanchor" href="#126358"> &para;</a><div class="date" title="2021-08-22 12:33"><strong>3 years ago</strong></div> <div class="text" id="Hcom126358"> <div class="phpcode"><code><span class="html">filemtime(..) only works with files on your server.<br />$T=filesize("index.php"); // Works.<br />$T=filesize("/public_html/dir/index.php"); // Works.<br /><br />But the following will not work.<br />$T=filesize("<a href="https://mydomain.com/dir/index.php" rel="nofollow" target="_blank">https://mydomain.com/dir/index.php</a>"); // Will not work. Same domain but using web address.<br />$T=filesize("<a href="https://otherdomain.com/dir/index.php" rel="nofollow" target="_blank">https://otherdomain.com/dir/index.php</a>"); // Other domain, will not work.<br /><br />To get file date for other sites try:<br />(Note: Time zone may be in G.M.T. and not your local timezone)<br /><br />$T = getFileDate("<a href="https://mydomain.com/dir/index.php" rel="nofollow" target="_blank">https://mydomain.com/dir/index.php</a>");<br />$T = getFileDate("<a href="https://otherdomain.com/dir/index.php" rel="nofollow" target="_blank">https://otherdomain.com/dir/index.php</a>");<br /><br />function getFileDate($filePath){<br /> // Same as filemtime(..) but also works with remote files.<br /> // EG. = print date("j/m/Y, g:i:sa (e)",getFileDate("<a href="https://abc.com/file.jpg" rel="nofollow" target="_blank">https://abc.com/file.jpg</a>"));<br /> // File not found then will return -307756800 ( 1/04/1960)<br /> $ret = -(3562 *24*60*60); // 1/04/1960 3562= seconds to 1/01/1970<br /><br /> $ch = curl_init($filePath);<br /> curl_setopt($ch, CURLOPT_NOBODY, true);<br /> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br /> // Try and get the file modification date.<br /> curl_setopt($ch, CURLOPT_FILETIME, true);<br /> $result = curl_exec($ch);<br /> if($result===false){<br /> die(curl_error($ch)); <br /> return $ret;<br /> }<br /><br /> $ret = curl_getinfo($ch, CURLINFO_FILETIME);<br /> curl_close($ch);<br /><br /> return $ret;<br />}</span></code></div> </div> </div> <div class="note" id="98284"> <div class="votes"> <div id="Vu98284"> <a href="/manual/vote-note.php?id=98284&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd98284"> <a href="/manual/vote-note.php?id=98284&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V98284" title="68% like this..."> 13 </div> </div> <a href="#98284" class="name"> <strong class="user"><em>erelsgl at gmail dot com</em></strong></a><a class="genanchor" href="#98284"> &para;</a><div class="date" title="2010-06-07 01:08"><strong>14 years ago</strong></div> <div class="text" id="Hcom98284"> <div class="phpcode"><code><span class="html">Cheaper and dirtier way to code a cache:<br /><br /><span class="default">&lt;?php<br />$cache_file </span><span class="keyword">= </span><span class="string">'URI to cache file'</span><span class="keyword">;<br /></span><span class="default">$cache_life </span><span class="keyword">= </span><span class="string">'120'</span><span class="keyword">; </span><span class="comment">//caching time, in seconds<br /><br /></span><span class="default">$filemtime </span><span class="keyword">= @</span><span class="default">filemtime</span><span class="keyword">(</span><span class="default">$cache_file</span><span class="keyword">); </span><span class="comment">// returns FALSE if file does not exist<br /></span><span class="keyword">if (!</span><span class="default">$filemtime </span><span class="keyword">or (</span><span class="default">time</span><span class="keyword">() - </span><span class="default">$filemtime </span><span class="keyword">&gt;= </span><span class="default">$cache_life</span><span class="keyword">)){<br /> </span><span class="default">ob_start</span><span class="keyword">();<br /> </span><span class="default">resource_consuming_function</span><span class="keyword">();<br /> </span><span class="default">file_put_contents</span><span class="keyword">(</span><span class="default">$cache_file</span><span class="keyword">,</span><span class="default">ob_get_flush</span><span class="keyword">());<br />}else{<br /> </span><span class="default">readfile</span><span class="keyword">(</span><span class="default">$cache_file</span><span class="keyword">);<br />}<br /></span><span class="default">?&gt;</span></span></code></div> </div> </div> <div class="note" id="100692"> <div class="votes"> <div id="Vu100692"> <a href="/manual/vote-note.php?id=100692&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd100692"> <a href="/manual/vote-note.php?id=100692&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V100692" title="66% like this..."> 12 </div> </div> <a href="#100692" class="name"> <strong class="user"><em>myselfasunder at gmail dot XYZ dot com</em></strong></a><a class="genanchor" href="#100692"> &para;</a><div class="date" title="2010-10-31 02:02"><strong>14 years ago</strong></div> <div class="text" id="Hcom100692"> <div class="phpcode"><code><span class="html">There's a deeply-seated problem with filemtime() under Windows due to the fact that it calls Windows' stat() function, which implements DST (according to this bug: <a href="http://bugs.php.net/bug.php?id=40568" rel="nofollow" target="_blank">http://bugs.php.net/bug.php?id=40568</a>). The detection of DST on the time of the file is confused by whether the CURRENT time of the current system is currently under DST. <br /> <br />This is a fix for the mother of all annoying bugs: <br /> <br /><span class="default">&lt;?php <br /></span><span class="keyword">function </span><span class="default">GetCorrectMTime</span><span class="keyword">(</span><span class="default">$filePath</span><span class="keyword">) <br />{ <br /> <br /> </span><span class="default">$time </span><span class="keyword">= </span><span class="default">filemtime</span><span class="keyword">(</span><span class="default">$filePath</span><span class="keyword">); <br /> <br /> </span><span class="default">$isDST </span><span class="keyword">= (</span><span class="default">date</span><span class="keyword">(</span><span class="string">'I'</span><span class="keyword">, </span><span class="default">$time</span><span class="keyword">) == </span><span class="default">1</span><span class="keyword">); <br /> </span><span class="default">$systemDST </span><span class="keyword">= (</span><span class="default">date</span><span class="keyword">(</span><span class="string">'I'</span><span class="keyword">) == </span><span class="default">1</span><span class="keyword">); <br /> <br /> </span><span class="default">$adjustment </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; <br /> <br /> if(</span><span class="default">$isDST </span><span class="keyword">== </span><span class="default">false </span><span class="keyword">&amp;&amp; </span><span class="default">$systemDST </span><span class="keyword">== </span><span class="default">true</span><span class="keyword">) <br /> </span><span class="default">$adjustment </span><span class="keyword">= </span><span class="default">3600</span><span class="keyword">; <br /> <br /> else if(</span><span class="default">$isDST </span><span class="keyword">== </span><span class="default">true </span><span class="keyword">&amp;&amp; </span><span class="default">$systemDST </span><span class="keyword">== </span><span class="default">false</span><span class="keyword">) <br /> </span><span class="default">$adjustment </span><span class="keyword">= -</span><span class="default">3600</span><span class="keyword">; <br /> <br /> else <br /> </span><span class="default">$adjustment </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">; <br /> <br /> return (</span><span class="default">$time </span><span class="keyword">+ </span><span class="default">$adjustment</span><span class="keyword">); <br />} <br /></span><span class="default">?&gt; <br /></span> <br />Dustin Oprea</span></code></div> </div> </div> <div class="note" id="48035"> <div class="votes"> <div id="Vu48035"> <a href="/manual/vote-note.php?id=48035&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd48035"> <a href="/manual/vote-note.php?id=48035&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V48035" title="66% like this..."> 4 </div> </div> <a href="#48035" class="name"><strong class="user"><em>Anonymous</em></strong></a><a class="genanchor" href="#48035"> &para;</a><div class="date" title="2004-12-09 08:30"><strong>20 years ago</strong></div> <div class="text" id="Hcom48035"> <div class="phpcode"><code><span class="html">A comment below states<br /><br /> "When using this function to get the modified date of a directory, <br /> it returns the date of the file in that directory that was last modified."<br /><br />this is not (necessarily) correct, the modification time of a directory will be the time of the last file *creation* in a directory (and not in it's sub directories).</span></code></div> </div> </div> <div class="note" id="42065"> <div class="votes"> <div id="Vu42065"> <a href="/manual/vote-note.php?id=42065&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd42065"> <a href="/manual/vote-note.php?id=42065&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V42065" title="66% like this..."> 3 </div> </div> <a href="#42065" class="name"> <strong class="user"><em>adam at roomvoter dot com</em></strong></a><a class="genanchor" href="#42065"> &para;</a><div class="date" title="2004-05-01 01:42"><strong>20 years ago</strong></div> <div class="text" id="Hcom42065"> <div class="phpcode"><code><span class="html">The snippet of code earlier that allows you to delete all files older than 2 weeks uses the function (filemtime) - which checks the original create date of the file (filesystem independent). You MAY want to use filectime() - that looks at when the file was last changed on YOUR file system.<br /><br /> if (is_dir("$path") ) <br /> { <br /> $handle=opendir($path); <br /> while (false!==($file = readdir($handle))) { <br /> if ($file != "." &amp;&amp; $file != "..") { <br /> $Diff = (time() - filectime("$path/$file"))/60/60/24;<br /> if ($Diff &gt; 14) unlink("$path/$file");<br /><br /> } <br /> }<br /> closedir($handle); <br /> }</span></code></div> </div> </div> <div class="note" id="95986"> <div class="votes"> <div id="Vu95986"> <a href="/manual/vote-note.php?id=95986&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd95986"> <a href="/manual/vote-note.php?id=95986&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V95986" title="63% like this..."> 6 </div> </div> <a href="#95986" class="name"> <strong class="user"><em>_michael</em></strong></a><a class="genanchor" href="#95986"> &para;</a><div class="date" title="2010-02-01 11:09"><strong>15 years ago</strong></div> <div class="text" id="Hcom95986"> <div class="phpcode"><code><span class="html">While testing on Windows, I noticed that the precision of filemtime is just 1 second.<br /><br />So if you use clearstatcache() and filemtime() to check if a file has been modified, it might fail to detect the change. The modifications just have to happen within less than a second.<br /><br />(I ran into this with Apache on Windows XP.)</span></code></div> </div> </div> <div class="note" id="50839"> <div class="votes"> <div id="Vu50839"> <a href="/manual/vote-note.php?id=50839&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd50839"> <a href="/manual/vote-note.php?id=50839&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V50839" title="62% like this..."> 7 </div> </div> <a href="#50839" class="name"> <strong class="user"><em>notepad at codewalkers dot com</em></strong></a><a class="genanchor" href="#50839"> &para;</a><div class="date" title="2005-03-11 01:53"><strong>20 years ago</strong></div> <div class="text" id="Hcom50839"> <div class="phpcode"><code><span class="html">i needed the ability to grab the mod time of an image on a remote site. the following is the solution with the help of Joe Ferris.<br /><br /><span class="default">&lt;?php<br /><br /></span><span class="keyword">function </span><span class="default">filemtime_remote</span><span class="keyword">(</span><span class="default">$uri</span><span class="keyword">)<br />{<br /> </span><span class="default">$uri </span><span class="keyword">= </span><span class="default">parse_url</span><span class="keyword">(</span><span class="default">$uri</span><span class="keyword">);<br /> </span><span class="default">$handle </span><span class="keyword">= @</span><span class="default">fsockopen</span><span class="keyword">(</span><span class="default">$uri</span><span class="keyword">[</span><span class="string">'host'</span><span class="keyword">],</span><span class="default">80</span><span class="keyword">);<br /> if(!</span><span class="default">$handle</span><span class="keyword">)<br /> return </span><span class="default">0</span><span class="keyword">;<br /><br /> </span><span class="default">fputs</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">,</span><span class="string">"GET </span><span class="default">$uri</span><span class="keyword">[</span><span class="default">path</span><span class="keyword">]</span><span class="string"> HTTP/1.1\r\nHost: </span><span class="default">$uri</span><span class="keyword">[</span><span class="default">host</span><span class="keyword">]</span><span class="string">\r\n\r\n"</span><span class="keyword">);<br /> </span><span class="default">$result </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">;<br /> while(!</span><span class="default">feof</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">))<br /> {<br /> </span><span class="default">$line </span><span class="keyword">= </span><span class="default">fgets</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">,</span><span class="default">1024</span><span class="keyword">);<br /> if(!</span><span class="default">trim</span><span class="keyword">(</span><span class="default">$line</span><span class="keyword">))<br /> break;<br /><br /> </span><span class="default">$col </span><span class="keyword">= </span><span class="default">strpos</span><span class="keyword">(</span><span class="default">$line</span><span class="keyword">,</span><span class="string">':'</span><span class="keyword">);<br /> if(</span><span class="default">$col </span><span class="keyword">!== </span><span class="default">false</span><span class="keyword">)<br /> {<br /> </span><span class="default">$header </span><span class="keyword">= </span><span class="default">trim</span><span class="keyword">(</span><span class="default">substr</span><span class="keyword">(</span><span class="default">$line</span><span class="keyword">,</span><span class="default">0</span><span class="keyword">,</span><span class="default">$col</span><span class="keyword">));<br /> </span><span class="default">$value </span><span class="keyword">= </span><span class="default">trim</span><span class="keyword">(</span><span class="default">substr</span><span class="keyword">(</span><span class="default">$line</span><span class="keyword">,</span><span class="default">$col</span><span class="keyword">+</span><span class="default">1</span><span class="keyword">));<br /> if(</span><span class="default">strtolower</span><span class="keyword">(</span><span class="default">$header</span><span class="keyword">) == </span><span class="string">'last-modified'</span><span class="keyword">)<br /> {<br /> </span><span class="default">$result </span><span class="keyword">= </span><span class="default">strtotime</span><span class="keyword">(</span><span class="default">$value</span><span class="keyword">);<br /> break;<br /> }<br /> }<br /> }<br /> </span><span class="default">fclose</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">);<br /> return </span><span class="default">$result</span><span class="keyword">;<br />}<br /></span><span class="comment">// echo filemtime_remote('<a href="http://www.somesite.com/someimage.jpg" rel="nofollow" target="_blank">http://www.somesite.com/someimage.jpg</a>');<br /><br /></span><span class="default">?&gt;</span></span></code></div> </div> </div> <div class="note" id="35779"> <div class="votes"> <div id="Vu35779"> <a href="/manual/vote-note.php?id=35779&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd35779"> <a href="/manual/vote-note.php?id=35779&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V35779" title="62% like this..."> 4 </div> </div> <a href="#35779" class="name"> <strong class="user"><em>wookie at at no-way dot org</em></strong></a><a class="genanchor" href="#35779"> &para;</a><div class="date" title="2003-09-14 02:17"><strong>21 years ago</strong></div> <div class="text" id="Hcom35779"> <div class="phpcode"><code><span class="html">Another little handy tool; to get the most recent modified time from files in a directory. It even does recursive directories if you set the $doRecursive param to true. Based on a file/directory list function I saw somewhere on this site. ;)<br /><br />function mostRecentModifiedFileTime($dirName,$doRecursive) {<br /> $d = dir($dirName);<br /> $lastModified = 0;<br /> while($entry = $d-&gt;read()) {<br /> if ($entry != "." &amp;&amp; $entry != "..") {<br /> if (!is_dir($dirName."/".$entry)) {<br /> $currentModified = filemtime($dirName."/".$entry);<br /> } else if ($doRecursive &amp;&amp; is_dir($dirName."/".$entry)) {<br /> $currentModified = mostRecentModifiedFileTime($dirName."/".$entry,true);<br /> }<br /> if ($currentModified &gt; $lastModified){<br /> $lastModified = $currentModified;<br /> }<br /> }<br /> }<br /> $d-&gt;close();<br /> return $lastModified;<br />}</span></code></div> </div> </div> <div class="note" id="102498"> <div class="votes"> <div id="Vu102498"> <a href="/manual/vote-note.php?id=102498&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd102498"> <a href="/manual/vote-note.php?id=102498&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V102498" title="60% like this..."> 3 </div> </div> <a href="#102498" class="name"> <strong class="user"><em>Will Davies Vasconcelos</em></strong></a><a class="genanchor" href="#102498"> &para;</a><div class="date" title="2011-02-17 10:22"><strong>14 years ago</strong></div> <div class="text" id="Hcom102498"> <div class="phpcode"><code><span class="html">Here is a handy script to create a csv file with file names and the date when files in a given folder were inserted:<br /><br /><span class="default">&lt;?php<br /> header</span><span class="keyword">(</span><span class="string">"Pragma: public"</span><span class="keyword">);<br /> </span><span class="default">header</span><span class="keyword">(</span><span class="string">"Cache-Control: private"</span><span class="keyword">);<br /> </span><span class="default">header</span><span class="keyword">(</span><span class="string">"Content-Type: text/csv"</span><span class="keyword">);<br /> </span><span class="default">header</span><span class="keyword">(</span><span class="string">"Content-Disposition: attachment; filename=age-of-files.csv"</span><span class="keyword">);<br /><br /> </span><span class="default">$result </span><span class="keyword">= array();<br /> </span><span class="default">$handle </span><span class="keyword">= </span><span class="default">opendir</span><span class="keyword">(</span><span class="string">"."</span><span class="keyword">);<br /> while (</span><span class="default">$datei </span><span class="keyword">= </span><span class="default">readdir</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">)) <br /> {<br /> if ((</span><span class="default">$datei </span><span class="keyword">!= </span><span class="string">'.'</span><span class="keyword">) &amp;&amp; (</span><span class="default">$datei </span><span class="keyword">!= </span><span class="string">'..'</span><span class="keyword">)) <br /> {<br /> </span><span class="default">$file </span><span class="keyword">= </span><span class="string">"./"</span><span class="keyword">.</span><span class="default">$datei</span><span class="keyword">;<br /> if (!</span><span class="default">is_dir</span><span class="keyword">(</span><span class="default">$file</span><span class="keyword">))<br /> </span><span class="default">$result</span><span class="keyword">[] = </span><span class="default">$file</span><span class="keyword">;<br /> }<br /> }<br /> </span><span class="default">closedir</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">);<br /> foreach(</span><span class="default">$result </span><span class="keyword">as </span><span class="default">$r</span><span class="keyword">)<br /> if (</span><span class="default">file_exists</span><span class="keyword">(</span><span class="default">$r</span><span class="keyword">))<br /> echo </span><span class="default">substr</span><span class="keyword">(</span><span class="default">$r</span><span class="keyword">,</span><span class="default">2</span><span class="keyword">).</span><span class="string">","</span><span class="keyword">.</span><span class="default">date </span><span class="keyword">(</span><span class="string">"m/d/Y"</span><span class="keyword">, </span><span class="default">filemtime</span><span class="keyword">(</span><span class="default">$r</span><span class="keyword">)).</span><span class="string">"\r\n"</span><span class="keyword">;<br /></span><span class="default">?&gt;</span></span></code></div> </div> </div> <div class="note" id="68868"> <div class="votes"> <div id="Vu68868"> <a href="/manual/vote-note.php?id=68868&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd68868"> <a href="/manual/vote-note.php?id=68868&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V68868" title="60% like this..."> 2 </div> </div> <a href="#68868" class="name"> <strong class="user"><em>Benan Tumkaya (benantumkaya at yahoo)</em></strong></a><a class="genanchor" href="#68868"> &para;</a><div class="date" title="2006-08-14 02:11"><strong>18 years ago</strong></div> <div class="text" id="Hcom68868"> <div class="phpcode"><code><span class="html">Here is a small but handy script that you can use to find which files in your server are modified after a date/time that you specify. This script will go through all folders in the specified directory recursively and echo the modified files with the last modified date/time...<br /><br />//Starts Here<br />//Put here the directory you want to search for. Put / if you want to search your entire domain<br />$dir='/';<br /><br />//Put the date you want to compare with in the format of: YYYY-mm-dd hh:mm:ss<br />$comparedatestr="2006-08-12 00:00:00";<br />$comparedate=strtotime($comparedatestr);<br /><br />//I run the function here to start the search. <br />directory_tree($dir,$comparedate);<br /><br />//This is the function which is doing the search...<br />function directory_tree($address,$comparedate){ <br /><br /> @$dir = opendir($address); <br /><br /> if(!$dir){ return 0; } <br /> while($entry = readdir($dir)){ <br /> if(is_dir("$address/$entry") &amp;&amp; ($entry != ".." &amp;&amp; $entry != ".")){ <br /> directory_tree("$address/$entry",$comparedate);<br /> } <br /> else {<br /><br /> if($entry != ".." &amp;&amp; $entry != ".") {<br /> <br /> $fulldir=$address.'/'.$entry;<br /> $last_modified = filemtime($fulldir);<br /> $last_modified_str= date("Y-m-d h:i:s", $last_modified);<br /><br /> if($comparedate &lt; $last_modified) {<br /> echo $fulldir.'=&gt;'.$last_modified_str;<br /> echo "&lt;BR&gt;";<br /> }<br /><br /> }<br /><br /> }<br /><br /> } <br /><br />}</span></code></div> </div> </div> <div class="note" id="111171"> <div class="votes"> <div id="Vu111171"> <a href="/manual/vote-note.php?id=111171&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd111171"> <a href="/manual/vote-note.php?id=111171&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V111171" title="58% like this..."> 2 </div> </div> <a href="#111171" class="name"> <strong class="user"><em>nieprzeklinaj at gmail dot com</em></strong></a><a class="genanchor" href="#111171"> &para;</a><div class="date" title="2013-01-22 12:56"><strong>12 years ago</strong></div> <div class="text" id="Hcom111171"> <div class="phpcode"><code><span class="html">Here's a handy little function for smart cache overriding :)<br /><span class="default">&lt;?php<br /></span><span class="keyword">function </span><span class="default">img</span><span class="keyword">(</span><span class="default">$src</span><span class="keyword">, </span><span class="default">$alt</span><span class="keyword">=</span><span class="string">''</span><span class="keyword">, </span><span class="default">$attr</span><span class="keyword">=</span><span class="string">''</span><span class="keyword">)<br />{<br /> if(</span><span class="default">file_exists</span><span class="keyword">(</span><span class="default">$src</span><span class="keyword">))<br /> {<br /> </span><span class="default">$lmod </span><span class="keyword">= </span><span class="default">filemtime</span><span class="keyword">(</span><span class="default">$src</span><span class="keyword">);<br /> echo </span><span class="string">'&lt;img src="'</span><span class="keyword">.</span><span class="default">$src</span><span class="keyword">.</span><span class="string">'?lmod='</span><span class="keyword">.</span><span class="default">$lmod</span><span class="keyword">.</span><span class="string">'" alt="'</span><span class="keyword">.</span><span class="default">$alt</span><span class="keyword">.</span><span class="string">'" '</span><span class="keyword">.</span><span class="default">$attr</span><span class="keyword">.</span><span class="string">' /&gt;'</span><span class="keyword">;<br /> }<br />}<br /></span><span class="default">?&gt;</span></span></code></div> </div> </div> <div class="note" id="1050"> <div class="votes"> <div id="Vu1050"> <a href="/manual/vote-note.php?id=1050&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd1050"> <a href="/manual/vote-note.php?id=1050&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V1050" title="60% like this..."> 2 </div> </div> <a href="#1050" class="name"> <strong class="user"><em>jay at fudge dot org</em></strong></a><a class="genanchor" href="#1050"> &para;</a><div class="date" title="1999-06-29 01:55"><strong>25 years ago</strong></div> <div class="text" id="Hcom1050"> <div class="phpcode"><code><span class="html">If you want this functionality for the parent web page you should use getlastmod() <br />i.e. <br /><span class="default">&lt;?php </span><span class="keyword">echo </span><span class="string">"Last modified: "</span><span class="keyword">.</span><span class="default">date</span><span class="keyword">( </span><span class="string">"F d Y H:i:s."</span><span class="keyword">, </span><span class="default">getlastmod</span><span class="keyword">() ); </span><span class="default">?&gt; <br /></span>within the included page... i.e. as a commont footer include for all pages</span></code></div> </div> </div> <div class="note" id="93029"> <div class="votes"> <div id="Vu93029"> <a href="/manual/vote-note.php?id=93029&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd93029"> <a href="/manual/vote-note.php?id=93029&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V93029" title="58% like this..."> 2 </div> </div> <a href="#93029" class="name"> <strong class="user"><em>Anonymous</em></strong></a><a class="genanchor" href="#93029"> &para;</a><div class="date" title="2009-08-19 03:52"><strong>15 years ago</strong></div> <div class="text" id="Hcom93029"> <div class="phpcode"><code><span class="html">Please note that many of the functions below that people have provided to get files modified after a certain time in a directory will NOT get all files on a Windows operating system.<br /><br />If you copy and paste any file inside the folder or into the folder from another folder (such as images that may be used but aren't going to be modified right away), the modified time is not updated on these copied files, only the creation time.<br /><br />You need to use filectime with filemtime to assure you get copied files that aren't modified but are obviously new.</span></code></div> </div> </div> <div class="note" id="117806"> <div class="votes"> <div id="Vu117806"> <a href="/manual/vote-note.php?id=117806&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd117806"> <a href="/manual/vote-note.php?id=117806&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V117806" title="57% like this..."> 2 </div> </div> <a href="#117806" class="name"> <strong class="user"><em>Don</em></strong></a><a class="genanchor" href="#117806"> &para;</a><div class="date" title="2015-08-12 06:13"><strong>9 years ago</strong></div> <div class="text" id="Hcom117806"> <div class="phpcode"><code><span class="html">To find the oldest file in a directory : <br />$directory= "C:\\";<br /><br />$smallest_time=INF;<br /><br />$oldest_file='';<br /><br />if ($handle = opendir($directory)) {<br /><br /> while (false !== ($file = readdir($handle))) {<br /><br /> $time=filemtime($directory.'/'.$file);<br /><br /> if (is_file($directory.'/'.$file)) {<br /><br /> if ($time &lt; $smallest_time) {<br /> $oldest_file = $file;<br /> $smallest_time = $time;<br /> }<br /> }<br /> }<br /> closedir($handle);<br />}<br /><br />echo $oldest_file;</span></code></div> </div> </div> <div class="note" id="68814"> <div class="votes"> <div id="Vu68814"> <a href="/manual/vote-note.php?id=68814&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd68814"> <a href="/manual/vote-note.php?id=68814&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V68814" title="57% like this..."> 3 </div> </div> <a href="#68814" class="name"> <strong class="user"><em>csnyder at chxo dot com</em></strong></a><a class="genanchor" href="#68814"> &para;</a><div class="date" title="2006-08-11 08:59"><strong>18 years ago</strong></div> <div class="text" id="Hcom68814"> <div class="phpcode"><code><span class="html">If PHP's integer type is only 32 bits on your system, filemtime() will fail on files over 2GB with the warning "stat failed". All stat()-related commands will exhibit the same behavior.<br /><br />As a workaround, you can call the system's stat command to get the modification time of a file:<br /><br />On FreeBSD:<br />$mtime = exec ('stat -f %m '. escapeshellarg ($path));<br /><br />On Linux:<br />$mtime = exec ('stat -c %Y '. escapeshellarg ($path));<br /><br />Thanks to "mpb dot mail at gmail dot com" for his/her similar comment on stat().</span></code></div> </div> </div> <div class="note" id="91665"> <div class="votes"> <div id="Vu91665"> <a href="/manual/vote-note.php?id=91665&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd91665"> <a href="/manual/vote-note.php?id=91665&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V91665" title="55% like this..."> 1 </div> </div> <a href="#91665" class="name"> <strong class="user"><em>info at daniel-marschall dot de</em></strong></a><a class="genanchor" href="#91665"> &para;</a><div class="date" title="2009-06-22 03:59"><strong>15 years ago</strong></div> <div class="text" id="Hcom91665"> <div class="phpcode"><code><span class="html">It could be useful to determinate the timestamp of the newest file in a directory. (e.g. if you want to find out when the last change was made to your project).<br /><br />Following function will help you:<br /><br /><span class="default">&lt;?php<br /><br /></span><span class="keyword">function </span><span class="default">getAllFiles</span><span class="keyword">(</span><span class="default">$directory</span><span class="keyword">, </span><span class="default">$recursive </span><span class="keyword">= </span><span class="default">true</span><span class="keyword">) {<br /> </span><span class="default">$result </span><span class="keyword">= array();<br /> </span><span class="default">$handle </span><span class="keyword">= </span><span class="default">opendir</span><span class="keyword">(</span><span class="default">$directory</span><span class="keyword">);<br /> while (</span><span class="default">$datei </span><span class="keyword">= </span><span class="default">readdir</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">)) <br /> {<br /> if ((</span><span class="default">$datei </span><span class="keyword">!= </span><span class="string">'.'</span><span class="keyword">) &amp;&amp; (</span><span class="default">$datei </span><span class="keyword">!= </span><span class="string">'..'</span><span class="keyword">)) <br /> {<br /> </span><span class="default">$file </span><span class="keyword">= </span><span class="default">$directory</span><span class="keyword">.</span><span class="default">$datei</span><span class="keyword">;<br /> if (</span><span class="default">is_dir</span><span class="keyword">(</span><span class="default">$file</span><span class="keyword">)) {<br /> if (</span><span class="default">$recursive</span><span class="keyword">) {<br /> </span><span class="default">$result </span><span class="keyword">= </span><span class="default">array_merge</span><span class="keyword">(</span><span class="default">$result</span><span class="keyword">, </span><span class="default">getAllFiles</span><span class="keyword">(</span><span class="default">$file</span><span class="keyword">.</span><span class="string">'/'</span><span class="keyword">));<br /> }<br /> } else {<br /> </span><span class="default">$result</span><span class="keyword">[] = </span><span class="default">$file</span><span class="keyword">;<br /> } <br /> }<br /> }<br /> </span><span class="default">closedir</span><span class="keyword">(</span><span class="default">$handle</span><span class="keyword">);<br /> return </span><span class="default">$result</span><span class="keyword">;<br />}<br /><br />function </span><span class="default">getHighestFileTimestamp</span><span class="keyword">(</span><span class="default">$directory</span><span class="keyword">, </span><span class="default">$recursive </span><span class="keyword">= </span><span class="default">true</span><span class="keyword">) {<br /> </span><span class="default">$allFiles </span><span class="keyword">= </span><span class="default">getAllFiles</span><span class="keyword">(</span><span class="default">$directory</span><span class="keyword">, </span><span class="default">$recursive</span><span class="keyword">);<br /> </span><span class="default">$highestKnown </span><span class="keyword">= </span><span class="default">0</span><span class="keyword">;<br /> foreach (</span><span class="default">$allFiles </span><span class="keyword">as </span><span class="default">$val</span><span class="keyword">) {<br /> </span><span class="default">$currentValue </span><span class="keyword">= </span><span class="default">filemtime</span><span class="keyword">(</span><span class="default">$val</span><span class="keyword">);<br /> if (</span><span class="default">$currentValue </span><span class="keyword">&gt; </span><span class="default">$highestKnown</span><span class="keyword">) </span><span class="default">$highestKnown </span><span class="keyword">= </span><span class="default">$currentValue</span><span class="keyword">;<br /> }<br /> return </span><span class="default">$highestKnown</span><span class="keyword">;<br />}<br /><br /></span><span class="comment">// Use example<br /><br /></span><span class="keyword">echo </span><span class="string">'The newest file has the time stamp:&lt;br&gt;'</span><span class="keyword">;<br />echo </span><span class="default">date</span><span class="keyword">(</span><span class="string">'Y-m-d H:i:s'</span><span class="keyword">, </span><span class="default">getHighestFileTimestamp</span><span class="keyword">(</span><span class="string">'../'</span><span class="keyword">));<br /><br /></span><span class="default">?&gt;</span></span></code></div> </div> </div> <div class="note" id="52191"> <div class="votes"> <div id="Vu52191"> <a href="/manual/vote-note.php?id=52191&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd52191"> <a href="/manual/vote-note.php?id=52191&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V52191" title="55% like this..."> 1 </div> </div> <a href="#52191" class="name"> <strong class="user"><em>dma05 at web dot de</em></strong></a><a class="genanchor" href="#52191"> &para;</a><div class="date" title="2005-04-23 02:25"><strong>19 years ago</strong></div> <div class="text" id="Hcom52191"> <div class="phpcode"><code><span class="html">concerning "notepad at codewalkers dot com"'s code:<br /><br />this code is pretty neat, but i just wanted to note that using the "HEAD"-method instead of the "GET"-method in the http-request might be preferrable, since then not the whole resource is being downloaded...<br /><br />http/1.1 definiton snippet:<br />Section "9.4 HEAD"<br /><br /> The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification. [...]<br />-- snippet end ---<br /><br />the code would then be...:<br /><br />-- snippet ---<br />fputs($handle,"HEAD $uri[path] HTTP/1.1\r\nHost: $uri[host]\r\n\r\n");<br />-- snippet end ---<br /><br />regards, Magnus</span></code></div> </div> </div> <div class="note" id="93266"> <div class="votes"> <div id="Vu93266"> <a href="/manual/vote-note.php?id=93266&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd93266"> <a href="/manual/vote-note.php?id=93266&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V93266" title="54% like this..."> 1 </div> </div> <a href="#93266" class="name"> <strong class="user"><em>contacto at hardcode dot com dot ar</em></strong></a><a class="genanchor" href="#93266"> &para;</a><div class="date" title="2009-08-31 08:03"><strong>15 years ago</strong></div> <div class="text" id="Hcom93266"> <div class="phpcode"><code><span class="html">when working with swf files (flash animations), there is a nice way to avoid the browser cache. i used to do this by hand, then i used a random number, but with large animations while working online, it gets boring because the server always downloads the whole animation, even if there was no change. <br />but... <br /> <br />this will do the trick <br /> <br />base.swf?rand=<span class="default">&lt;?php </span><span class="keyword">echo </span><span class="default">filemtime</span><span class="keyword">(</span><span class="string">"base.swf"</span><span class="keyword">) </span><span class="default">?&gt; <br /></span> <br />&lt;embed src="base.swf?rand=<span class="default">&lt;?php </span><span class="keyword">echo </span><span class="default">filemtime</span><span class="keyword">(</span><span class="string">"base.swf"</span><span class="keyword">) </span><span class="default">?&gt;</span>" quality="high" pluginspage="<a href="http://www.macromedia.com/go/getflashplayer" rel="nofollow" target="_blank">http://www.macromedia.com/go/getflashplayer</a>" type="application/x-shockwave-flash" width="1680" height="1050"&gt;</span></code></div> </div> </div> <div class="note" id="113262"> <div class="votes"> <div id="Vu113262"> <a href="/manual/vote-note.php?id=113262&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd113262"> <a href="/manual/vote-note.php?id=113262&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V113262" title="53% like this..."> 1 </div> </div> <a href="#113262" class="name"> <strong class="user"><em>tobias</em></strong></a><a class="genanchor" href="#113262"> &para;</a><div class="date" title="2013-09-18 08:02"><strong>11 years ago</strong></div> <div class="text" id="Hcom113262"> <div class="phpcode"><code><span class="html">The mentioned example:<br /><br />$filename = 'somefile.txt';<br />if (file_exists($filename)) {<br /> echo "$filename wurde zuletzt modifiziert:: " . date ("F d Y H:i:s.", filemtime($filename));<br />}<br /><br />works, however is not ideal from a performance point of view of serving static files through PHP, since it basically needs two perform two file system operations (file_exists and filemtime). A more effective way would be to only use filemtime and save the overhead of file_exists using:<br /><br />$filename = 'somefile.txt';<br />$fmtime = filemtime($filename);<br />if (!$fmtime) {<br /> echo "$filename wurde zuletzt modifiziert:: " . date ("F d Y H:i:s.", $fmtime);<br />}</span></code></div> </div> </div> <div class="note" id="866"> <div class="votes"> <div id="Vu866"> <a href="/manual/vote-note.php?id=866&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd866"> <a href="/manual/vote-note.php?id=866&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V866" title="53% like this..."> 1 </div> </div> <a href="#866" class="name"> <strong class="user"><em>gerardj at home dot com</em></strong></a><a class="genanchor" href="#866"> &para;</a><div class="date" title="1999-05-19 08:27"><strong>25 years ago</strong></div> <div class="text" id="Hcom866"> <div class="phpcode"><code><span class="html">The above code works fine if you place it on each page you want a date stamp on. I've found that if you place a reference such as filemtime(__FILE__) in an included or required file, that the modification time of the inherited file will be returned, not the time of the file that did the ineriting.</span></code></div> </div> </div> <div class="note" id="124584"> <div class="votes"> <div id="Vu124584"> <a href="/manual/vote-note.php?id=124584&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd124584"> <a href="/manual/vote-note.php?id=124584&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V124584" title="50% like this..."> 0 </div> </div> <a href="#124584" class="name"> <strong class="user"><em>Steve GS</em></strong></a><a class="genanchor" href="#124584"> &para;</a><div class="date" title="2020-01-09 10:39"><strong>5 years ago</strong></div> <div class="text" id="Hcom124584"> <div class="phpcode"><code><span class="html">If the file is not in the same directory as the executing script, a failure will result unless you use $_SERVER. This is because, like file_exists(), the command works on a file system, not via HTTP. So this will NOT work properly (and will probably show a warning):<br /><br />echo '&lt;img src="' . $home_url . '/images/image.jpg?v=' . filemtime ($home_url . '/images/image.jpg') . '" /&gt;';<br /><br />but this will:<br /><br />echo '&lt;img src="' . $home_url . '/images/image.jpg?v=' . filemtime ($_SERVER['DOCUMENT_ROOT'] . '/images/image.jpg') . '" /&gt;';<br /><br />The second script above will ensure any updated image is guaranteed to replace its predecessor without forcing the browser to reload the image on every visit.</span></code></div> </div> </div> <div class="note" id="113385"> <div class="votes"> <div id="Vu113385"> <a href="/manual/vote-note.php?id=113385&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd113385"> <a href="/manual/vote-note.php?id=113385&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V113385" title="50% like this..."> 0 </div> </div> <a href="#113385" class="name"> <strong class="user"><em>arkadia81 at gmail dot com</em></strong></a><a class="genanchor" href="#113385"> &para;</a><div class="date" title="2013-10-04 01:56"><strong>11 years ago</strong></div> <div class="text" id="Hcom113385"> <div class="phpcode"><code><span class="html">A little amendment to "tobias" post: <br /><br />Insted of:<br />$filename = 'somefile.txt';<br /> $fmtime = filemtime($filename);<br /> if (!$fmtime) {<br /> echo "$filename wurde zuletzt modifiziert:: " . date ("F d Y H:i:s.", $fmtime);<br />}<br /><br />it should be something like this:<br />$filename = 'somefile.txt';<br /> $fmtime = filemtime($filename);<br /> if ($fmtime != false) {<br /> echo "$filename wurde zuletzt modifiziert:: " . date ("F d Y H:i:s.", $fmtime);<br /> }<br /><br />filemtime() returns false on failure so in the first example it will never display the date modified.</span></code></div> </div> </div> <div class="note" id="95165"> <div class="votes"> <div id="Vu95165"> <a href="/manual/vote-note.php?id=95165&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd95165"> <a href="/manual/vote-note.php?id=95165&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V95165" title="50% like this..."> 0 </div> </div> <a href="#95165" class="name"> <strong class="user"><em>james dot schafer at gotalk dot net dot au</em></strong></a><a class="genanchor" href="#95165"> &para;</a><div class="date" title="2009-12-16 01:36"><strong>15 years ago</strong></div> <div class="text" id="Hcom95165"> <div class="phpcode"><code><span class="html">On the Windows system filectime() returns the creation time. To find the changed time use fileatime() or filemtime().</span></code></div> </div> </div> <div class="note" id="95585"> <div class="votes"> <div id="Vu95585"> <a href="/manual/vote-note.php?id=95585&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd95585"> <a href="/manual/vote-note.php?id=95585&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V95585" title="42% like this..."> -1 </div> </div> <a href="#95585" class="name"> <strong class="user"><em>Nilo</em></strong></a><a class="genanchor" href="#95585"> &para;</a><div class="date" title="2010-01-10 11:01"><strong>15 years ago</strong></div> <div class="text" id="Hcom95585"> <div class="phpcode"><code><span class="html">Cheap and dirty way to code a cache:<br /><br /><span class="default">&lt;?php<br />$cache_file </span><span class="keyword">= </span><span class="string">'URI to cache file'</span><span class="keyword">;<br /></span><span class="default">$cache_life </span><span class="keyword">= </span><span class="string">'120'</span><span class="keyword">; </span><span class="comment">//caching time, in seconds<br /><br /></span><span class="keyword">if (!</span><span class="default">file_exists</span><span class="keyword">(</span><span class="default">$cache_file</span><span class="keyword">) or (</span><span class="default">time</span><span class="keyword">() - </span><span class="default">filemtime</span><span class="keyword">(</span><span class="default">$cache_file</span><span class="keyword">) &gt;= </span><span class="default">$cache_life</span><span class="keyword">)){<br /> </span><span class="default">ob_start</span><span class="keyword">();<br /> </span><span class="default">resource_consuming_function</span><span class="keyword">();<br /> </span><span class="default">file_put_contents</span><span class="keyword">(</span><span class="default">$cache_file</span><span class="keyword">,</span><span class="default">ob_get_flush</span><span class="keyword">());<br />}else{<br /> </span><span class="default">readfile</span><span class="keyword">(</span><span class="default">$cache_file</span><span class="keyword">);<br />}<br /></span><span class="default">?&gt;</span></span></code></div> </div> </div> <div class="note" id="114508"> <div class="votes"> <div id="Vu114508"> <a href="/manual/vote-note.php?id=114508&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd114508"> <a href="/manual/vote-note.php?id=114508&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V114508" title="40% like this..."> -1 </div> </div> <a href="#114508" class="name"> <strong class="user"><em>son9ne at gmail dot com</em></strong></a><a class="genanchor" href="#114508"> &para;</a><div class="date" title="2014-03-01 07:58"><strong>11 years ago</strong></div> <div class="text" id="Hcom114508"> <div class="phpcode"><code><span class="html">If you do use:<br /><span class="default">&lt;?php<br />$filename </span><span class="keyword">= </span><span class="string">'somefile.txt'</span><span class="keyword">;<br /></span><span class="default">$fmtime </span><span class="keyword">= </span><span class="default">filemtime</span><span class="keyword">(</span><span class="default">$filename</span><span class="keyword">);<br />if (!</span><span class="default">$fmtime</span><span class="keyword">) {<br /> echo </span><span class="string">"</span><span class="default">$filename</span><span class="string"> wurde zuletzt modifiziert:: " </span><span class="keyword">. </span><span class="default">date </span><span class="keyword">(</span><span class="string">"F d Y H:i:s."</span><span class="keyword">, </span><span class="default">$fmtime</span><span class="keyword">);<br />}<br /></span><span class="default">?&gt;<br /></span><br />Make note that it will throw a warning as the documentation states. While tobias makes a good point for production, when error reporting is off, this can increase your performance over using `file_exists`. However, if error reporting is on and it's writing to a log... this is not the case. Obviously this varies on sever specs and the size of the error log. For most cases, it's still ideal to use the `file_exists` method.</span></code></div> </div> </div> <div class="note" id="97194"> <div class="votes"> <div id="Vu97194"> <a href="/manual/vote-note.php?id=97194&amp;page=function.filemtime&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a> </div> <div id="Vd97194"> <a href="/manual/vote-note.php?id=97194&amp;page=function.filemtime&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a> </div> <div class="tally" id="V97194" title="37% like this..."> -2 </div> </div> <a href="#97194" class="name"> <strong class="user"><em>dagger80</em></strong></a><a class="genanchor" href="#97194"> &para;</a><div class="date" title="2010-04-07 12:22"><strong>14 years ago</strong></div> <div class="text" id="Hcom97194"> <div class="phpcode"><code><span class="html">Also on 32-bit systems, filemtime() also does not work for files with modification time set beyond Jan 2038. It is the dreaded time_t overflow bug for unix seconds.<br /><br />On windows you can set the system time to any arbitrary future date, and any new files you created or edited will automatically change to that future date.<br /><br />If exec isn't permitted for some reason, and if you could access the file via an web url (e.g. <a href="http://localhost/yourfile.txt" rel="nofollow" target="_blank">http://localhost/yourfile.txt</a>), another workaround is to get the Last-Modified time from the HTTP headers (e.g. get_headers(url) ), and parse it as a DateTime object.<br /><br />I have tested it and it works for years like 2050 and 3012.</span></code></div> </div> </div></div> <div class="foot"><a href="/manual/add-note.php?sect=function.filemtime&amp;repo=en&amp;redirect=https://www.php.net/manual/en/function.filemtime.php">+<small>add a note</small></a></div> </section> </section><!-- layout-content --> <aside class='layout-menu'> <ul class='parent-menu-list'> <li> <a href="ref.filesystem.php">Filesystem Functions</a> <ul class='child-menu-list'> <li class=""> <a href="function.basename.php" title="basename">basename</a> </li> <li class=""> <a href="function.chgrp.php" title="chgrp">chgrp</a> </li> <li class=""> <a href="function.chmod.php" title="chmod">chmod</a> </li> <li class=""> <a href="function.chown.php" title="chown">chown</a> </li> <li class=""> <a href="function.clearstatcache.php" title="clearstatcache">clearstatcache</a> </li> <li class=""> <a href="function.copy.php" title="copy">copy</a> </li> <li class=""> <a href="function.delete.php" title="delete">delete</a> </li> <li class=""> <a href="function.dirname.php" title="dirname">dirname</a> </li> <li class=""> <a href="function.disk-free-space.php" title="disk_&#8203;free_&#8203;space">disk_&#8203;free_&#8203;space</a> </li> <li class=""> <a href="function.disk-total-space.php" title="disk_&#8203;total_&#8203;space">disk_&#8203;total_&#8203;space</a> </li> <li class=""> <a href="function.diskfreespace.php" title="diskfreespace">diskfreespace</a> </li> <li class=""> <a href="function.fclose.php" title="fclose">fclose</a> </li> <li class=""> <a href="function.fdatasync.php" title="fdatasync">fdatasync</a> </li> <li class=""> <a href="function.feof.php" title="feof">feof</a> </li> <li class=""> <a href="function.fflush.php" title="fflush">fflush</a> </li> <li class=""> <a href="function.fgetc.php" title="fgetc">fgetc</a> </li> <li class=""> <a href="function.fgetcsv.php" title="fgetcsv">fgetcsv</a> </li> <li class=""> <a href="function.fgets.php" title="fgets">fgets</a> </li> <li class=""> <a href="function.fgetss.php" title="fgetss">fgetss</a> </li> <li class=""> <a href="function.file.php" title="file">file</a> </li> <li class=""> <a href="function.file-exists.php" title="file_&#8203;exists">file_&#8203;exists</a> </li> <li class=""> <a href="function.file-get-contents.php" title="file_&#8203;get_&#8203;contents">file_&#8203;get_&#8203;contents</a> </li> <li class=""> <a href="function.file-put-contents.php" title="file_&#8203;put_&#8203;contents">file_&#8203;put_&#8203;contents</a> </li> <li class=""> <a href="function.fileatime.php" title="fileatime">fileatime</a> </li> <li class=""> <a href="function.filectime.php" title="filectime">filectime</a> </li> <li class=""> <a href="function.filegroup.php" title="filegroup">filegroup</a> </li> <li class=""> <a href="function.fileinode.php" title="fileinode">fileinode</a> </li> <li class="current"> <a href="function.filemtime.php" title="filemtime">filemtime</a> </li> <li class=""> <a href="function.fileowner.php" title="fileowner">fileowner</a> </li> <li class=""> <a href="function.fileperms.php" title="fileperms">fileperms</a> </li> <li class=""> <a href="function.filesize.php" title="filesize">filesize</a> </li> <li class=""> <a href="function.filetype.php" title="filetype">filetype</a> </li> <li class=""> <a href="function.flock.php" title="flock">flock</a> </li> <li class=""> <a href="function.fnmatch.php" title="fnmatch">fnmatch</a> </li> <li class=""> <a href="function.fopen.php" title="fopen">fopen</a> </li> <li class=""> <a href="function.fpassthru.php" title="fpassthru">fpassthru</a> </li> <li class=""> <a href="function.fputcsv.php" title="fputcsv">fputcsv</a> </li> <li class=""> <a href="function.fputs.php" title="fputs">fputs</a> </li> <li class=""> <a href="function.fread.php" title="fread">fread</a> </li> <li class=""> <a href="function.fscanf.php" title="fscanf">fscanf</a> </li> <li class=""> <a href="function.fseek.php" title="fseek">fseek</a> </li> <li class=""> <a href="function.fstat.php" title="fstat">fstat</a> </li> <li class=""> <a href="function.fsync.php" title="fsync">fsync</a> </li> <li class=""> <a href="function.ftell.php" title="ftell">ftell</a> </li> <li class=""> <a href="function.ftruncate.php" title="ftruncate">ftruncate</a> </li> <li class=""> <a href="function.fwrite.php" title="fwrite">fwrite</a> </li> <li class=""> <a href="function.glob.php" title="glob">glob</a> </li> <li class=""> <a href="function.is-dir.php" title="is_&#8203;dir">is_&#8203;dir</a> </li> <li class=""> <a href="function.is-executable.php" title="is_&#8203;executable">is_&#8203;executable</a> </li> <li class=""> <a href="function.is-file.php" title="is_&#8203;file">is_&#8203;file</a> </li> <li class=""> <a href="function.is-link.php" title="is_&#8203;link">is_&#8203;link</a> </li> <li class=""> <a href="function.is-readable.php" title="is_&#8203;readable">is_&#8203;readable</a> </li> <li class=""> <a href="function.is-uploaded-file.php" title="is_&#8203;uploaded_&#8203;file">is_&#8203;uploaded_&#8203;file</a> </li> <li class=""> <a href="function.is-writable.php" title="is_&#8203;writable">is_&#8203;writable</a> </li> <li class=""> <a href="function.is-writeable.php" title="is_&#8203;writeable">is_&#8203;writeable</a> </li> <li class=""> <a href="function.lchgrp.php" title="lchgrp">lchgrp</a> </li> <li class=""> <a href="function.lchown.php" title="lchown">lchown</a> </li> <li class=""> <a href="function.link.php" title="link">link</a> </li> <li class=""> <a href="function.linkinfo.php" title="linkinfo">linkinfo</a> </li> <li class=""> <a href="function.lstat.php" title="lstat">lstat</a> </li> <li class=""> <a href="function.mkdir.php" title="mkdir">mkdir</a> </li> <li class=""> <a href="function.move-uploaded-file.php" title="move_&#8203;uploaded_&#8203;file">move_&#8203;uploaded_&#8203;file</a> </li> <li class=""> <a href="function.parse-ini-file.php" title="parse_&#8203;ini_&#8203;file">parse_&#8203;ini_&#8203;file</a> </li> <li class=""> <a href="function.parse-ini-string.php" title="parse_&#8203;ini_&#8203;string">parse_&#8203;ini_&#8203;string</a> </li> <li class=""> <a href="function.pathinfo.php" title="pathinfo">pathinfo</a> </li> <li class=""> <a href="function.pclose.php" title="pclose">pclose</a> </li> <li class=""> <a href="function.popen.php" title="popen">popen</a> </li> <li class=""> <a href="function.readfile.php" title="readfile">readfile</a> </li> <li class=""> <a href="function.readlink.php" title="readlink">readlink</a> </li> <li class=""> <a href="function.realpath.php" title="realpath">realpath</a> </li> <li class=""> <a href="function.realpath-cache-get.php" title="realpath_&#8203;cache_&#8203;get">realpath_&#8203;cache_&#8203;get</a> </li> <li class=""> <a href="function.realpath-cache-size.php" title="realpath_&#8203;cache_&#8203;size">realpath_&#8203;cache_&#8203;size</a> </li> <li class=""> <a href="function.rename.php" title="rename">rename</a> </li> <li class=""> <a href="function.rewind.php" title="rewind">rewind</a> </li> <li class=""> <a href="function.rmdir.php" title="rmdir">rmdir</a> </li> <li class=""> <a href="function.set-file-buffer.php" title="set_&#8203;file_&#8203;buffer">set_&#8203;file_&#8203;buffer</a> </li> <li class=""> <a href="function.stat.php" title="stat">stat</a> </li> <li class=""> <a href="function.symlink.php" title="symlink">symlink</a> </li> <li class=""> <a href="function.tempnam.php" title="tempnam">tempnam</a> </li> <li class=""> <a href="function.tmpfile.php" title="tmpfile">tmpfile</a> </li> <li class=""> <a href="function.touch.php" title="touch">touch</a> </li> <li class=""> <a href="function.umask.php" title="umask">umask</a> </li> <li class=""> <a href="function.unlink.php" title="unlink">unlink</a> </li> </ul> </li> </ul> </aside> </div><!-- layout --> <footer> <div class="container footer-content"> <div class="row-fluid"> <ul class="footmenu"> <li><a href="/manual/en/copyright.php">Copyright &copy; 2001-2025 The PHP Documentation Group</a></li> <li><a href="/my.php">My PHP.net</a></li> <li><a href="/contact.php">Contact</a></li> <li><a href="/sites.php">Other PHP.net sites</a></li> <li><a href="/privacy.php">Privacy policy</a></li> </ul> </div> </div> </footer> <script src="/cached.php?t=1731172202&amp;f=/js/ext/jquery-3.6.0.min.js"></script> <script src="/cached.php?t=1723177202&amp;f=/js/ext/FuzzySearch.min.js"></script> <script src="/cached.php?t=1707321815&amp;f=/js/ext/mousetrap.min.js"></script> <script src="/cached.php?t=1707321815&amp;f=/js/ext/jquery.scrollTo.min.js"></script> <script src="/cached.php?t=1738028401&amp;f=/js/search.js"></script> <script src="/cached.php?t=1738742401&amp;f=/js/common.js"></script> <script type="module" src="/cached.php?t=1741309802&amp;f=/js/interactive-examples.js"></script> <a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/to-top@2x.png"></a> <div id="search-modal__backdrop" class="search-modal__backdrop"> <div role="dialog" aria-label="Search modal" id="search-modal" class="search-modal" > <div class="search-modal__header"> <div class="search-modal__form"> <div class="search-modal__input-icon"> <!-- https://feathericons.com search --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <circle cx="11" cy="11" r="8"></circle> <line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </div> <input type="search" id="search-modal__input" class="search-modal__input" placeholder="Search docs" aria-label="Search docs" /> </div> <button aria-label="Close" class="search-modal__close"> <!-- https://pictogrammers.com/library/mdi/icon/close/ --> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="24" viewBox="0 0 24 24" > <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"/> </svg> </button> </div> <div role="listbox" aria-label="Search results" id="search-modal__results" class="search-modal__results" ></div> <div class="search-modal__helper-text"> <div> <kbd>↑</kbd> and <kbd>↓</kbd> to navigate • <kbd>Enter</kbd> to select • <kbd>Esc</kbd> to close </div> <div> Press <kbd>Enter</kbd> without selection to search using Google </div> </div> </div> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10