Current Location: Home> Latest Articles> Complete Guide to Converting US Time to China Time in PHP

Complete Guide to Converting US Time to China Time in PHP

M66 2025-10-07

Getting US Time

When handling timezone conversion in PHP, the first step is to get the US time. You can use the date_default_timezone_set function to set the timezone to US Eastern Time, then use the date function to get the current time. For example:

date_default_timezone_set('America/New_York');
$us_time = date('Y-m-d H:i:s');
echo "US time: {$us_time}";

Converting US Time to Timestamp

To facilitate further calculations, the US time should be converted to a timestamp. You can use the strtotime function:

$us_timestamp = strtotime($us_time);
echo "US timestamp: {$us_timestamp}";

Converting Timestamp to China Time

After obtaining the US timestamp, it can be converted to China time. Set the timezone to China first, then use the date function:

date_default_timezone_set('Asia/Shanghai');
$cn_time = date('Y-m-d H:i:s', $us_timestamp);
echo "China time: {$cn_time}";

Complete Code Example

Here is the complete example that combines the above steps:

date_default_timezone_set('America/New_York');
$us_time = date('Y-m-d H:i:s');
$us_timestamp = strtotime($us_time);

date_default_timezone_set('Asia/Shanghai');
$cn_time = date('Y-m-d H:i:s', $us_timestamp);

echo "US time: {$us_time}";
echo "US timestamp: {$us_timestamp}";
echo "China time: {$cn_time}";

Conclusion

With these steps, you can successfully convert US time to China time in PHP. In practical applications, you can adjust the timezone and format as needed. Mastering these methods makes it easier to handle time information from different regions.