php PHP日期(); 带时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20288789/
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
PHP date(); with timezone?
提问by user2917204
So I've checked the list of supported time zones in PHPand I was wondering how could I include them in the date(); function? Thanks!
所以我检查了PHP中支持的时区列表,我想知道如何将它们包含在 date(); 中。功能?谢谢!
I don't want a default timezone, each user has their timezone stored in the database, I take that timezone of the user and use it. How? I know how to take it from the database, not how to use it, though.
我不想要默认时区,每个用户都将他们的时区存储在数据库中,我使用用户的时区并使用它。如何?我知道如何从数据库中获取它,但不知道如何使用它。
回答by N.B.
For such task, you should really be using PHP's DateTime class. Please ignore all of the answers advising you to use date() or date_set_time_zone, it's simply bad and outdated.
对于此类任务,您确实应该使用 PHP 的 DateTime 类。请忽略所有建议您使用 date() 或 date_set_time_zone 的答案,它只是糟糕且过时。
I'll use pseudocode to demonstrate, so try to adjust the code to suit your needs.
我将使用伪代码进行演示,因此请尝试调整代码以满足您的需求。
Assuming that variable $tz contains string name of a valid time zone and variable $timestamp contains the timestamp you wish to format according to time zone, the code would look like this:
假设变量 $tz 包含有效时区的字符串名称,变量 $timestamp 包含您希望根据时区格式化的时间戳,代码将如下所示:
$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('d.m.Y, H:i:s');
DateTime class is powerful, and to grasp all of its capabilities - you should devote some of your time reading about it at php.net. To answer your question fully - yes, you can adjust the time zone parameter dynamically (on each iteration while reading from db, you can create a new DateTimeZone() object).
DateTime 类功能强大,要掌握它的所有功能 - 您应该花一些时间在 php.net 上阅读它。要完全回答您的问题 - 是的,您可以动态调整时区参数(在从 db 读取时的每次迭代中,您可以创建一个新的 DateTimeZone() 对象)。
回答by Suresh Kamrushi
If I understood correct,You need to set time zone first like:
如果我理解正确,您需要先设置时区,例如:
date_default_timezone_set('UTC');
And than you can use date function:
并且您可以使用日期功能:
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
回答by ceejayoz
Use the DateTime classinstead, as it supports timezones. The DateTime equivalent of date()is DateTime::format.
请改用DateTime 类,因为它支持时区。DateTime 的等效项date()是DateTime::format。
An extremely helpful wrapper for DateTime is Carbon- definitely give it a look.
DateTime 的一个非常有用的包装器是Carbon- 一定要看看它。
You'll want to store in the database as UTC and convert on the application level.
您需要将 UTC 存储在数据库中并在应用程序级别进行转换。
回答by Andrew
The answer abovecaused me to jump through some hoops/gotchas, so just posting the cleaner code that worked for me:
上面的答案让我跳过了一些圈套/陷阱,所以只需发布对我有用的更干净的代码:
$dt = new DateTime();
$dt->setTimezone(new DateTimeZone('America/New_York'));
$dt->setTimestamp(123456789);
echo $dt->format('F j, Y @ G:i');
回答by user3049608
It should like this:
它应该是这样的:
date_default_timezone_set('America/New_York');
回答by emmanuel
this works perfectly in 2019:
这在 2019 年完美运行:
date('Y-m-d H:i:s',strtotime($date. ' '.$timezone));
回答by dzona
Try this. You can pass either unix timestamp, or datetime string
尝试这个。您可以传递 unix 时间戳或日期时间字符串
public static function convertToTimezone($timestamp, $fromTimezone, $toTimezone, $format='Y-m-d H:i:s')
{
$datetime = is_numeric($timestamp) ?
DateTime::createFromFormat ('U' , $timestamp, new DateTimeZone($fromTimezone)) :
new DateTime($timestamp, new DateTimeZone($fromTimezone));
$datetime->setTimezone(new DateTimeZone($toTimezone));
return $datetime->format($format);
}
回答by Shatadip
I have created this very straightforward function, and it works like a charm:
我创建了这个非常简单的函数,它的作用就像一个魅力:
function ts2time($timestamp,$timezone){ /* input: 1518404518,America/Los_Angeles */
$date = new DateTime(date("d F Y H:i:s",$timestamp));
$date->setTimezone(new DateTimeZone($timezone));
$rt=$date->format('M d, Y h:i:s a'); /* output: Feb 11, 2018 7:01:58 pm */
return $rt;
}
回答by Yatin Trivedi
You can replace database value in date_default_timezone_set function, date_default_timezone_set(SOME_PHP_VARIABLE); but just needs to take care of exact values relevant to the timezones.
您可以在 date_default_timezone_set 函数中替换数据库值,date_default_timezone_set(SOME_PHP_VARIABLE); 但只需要处理与时区相关的确切值。

