• 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

KenSmith

Active member
#1
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?
 

KenSmith

Active member
#2
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')
 

oVR

Active member
#3
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.
 

KenSmith

Active member
#4
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.