在PST中显示时间

Zac*_*ith 13 php timezone

使用PHP在PST(西海岸)时间显示当前时间的最简单方法是什么?

Tat*_*nen 20

嗯,最简单的可能是:

date_default_timezone_set('America/Los_Angeles');
echo date('Y-m-d');
Run Code Online (Sandbox Code Playgroud)

查看支持的时区,找到适合您需求的时区.


Cha*_*les 11

让我们尝试使用PHP的现代日期处理的解决方案.此示例需要PHP 5.2或更高版本.

// Right now it's about four minutes before 1 PM, PST.
$pst = new DateTimeZone('America/Los_Angeles');
$three_hours_ago = new DateTime('-3 hours', $pst); // first argument uses strtotime parsing
echo $three_hours_ago->format('Y-m-d H:i:s'); // "2010-06-15 09:56:36"
Run Code Online (Sandbox Code Playgroud)