Current Location: Home> Latest Articles> How to Use gmdate to Get the Current GMT Time? Practical Examples You Can Master Instantly

How to Use gmdate to Get the Current GMT Time? Practical Examples You Can Master Instantly

gitbox 2025-09-08

How to Use gmdate to Get the Current GMT Time? Practical Examples You Can Master Instantly

In PHP, gmdate() is a very useful function that helps you get the current GMT time. GMT (Greenwich Mean Time) is one of the world standard times, commonly used to represent a unified global time. Unlike local time, GMT is not affected by time zone changes, making it ideal for maintaining consistency when recording and transmitting time data.

This article will show you how to use the gmdate() function to get the current GMT time, along with practical examples to help you understand and apply it effectively.

What Is the gmdate() Function?

The gmdate() function is a date and time formatting function in PHP that returns the current GMT time according to a specified format. Its syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">gmdate</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$format</span></span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$timestamp</span></span> = </span><span><span class="hljs-title function_ invoke__">time</span></span><span>()) : </span><span><span class="hljs-keyword">string</span></span><span>
</span></span>
  • $format: This is a string that formats the date and time, similar to the date() function. Common format characters include: Y (four-digit year), m (month), d (day), H (hour), i (minutes), s (seconds), and more.

  • $timestamp: This parameter is a timestamp, with the default value being time(), i.e., the current time. If you want to get the GMT time for a specific moment, you can pass the timestamp of that time.

Example 1: Get the Current GMT Time

The most common use is to get the current GMT time. You can do this with the following code:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">gmdate</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Output example:

<span><span><span class="hljs-number">2025-07-14 12:34:56</span></span><span>
</span></span>

In this example, gmdate('Y-m-d H:i:s') returns the current GMT time in the format YYYY-MM-DD HH:MM:SS.

Example 2: Get the Current GMT Timestamp

If you need the current GMT timestamp (the number of seconds since the Unix epoch), you can combine gmdate() with strtotime():

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strtotime</span></span><span>(</span><span><span class="hljs-title function_ invoke__">gmdate</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span><span>));
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Output example:

<span><span>1636736400
</span></span>

Example 3: Customize GMT Time Format

You can customize the output format according to your needs, for example, displaying only the year and month, or just the time:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">gmdate</span></span><span>(</span><span><span class="hljs-string">'Y-m'</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Output example:

<span><span><span class="hljs-number">2025-07</span></span><span>
</span></span>

Or display only the time:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">gmdate</span></span><span>(</span><span><span class="hljs-string">'H:i:s'</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Output example:

<span><span><span class="hljs-section">12:34:56</span></span><span>
</span></span>

Example 4: Get GMT Time for a Specific Timestamp

If you want to get the GMT time for a specific timestamp, you can pass it as a parameter:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$timestamp</span></span><span> = </span><span><span class="hljs-number">1625932800</span></span><span>; </span><span><span class="hljs-comment">// 2021-07-10 00:00:00 UTC</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">gmdate</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span><span>, </span><span><span class="hljs-variable">$timestamp</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Output example:

<span><span><span class="hljs-number">2021-07-10 00:00:00</span></span><span>
</span></span>

Conclusion

From the examples above, you should now have a clearer understanding of the gmdate() function in PHP. Whether you need to get the current GMT time, timestamp, or customize the time format, gmdate() can conveniently help you handle time-related operations.

If you encounter issues with time conversion or formatting in your development, try using gmdate(); it’s a powerful tool for working with GMT time.