Current Location: Home> Latest Articles> How to Use gmdate to Output ISO 8601 Formatted Time? Practical Code Examples

How to Use gmdate to Output ISO 8601 Formatted Time? Practical Code Examples

gitbox 2025-08-26
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Unrelated preface PHP code</span></span><span>
</span><span><span class="hljs-variable">$dummyVar</span></span><span> = </span><span><span class="hljs-string">"This part of the code is unrelated to the main text, only a placeholder"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strtoupper</span></span><span>(</span><span><span class="hljs-variable">$dummyVar</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>Title: How to Use gmdate to Output ISO 8601 Formatted Time? Practical Code Examples</p>
</li>
<li></li>
<li>
<p>ISO 8601 is an international standard for representing date and time, commonly used in APIs, logging, or cross-time-zone data exchange.</p>
</li>
<li>
<p>In PHP, we can use the gmdate() function to generate UTC time formatted according to ISO 8601.</p>
</li>
<li></li>
<li>
<p>Common ISO 8601 format examples:</p>
</li>
<li>
<ol>
<li>
<p>Full date and time: YYYY-MM-DDTHH:MM:SSZ  (e.g., 2025-08-19T12:34:56Z)</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>Date only: YYYY-MM-DD</p>
</li>
</ol>
</li>
<li></li>
<li>
<p>gmdate() works similarly to date(), but it returns Greenwich Mean Time (UTC).<br>
*/</p>
</li>
</ul>
<p>// Current UTC time<br>
$utcTime = gmdate("Y-m-d\TH:i:s\Z");<br>
echo "Current UTC time (ISO 8601 format): $utcTime<br>";</p>
<p>// Using DateTime object with timezone<br>
$date = new DateTime("now", new DateTimeZone("UTC"));<br>
echo "ISO 8601 time generated using DateTime object: " . $date->format(DateTime::ATOM) . "<br>";</p>
<p>/**</p>
<ul>
<li>
<p>Useful Tips:</p>
</li>
<li>
<ol>
<li>
<p>gmdate("c") does not always strictly match ISO 8601 UTC format, so it's recommended to use gmdate("Y-m-d\TH:i:s\Z").</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>If you need milliseconds or microseconds, you can use DateTime with DateTime::format('Y-m-d\TH:i:s.u\Z').<br>
*/<br>
?></p>
</li>
</ol>
</li>
</ul>
<p data-is-last-node="" data-is-only-node=""><?php<br>
// Unrelated ending PHP code<br>
function footerNote() {<br>
return "The examples in this article are for demonstrating PHP date handling only.";<br>
}<br>
echo "<footer>" . footerNote() . "</footer>";<br>
?><br>
</span>