W3docs

gmdate()

Learn the PHP gmdate() function: format a UTC/GMT date and time, its syntax and parameters, common format characters, gmdate() vs date(), and runnable examples.

Introduction

The gmdate() function formats a Unix timestamp as a human-readable date and time string in GMT/UTC, ignoring the server's configured timezone. It is the UTC-only counterpart of date(): both take the same format characters, but gmdate() always returns the time in Greenwich Mean Time. This makes it the right tool whenever you need a timezone-independent value — logging an event, building an HTTP header, or storing a normalized timestamp that every server and client interprets the same way.

This guide covers the syntax, parameters, the most common format characters, how gmdate() differs from date(), and several runnable examples.

Syntax

gmdate(string $format, ?int $timestamp = null): string

Parameters

  • $format (required) — A string of format characters that are replaced with the corresponding date/time values. Any character not recognized as a format specifier is printed as-is. To output a literal letter that would otherwise be interpreted, escape it with a backslash (for example \T).
  • $timestamp (optional) — A Unix timestamp (seconds elapsed since January 1, 1970, 00:00:00 UTC). When omitted or null, the current time is used.

The function returns the formatted date/time as a string, always expressed in UTC regardless of date_default_timezone_set() or the server's date.timezone setting.

Common format characters

These are the specifiers you will reach for most often. They are identical to the ones used by date().

CharacterMeaningExample
Y4-digit year2026
mMonth, 2 digits with leading zero06
dDay of the month, 2 digits04
HHour, 24-hour format (00–23)05
iMinutes (00–59)45
sSeconds (00–59)57
DDay of the week, short textSun
MMonth, short textMar
TTimezone abbreviationGMT
USeconds since the Unix epoch1646372757

Examples

Example 1: Print the current GMT/UTC date and time

With no timestamp argument, gmdate() formats the current moment in UTC.

php— editable, runs on the server

Example 2: Format the date and time with custom specifiers

A common pattern is building a readable, RFC-style timestamp. Because gmdate() is UTC, the T specifier always prints GMT.

php— editable, runs on the server

Example 3: Format a specific timestamp

Pass a Unix timestamp as the second argument to format a fixed point in time instead of "now".

php— editable, runs on the server

gmdate() vs date()

date() formats a timestamp using the local timezone configured for the script, while gmdate() always formats in UTC. Given the same timestamp, the two return the same string only when the local timezone is UTC.

<?php

date_default_timezone_set('America/New_York');
$timestamp = 1646372757;

echo date('Y-m-d H:i:s', $timestamp);   // 2022-03-04 00:45:57 (local, UTC-5)
echo "\n";
echo gmdate('Y-m-d H:i:s', $timestamp); // 2022-03-04 05:45:57 (UTC)

Use gmdate() when the value must be timezone-independent (logs, HTTP Date headers, API responses, database storage). Use date() when you are displaying a time to a user in their local zone.

When to use gmdate()

  • Standardized logging — store timestamps in UTC so entries from servers in different regions stay comparable.
  • HTTP headers and caching — headers such as Expires and Last-Modified must be expressed in GMT.
  • APIs and data exchange — return UTC so clients can convert to any local zone reliably.

For full object-oriented date handling with explicit timezone conversion, prefer the DateTime and DateTimeZone classes. To build the timestamps you pass to gmdate(), see gmmktime() (UTC) and mktime() (local time).

Conclusion

The gmdate() function provides a straightforward way to format dates and times in UTC, using the same format characters as date() but always anchored to GMT. This guide covered its syntax, parameters, the most common format characters, the difference from date(), and practical examples to help you integrate it into your projects. For more on working with dates in PHP, see the PHP Date and Time overview.

Practice

Practice
What is the purpose of the gmdate() function in PHP?
What is the purpose of the gmdate() function in PHP?
Was this page helpful?