Jump to content

Save date / time in user timezone


gebeer
 Share

Recommended Posts

Hi there,

can't seem to get my head around handling user timezones the right way and hope to get some feedback from people more knowledgeable than me.

My scenario:

Users can set publishing date/time for pages they create through a frontend form. These should be saved in the users' timezone.

I have a date field for publishing time. PW saves dates as unix timestamps to the DB. For conversion of the date/time to the timestamp PW uses the default server time zone setting (in my case Europe/Berln).

Now when a user in timezone America/New_York creates a page and sets the publishing date, it will be converted to a timestamp using the server timezone Europe/Berlin and saved to the DB.

Later the user checks if his page is being published at the set time in New York. But it will be published at the wrong time because the user is in a different timezone.

I assume it would be best to convert the publishing date/time to a timestamp using the user timezone and save that to the DB. Am I right here?

How would I accomplish that?

I found date_default_timezone_set. Can I simply use this to set the user timezone before I save values, like

date_default_timezone_set($userTimezone); // where $userTimezone is a string like "America/New_York"

$editpage->of(false);
foreach($adform as $field) { // loop through all fields and save them
    if(in_array($field->name, $ignorefields)) continue;
    $editpage->set($field->name, $field->value);
}
$editpage->of(true);

date_default_timezone_set($config->timezone); // do I have to set it back to server timezone here?

Link to comment
Share on other sites

I can set the user timezone with date_default_timezone_set.

It is important to set it before my form gets rendered, submitted and processed, not just before I save values.

The current time in my date field gets calculated based on the set time zone.

Example:

set timezone Europe/Berlin, current date

post-1920-0-30995300-1414134082_thumb.pn

set timezone America/New_York, current date

post-1920-0-05464300-1414134121_thumb.pn

When PW saves publishing timestamps, I get timestamps for 2014-10-24 03:00

Europe/Berlin: 1414112400

America/New_York: 1414134000

Which is 21600 seconds or 6 hours off. Which is correct.

So I think I'm on the right track now handling different timezones.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...