<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// The following is pre-article content, unrelated to the main content</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"This is an example of pre-article content, which can include site announcements or copyright information.\n"</span></span><span>;
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p></span><?php<br>
// Main content starts<br>
echo "<h1>How to Format PHP Dates Using getdate() and Display the Timezone Information</h1>";</p>
<p>// 1. Get the current timestamp<br>
$timestamp = time();</p>
<p>// 2. Use getdate() to get the time array<br>
$dateArray = getdate($timestamp);</p>
<p>// 3. Format date information<br>
echo "<p>Current Year: " . $dateArray['year'] . "</p>";<br>
echo "<p>Current Month: ". </span>$dateArray[</span>'mon'] . </span>"</p>";<br>
</span>echo "<p>Current Day: ". </span>$dateArray[</span>'mday'] . </span>"</p>";<br>
</span>echo "<p>Weekday: ". </span>$dateArray[</span>'weekday'] . </span>"</p>";<br>
</span>echo "<p>Hour: ". </span>$dateArray[</span>'hours'] . </span>"</p>";<br>
</span>echo "<p>Minute: ". </span>$dateArray[</span>'minutes'] . </span>"</p>";<br>
</span>echo "<p>Second: ". </span>$dateArray[</span>'seconds'] . </span>"</p>";</p>
<p></span>// 4. Get and display the timezone<br>
$timezone = date_default_timezone_get();<br>
echo "<p>Current Timezone: ". </span>$timezone. </span>"</p>";</p>
<p></span>// 5. Display the full formatted date<br>
$formattedDate = sprintf(<br>
"%04d-%02d-%02d %02d:%02d:%02d",<br>
$dateArray[</span>'year'],<br>
</span>$dateArray[</span>'mon'],<br>
</span>$dateArray[</span>'mday'],<br>
</span>$dateArray[</span>'hours'],<br>
</span>$dateArray[</span>'minutes'],<br>
</span>$dateArray[</span>'seconds']<br>
);<br>
</span>echo "<p>Full Formatted Date (with Timezone): ". </span>$formattedDate. </span>. $timezone. </span>"</p>";</p>
<p>// End of article<br>
?><br>
</span>
This example article demonstrates how to:
Use getdate() to retrieve a detailed array of the current date and time.
Access year, month, day, hour, minute, second, and weekday information from the array.
Use date_default_timezone_get() to get and display the current timezone.
Format the date into a readable full date string with sprintf() and include the timezone.
You can run this PHP code directly to see the formatted date and timezone information.