1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to set Timezone in my Controller

Discussion in 'XenForo Development Discussions' started by KenSmith, Aug 28, 2013.

  1. KenSmith

    KenSmith Active Member

    In my custom controller, I see that date_default_timezone_get() returns 'UTC'.
    Php.ini and my user preferences are both set to 'America/Chicago' instead.

    Do I need to make some call to setup the current visitor, and will this set their preferred timezone?
     
  2. KenSmith

    KenSmith Active Member

    OK, I figured this out...

    I was previously using PHP's date('Y-m-d') to get today's date.
    Because XenForo_Application set the date_default_timezone to 'UTC', calls to date() will return UTC versions.

    Here's how to get today's date string in the correct timezone:
    XenForo_Locale::date(XenForo_Application::$time,'picker')
     
  3. oVR

    oVR Active Member

    I a total noob. Can you explain this to me as if I was a 5 year old? I'm having a timezone issue and I think this will fix it but IDK what to do.
     
  4. KenSmith

    KenSmith Active Member

    Are you writing an add-on? What kind of issue are you having with timezones?

    To be clear about what I was talking about above, I was developing an add-on that needed to use today's date in a database query.
    I initially did this using the PHP function "date", but that didn't respect the visitor's preferred timezone because of the way XenForo_Application sets the PHP timezone to 'UTC'.
    Instead, anyone developing a XenForo add-on that needs to access the current date should use the 'date' function in XenForo_Locale.
    This will use the visitor's preferred timezone to decide which day it is.
     

Share This Page