小编Joh*_*ohn的帖子

PHP UTC到当地时间

服务器环境

Redhat Enterprise Linux
PHP 5.3.5

问题

假设我有一个UTC日期和时间,例如2011-04-27 02:45,我想把它转换成我当地的时间,即America/New_York.

三个问题:

1.)我的代码可以解决问题,你同意吗?

<?php

date_default_timezone_set('America/New_York');  // Set timezone.

$utc_ts = strtotime("2011-04-27 02:45");  // UTC Unix timestamp.

// Timezone offset in seconds. The offset for timezones west of UTC is always negative,
// and for those east of UTC is always positive.
$offset = date("Z");

$local_ts = $utc_ts + $offset;  // Local Unix timestamp. Add because $offset is negative.

$local_time = date("Y-m-d g:i A", $local_ts);  // Local time as yyyy-mm-dd h:m am/pm.

echo …
Run Code Online (Sandbox Code Playgroud)

php utc dst

21
推荐指数
2
解决办法
4万
查看次数

标签 统计

dst ×1

php ×1

utc ×1