简体   繁体   English

CodeIgniter到GMT的本地时区

[英]CodeIgniter local timezone to GMT

i need help with multi timezone website in codeigniter. 我在codeigniter中需要多时区网站的帮助。

config.php: config.php文件:

$config['time_reference'] = 'gmt';
date_default_timezone_set('UTC');

now when user want to see event(website is for events) it shows dates to his timezone with gmt_to_local() function, this works fine.. 现在当用户想要查看事件(网站是用于事件)时,它使用gmt_to_local()函数向他的时区显示日期,这很好..

Now problem is when user want to add new event, he picks date and time, now it gets converted to timestamp: 现在问题是当用户想要添加新事件时,他选择日期和时间,现在它被转换为时间戳:

strtotime($event_date);

And problem is that he picks date and time for event on his timezone, now i need to convert it to gmt, and save to db. 问题是他在他的时区选择事件的日期和时间,现在我需要将其转换为gmt,并保存到db。

Example, if user is in UP2(+2:00) and he set 12-25-2012 22:30:00 it will be converted to timestamp and saved in db, but it is incorrect, it should subtract 2:00 and save that timestamp to db as timestamp of 12-25-2012 20:30:00 (this is converted to gmt) 例如,如果用户在UP2(+2:00)并且他设置为12-25-2012 22:30:00它将被转换为时间戳并保存在db中,但是它不正确,它应该减去2:00并保存该时间戳为db作为时间戳12-25-2012 20:30:00 (这被转换为gmt)

Hope that you will understand. 希望你能理解。

Any solution for this? 对此有何解决方案? Thanks. 谢谢。

In your event form, you should send to the server a hidden input witch will contain client's timezone offset. 在您的事件表单中,您应该向服务器发送一个隐藏的输入,其中包含客户端的时区偏移量。 You can get it via Javascript's Date.getTimezoneOffset() method (it gives you offset in minutes between UTC and users's local time). 您可以通过Javascript的Date.getTimezoneOffset()方法获取它(它为您提供UTC与用户当地时间之间的分钟偏移量)。

If you can't do that, you can save user's local timezone into db or ask it in your event form. 如果您不能这样做,您可以将用户的本地时区保存到数据库中或在事件表单中询问它。

Then, in your INSERT or UPDATE query, you can use DATE_ADD('2012-12-13 19:41:00', [user offset] MINUTE) to convert timestamp. 然后,在INSERT或UPDATE查询中,您可以使用DATE_ADD('2012-12-13 19:41:00',[用户偏移] MINUTE)来转换时间戳。 You can also do it in php, or client-side when submitting form... 您也可以在提交表单时在php或客户端执行此操作...

For example in PHP : 例如在PHP中:

strtotime($event_date);

gives you the date's Unix timestamp (so, in seconds). 为您提供日期的Unix时间戳(以秒为单位)。 To convert it into another timezone, according you get the offset in minutes with javascript method, you can do : 要将其转换为另一个时区,根据您使用javascript方法获得偏差(以分钟为单位),您可以执行以下操作:

$dbTimestamp = strtotime($event_date) + $_POST['offsetInMinutes']/60;

If you get the offset in this form : '+2:00' or '-6:00', you can get offset in hours using a regular expression : 如果你得到这种形式的偏移:'+2:00'或'-6:00',你可以使用正则表达式以小时为单位获得偏移量:

preg_match('/(.*)\:00/', '+2:00', $match)

will set '+2' into $match[1] 将'+2'设置为$ match [1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM