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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Clarity
      Hello everyone!
      I'm new here and I want to set an user avatar to my profile. However, I don't see any option in my personal cabinet that can do it. Could you please tell me if there are there some requirements for user for setting an avatar or I'm missing something?
    • By rjgamer
      Hi guys,
      the field "redirect_last" of type DateTime got not updated. The update on the field "redirect_counter" works and got saved.
      Does anybody know what I did wrong in my code?
      if ($input->urlSegment(1) === 'redirect') { $page->of(false); $page->redirect_last = time(); $page->redirect_counter += 1; if ($page->save('redirect_counter')) { $session->redirect($page->website_url, 302); } } Thanks.
    • By theoretic
      Hi there! And thanks for Processwire!
      I've got an interesting case concerning access to current user page. It appears that PW somehow limits access to the frontend page of current user.
      I'm speaking about a specific PW configuration. We have two kind of users: 'regular' users with native user template and member users with specific member template and specific members parent page (by the way, it's so cool that PW allows to use custom user templates and custom parent for certain user pages!). So a member with name Joe has a page with member template and url like /members/joe .
      The members template has some access limitations: only member users can see pages with member template. It works like a charm in most situations. For example, user Bill (who has member template and is logged in) can browse a page with url /members/ann which also is a member page with member template.
      And now, meet the glitch! The above-mentioned Bill cannot get to his own page /members/bill ! PW generates 404 page instead.
      I see no reason for this behavior. From my point of view any member should have access to any member page in this situation. What am i doing wrong? Any advice is welcome!
    • By VeiJari
      Hello,
      I'm trying to create a page via api and populate values to it. I can populate everything except user pages to a page reference array.
      Code: 
      $dataUsers = $data->project->users; foreach($dataUsers as $dataUser) { $newProject->projectUsers->add(wire()->pages->find('template=user, id=' . $dataUser->id)); } I'm receiving my data via JSON.
      Is there something I'm missing?
      Thanks for help
    • By neonwired
      I'm hoping someone has seen this before. There doesn't appear to be an issue with the user info.

       

×
×
  • Create New...