Content · Programming · PHP

PHP getlastmod() Function: Usage and Examples

Understand PHP getlastmod() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use getlastmod() when you need to returns the modification timestamp of the script handling the request in a PHP application. The examples below show the actual operation and its limitations.

What Is getlastmod() in PHP?

Returns the modification timestamp of the script handling the request. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

getlastmod(): int|false

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

var_dump(getlastmod() === false || is_int(getlastmod()));

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

echo getlastmod() === false ? "Unavailable" : "Timestamp available";

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

$lastModified = getlastmod(); if ($lastModified !== false) echo date("Y-m-d",$lastModified);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

The result refers to the current script, not an arbitrary file; CLI and web environments can differ.

Comparison With Related Functions

ApproachDifference
getlastmod()Returns the modification timestamp of the script handling the request.
Related operationfilemtime() reads the modification time of a named file.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationThe result refers to the current script, not an arbitrary file; CLI and web environments can differ.
Confusing this function with a related operationfilemtime() reads the modification time of a named file.

Key Takeaways

  • Returns the modification timestamp of the script handling the request.
  • The result refers to the current script, not an arbitrary file; CLI and web environments can differ.
  • filemtime() reads the modification time of a named file.

Use getlastmod() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.