【发布时间】:2011-03-04 03:40:34
【问题描述】:
使用 PHP 以 PST(西海岸)时间显示当前时间的最简单方法是什么?
【问题讨论】:
使用 PHP 以 PST(西海岸)时间显示当前时间的最简单方法是什么?
【问题讨论】:
嗯,最简单的可能是:
date_default_timezone_set('America/Los_Angeles');
echo date('Y-m-d');
查看supported timezones,找到适合您需求的。
【讨论】:
.
echo date('r');
putenv('TZ=PST');
echo date('r');
【讨论】:
在时区之间转换日期/时间:
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
【讨论】:
让我们尝试一个使用 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"
【讨论】:
如果您正在使用或有权访问 Carbon,您可以这样做:
$timezone = 'America/Los_Angeles';
$now = Carbon::now()->tz($timezone)->toDateTimeString();
echo $now;
【讨论】: