如何在mysql中存储用户时区? [英] How to store users timezone in mysql?

查看:98
本文介绍了如何在mysql中存储用户时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在MySQL中存储时区的数据类型/结构


我将在我的数据库中只存储UTC时间,并希望使用PHP将时间戳转换为用户的本地时间。



我的问题是,在数据库中存储用户时区的最佳方式?



IE:-400,-4,-4:00



据说,我知道如何手动执行数学:

  $ utc_time = strtotime(2011-10-02 23:00:00); 
$ my_time = $ utc_time - 14400;

echo date($ timeformat,$ mytime);

我的问题是,用户的时区是从数据库中提取的,我不知道是否将添加或减少秒。那就是我试图找出如何保存时区的问题,以便我可以计算出偏移量。



我可以将时区保存为-14400 ,但是使用上面的例子,我不能仅仅组合两个字符串来为我做数学,使用 - sign:

  $ my_time = $ utc_time。$ timezone; 

所以...如何正确保存时区以完成数学?

解决方案

一个位置的时区并不总是相同的 - 例如,英国在3月和10月之间是BST(GMT + 1)。使用PHP支持的时区之一:



http://php.net/manual/en/timezones.php



如果您使用数字进行操作,请将其存储为小时或分钟。将UTC / GMT以西的时区存储为负数。例如,美国东海岸将为 -5 (小时)或 -300 (分钟) - 假设为5小时后。



然后,将其添加到时间戳 - 负数或正数将处理其余的。



<$ p
$ now = time()+($ offset * 60 * 60); $ p $ //存储时间为5小时
//存储时间为5分钟(-300)
$ now = time()+($ offset * 60);


Possible Duplicate:
Datatype/structure to store timezones in MySQL

I'll be storing only UTC times in my database, and want to convert the timestamps to a user's local time using PHP.

My question is, what is the best way to store the users timezone in the database?

IE: -400, -4, -4:00

With that said, I know how to do the math manually:

$utc_time = strtotime("2011-10-02 23:00:00");
$my_time = $utc_time - 14400;

echo date($timeformat, $mytime);

My problem is, with the user's timezone being pulled from the database, I don't know whether I'll be adding or subtracting seconds. That's where I run into the problem of trying to figure out how to save the timezone so that I can calculate the offset.

I can save the timezone as "-14400", but using the above example, I can't just combine the two strings to do the math for me using the - sign:

$my_time = $utc_time.$timezone;

So... how do I save the timezone properly to get the math done?

解决方案

The timezone in one location isn't always the same - for example, the UK is BST (GMT + 1) between March and October. Use one of the timezones supported by PHP:

http://php.net/manual/en/timezones.php

If you do go ahead using numbers, either store them as hours or minutes. Store timezones west of UTC/GMT as negative numbers. For example, the US East Coast would be -5 (hours) or -300 (minutes) - assuming it's 5 hours behind.

Then, add this to the timestamp - the negative or positive will handle the rest.

// for 5 hours behind when stored as hours (-5)
$now = time() + ($offset * 60 * 60);
// for 5 hours behind when stored as minutes (-300)
$now = time() + ($offset * 60);

这篇关于如何在mysql中存储用户时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆