PHP时间戳时区配置 [英] PHP timestamps & timezone configuration
问题描述
我的计划:
- 使用
获取当前时间戳strtotime(now)
- 将其转换为时区0 - 这是我不知道该怎么做的部分。我有代表用户时区的号码,例如-8小时。
- 将其存储在时区0中的数据库中
- 检索来自时区0的数据库
- 以相反的方向将其转换为用户时区
- 使用
日期('',timestamp)
显示它的功能
如何完成转换?或者我会这样做错了?
我需要能够以数字表示的时间将其存储在数据库中(如strtotime返回)
使用 time()
与 strtotime(now )
,您不需要担心转换时区的时区,因为时间戳没有时区:
time
返回一个UNIX时间戳,它与时区无关。因为
a UNIX时间戳表示自1970年UTC以来的秒数,您可以说它是
UTC,但它真的没有时区。
然后可以将该时间戳存储在数据库中。当您检索它时,您可以将其转换为用户时区。有这样的东西:
date_default_timezone_set('UTC');
$ timestamp ='1429066967';
//支持的时区:http://php.net/manual/en/timezones.php
$ userTimezone ='America / Los_Angeles';
$ dt = new DateTime();
//设置时间戳
$ dt-> setTimestamp($ timestamp);
//设置时区
$ dt-> setTimezone(new DateTimeZone($ userTimezone));
//格式化日期
$ date = $ dt->格式('Y-m-d H:i:s');
echo $ date;
输出: 2015-04-14 20:02:47 p>
但是,如果您只有UTC偏移量,可以尝试以下方式:
date_default_timezone_set('UTC');
$ timestamp ='1429066967';
$ offset = -8
$ userTimezone = timezone_name_from_abbr(,$ offset * 3600,false);
$ dt = new DateTime();
//设置时间戳
$ dt-> setTimestamp($ timestamp);
//设置时区
$ dt-> setTimezone(new DateTimeZone($ userTimezone));
//格式化日期
$ date = $ dt->格式('Y-m-d H:i:s');
echo $ date;
哪些也输出: 2015-04-14 20:02:47
My plan:
- Get the current timestamp using
strtotime("now")
- Convert that to timezone '0' - This is the part I don't know how to do. I have the number that represents the users timezone, like -8 hours for example.
- Store it in the database in timezone '0'
- Retrieve it from the database in timezone '0'
- Convert it to the users timezone in the opposite direction
- use the
date('', timestamp)
function to display it
How can I accomplish the conversion? Or am I going about this wrong?
I need to be able to store it in the database as a numerically represented time (like strtotime returns)
Using time()
is the same as strtotime("now")
and you do not need to worry about converting the timezone of the timestamp, as the timestamp has no timezone:
Does PHP time() return a GMT/UTC Timestamp?
time
returns a UNIX timestamp, which is timezone independent. Since a UNIX timestamp denotes the seconds since 1970 UTC you could say it's UTC, but it really has no timezone.
You can then store that timestamp in your database. When you retrieve it you can convert it to the users timezone. With something like this:
date_default_timezone_set('UTC');
$timestamp = '1429066967';
//Supported Timezones: http://php.net/manual/en/timezones.php
$userTimezone = 'America/Los_Angeles';
$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');
echo $date;
Outputs: 2015-04-14 20:02:47
But if you only have the UTC offset you could try this:
date_default_timezone_set('UTC');
$timestamp = '1429066967';
$offset = -8;
$userTimezone = timezone_name_from_abbr("", $offset*3600, false);
$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');
echo $date;
Which also outputs: 2015-04-14 20:02:47
这篇关于PHP时间戳时区配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!