php 以 PST 显示时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3048162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Time in PST
提问by Zach Smith
What is easiest way to display the current time in PST (West Coast) time using PHP?
使用 PHP 以 PST(西海岸)时间显示当前时间的最简单方法是什么?
回答by Tatu Ulmanen
Well, the easiest might be:
好吧,最简单的可能是:
date_default_timezone_set('America/Los_Angeles');
echo date('Y-m-d');
Take a look at supported timezonesto find one suitable for your needs.
回答by Charles
Let's try a solution that uses PHP's modern date handling. This example requires PHP 5.2 or better.
让我们尝试一个使用 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"
回答by zeros-and-ones
If you are using or have access to Carbon you could do this:
如果您正在使用或有权访问 Carbon,您可以这样做:
$timezone = 'America/Los_Angeles';
$now = Carbon::now()->tz($timezone)->toDateTimeString();
echo $now;
回答by Sarfraz
.
.
echo date('r');
putenv('TZ=PST');
echo date('r');
回答by Jeriko
To convert a date/time between timezones:
要在时区之间转换日期/时间:
include ("Date.php");
$d = new Date("2010-06-21 10:59:27"); // initialize object
$d->setTZByID("GMT"); // set local time zone
$d->convertTZByID("PST"); // convert to foreign time zone
echo $d->format("%A, %d %B %Y %T"); // retrieve converted date/time

