PHP gmstrftime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, gmstrftime() formats a timestamp as a GMT (UTC) string using C-style % format codes. It is deprecated and no longer available in modern PHP.

This function is deprecated as of PHP 8.1 (removal planned for a future PHP major). It is documented here so you can understand older code. For new code, use gmdate() or IntlDateFormatter instead.

Syntax

Call gmstrftime() with the values you need. Start with this line.

PHP
$text = gmstrftime('%Y-%m-%d', 1700000000);
CallWhat it does
gmstrftime('%Y-%m-%d', 1700000000)Returns "2023-11-14" on PHP versions that still have it.
gmstrftime('%A, %B %d', 1700000000)Returns "Tuesday, November 14" in the default C locale.
gmstrftime('%H:%M:%S', 1700000000)Returns "22:13:20".

Do not use this function in new code. Wrap old calls with function_exists('gmstrftime') while you migrate. gmdate() covers most cases, and IntlDateFormatter is the right tool when you need translated month and day names.

Library declarations
PHP
gmstrftime(string $format, ?int $timestamp = null): string|false

Parameters

ParameterDescription
formatRequired. A format string with % codes, such as %Y, %m, %d, %H, %M, %S, %A and %B.
timestampOptional. A Unix timestamp. If it is null or left out, the current time is used.

Return Value

Return ValueDescription
stringThe formatted GMT date and time.
falseReturned on failure.

PHP Examples

Example 1 — Legacy Call with a Safe Fallback

Check that the function exists before calling it. When it is missing, fall back to gmdate() so both paths print the same text.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

$ts = 1700000000;

if (function_exists('gmstrftime')) {
    echo gmstrftime('%A, %d %B %Y', $ts), PHP_EOL;
    echo gmstrftime('%H:%M:%S', $ts), PHP_EOL;
} else {
    echo gmdate('l, d F Y', $ts), PHP_EOL;
    echo gmdate('H:i:s', $ts), PHP_EOL;
}

How It Works

  1. error_reporting() hides the deprecation notice on PHP 8. 1 to 8.3.
  2. function_exists() prevents a fatal error on versions where the function was removed.
  3. %A, %d, %B and %Y match l, d, F and Y in gmdate().
  4. %H:%M:%S matches H:i:s. Note that %M is minutes, but the date() letter for minutes is i.

Example 2 — Epoch and the Last Second of Day One

Boundary timestamps behave like any other value.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

foreach ([0, 86399] as $ts) {
    if (function_exists('gmstrftime')) {
        echo gmstrftime('%Y-%m-%d %H:%M:%S', $ts), PHP_EOL;
    } else {
        echo gmdate('Y-m-d H:i:s', $ts), PHP_EOL;
    }
}

How It Works

  1. Timestamp 0 is midnight on 1 January 1970 UTC.
  2. 86399 is 23:59:59 on the same day, one second before day two begins.
  3. The loop reuses the same guard for each value.
  4. Both branches print identical text, so your output never depends on the PHP version.

Example 3 — Migrate % Codes to gmdate()

A small helper can translate common legacy codes into gmdate() letters while you update old code.

PHP
<?php
function legacyToGmdate(string $format, int $ts): string
{
    $map = [
        '%Y' => 'Y', '%m' => 'm', '%d' => 'd',
        '%H' => 'H', '%M' => 'i', '%S' => 's',
        '%A' => 'l', '%B' => 'F',
    ];

    return gmdate(strtr($format, $map), $ts);
}

echo legacyToGmdate('%A, %d %B %Y %H:%M', 1700000000), PHP_EOL;

How It Works

  1. The map pairs each old % code with the matching gmdate() letter.
  2. strtr() swaps every code in the format string in one pass.
  3. gmdate() then formats the timestamp with the new format.
  4. Only the codes in the map are converted, so add more pairs if your old formats need them.

Frequently Asked Questions

No. It was Deprecated in PHP 8.1 (removal planned for a future PHP major). Use gmdate() or IntlDateFormatter in new code.
Use gmdate() for UTC output with English names. Use IntlDateFormatter with the 'UTC' time zone when you need translated month and weekday names.
Its output depended on the system locale, which behaved differently across operating systems and was hard to test. The newer APIs are more reliable.
Wrap calls with function_exists('gmstrftime') and fall back to gmdate() or IntlDateFormatter, or replace the calls straight away.
gmstrftime() always formats in UTC, while strftime() uses the default time zone. Both are deprecated in PHP 8.1+.

Next: idate()

idate() returns one part of a date or time as an integer, such as the year, month, day or .

idate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful