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}";
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}";
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}";
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}";
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.