문제

i need help with multi timezone website in codeigniter.

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..

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.

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)

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).

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. You can also do it in php, or client-side when submitting form...

For example in PHP :

strtotime($event_date);

gives you the date's Unix timestamp (so, in seconds). To convert it into another timezone, according you get the offset in minutes with javascript method, you can do :

$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 :

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

will set '+2' into $match[1]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top