<?php
// This code is unrelated to the main article, only for demonstration
echo "Welcome to this article!";
?>
<p><hr></p>
<p><?php<br>
/**</p>
<ul>
<li>
<p>Title: How to Use timezone_location_get with date() to Retrieve and Format Accurate Time?</p>
</li>
<li></li>
<li>
<p>In PHP, handling time zone–related time data requires accurately retrieving time zone location information and formatting time correctly.</p>
</li>
<li>
<p>This article will show how to use PHP’s built-in timezone_location_get() function along with the date() function to get the geographical coordinates</p>
</li>
<li>
<p>of a time zone and finally output a formatted time string.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Understanding timezone_location_get()</p>
</li>
</ol>
</li>
<li>
<p>The timezone_location_get() function takes a DateTimeZone object as its parameter and returns geographical information about the time zone,</p>
</li>
<li>
<p>including longitude, latitude, and the country code.</p>
</li>
<li></li>
<li>
<p>Syntax example:</p>
</li>
<li>
<p>$tz = new DateTimeZone('Asia/Shanghai');</p>
</li>
<li>
<p>$location = timezone_location_get($tz);</p>
</li>
<li>
<p>var_dump($location);</p>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Formatting Time with the date() Function</p>
</li>
</ol>
</li>
<li>
<p>The date() function formats a timestamp into a readable time string. To ensure accuracy, you should first set the correct time zone using DateTime,</p>
</li>
<li>
<p>and then call the format() method.</p>
</li>
<li></li>
<li>
<ol start="3">
<li>
<p>Complete Example Code<br>
*/</p>
</li>
</ol>
</li>
</ul>
<p>date_default_timezone_set('UTC'); // Set default time zone to avoid warnings</p>
<p>// Create a DateTimeZone object with a specific time zone<br>
$timezoneName = 'Europe/London';<br>
$tz = new DateTimeZone($timezoneName);</p>
<p>// Retrieve location info for the time zone<br>
$location = timezone_location_get($tz);</p>
<p>// Output time zone geographic info<br>
echo "Time Zone Name: " . $timezoneName . PHP_EOL;<br>
echo "Country Code: " . $location['country_code'] . PHP_EOL;<br>
echo "Latitude: " . $location['latitude'] . PHP_EOL;<br>
echo "Longitude: " . $location['longitude'] . PHP_EOL;</p>
<p>// Create DateTime object with specified time zone<br>
$datetime = new DateTime('now', $tz);</p>
<p>// Format time using the date() function<br>
// Here we use the format() method, which works similarly to date() but respects the DateTime object's time zone<br>
$formattedTime = $datetime->format('Y-m-d H:i:s');</p>
<p>echo "Current Time (" . $timezoneName . "): " . $formattedTime . PHP_EOL;</p>
<p>/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>4. Summary</p>
</li>
<li>
<ol>
<li>
<p>timezone_location_get() provides geographic information for a time zone, making it easier to understand the corresponding area.</p>
</li>
</ol>
</li>
<li>
<ol start="2">
<li>
<p>Using DateTime with DateTimeZone ensures accurate time formatting with respect to the specified time zone.</p>
</li>
</ol>
</li>
<li>
<ol start="3">
<li>
<p>Using date() directly without setting the time zone may result in incorrect times.</p>
</li>
</ol>
</li>
<li></li>
<li data-is-last-node="">
<p data-is-last-node="">By following the approach in this article, you can more accurately manage and display time data across different time zones.<br>
*/<br>