This topic is locked

Time Zone issues

3/23/2011 10:17:29 PM
PHPRunner General questions
C
chrisclements author

Hi guys. I need some help with how to display times according to the time zone my users are in. I actually have 2 issues. The first is displaying the correct time and the other is saving the correct time into the database.
All the times in my database are GMT. I have also stored a timezone field in my user login table so that I can set the timezone for that user once I can figure out how to do it.
I also have ADD tables that include times that the user enters, they obviously do that based on the timezone they are in, so I need to convert those to GMT so that I can store them in the database correctly.
Any and all help on this issue would be appreciated.
Thanks,

Chris

J
Jane 3/24/2011

Hi,
please see my answers below:

  1. to convert GMT time to correct time for each user use Customoption on the "View as" settings dialog on the Visual Editor tab.

    Use PHP date() and mktime() functios to convert dates:

    http://php.net/manual/en/function.date.php

    http://php.net/manual/en/function.mktime.php
  2. to convert values entered on the add/edit pages use Before record added/Before record updated events on the Events tab. Actual values are in the $values array ($values["FieldName"]).

C
chrisclements author 3/27/2011

Can you please give me a specific example of the code please.



Hi,
please see my answers below:

  1. to convert GMT time to correct time for each user use Customoption on the "View as" settings dialog on the Visual Editor tab.

    Use PHP date() and mktime() functios to convert dates:

    http://php.net/manual/en/function.date.php

    http://php.net/manual/en/function.mktime.php
  2. to convert values entered on the add/edit pages use Before record added/Before record updated events on the Events tab. Actual values are in the $values array ($values["FieldName"]).

Admin 3/28/2011

Here is the sample code that takes GMT datetime as a source, calculates the offset between GMT and selected timezone and prints adjusted datetime value.
The actual code depends on how you store datetime (human readable or timestamp) and timezone (shift in seconds/hours or timezone name).

$time='2009-03-21 13:14';

$userTimezone = new DateTimeZone('America/New_York');

$gmtTimezone = new DateTimeZone('GMT');

$myDateTime = new DateTime($time, $gmtTimezone);

$offset = $userTimezone->getOffset($myDateTime);
echo date("Y-m-d h:i" , strtotime($time)+$offset);