This topic is locked
[SOLVED]

 TImezone per user

9/21/2017 1:26:04 AM
PHPRunner General questions
H
headingwest author

Hi All,
Does anyone have some tips for timezones per user? Example: user adds a record and they need to see what date/time it was added for their timezone. So at the same moment it could be 10:00am or 10:00pm depending on your timezone. I need the user to see their own time.
The function date_default_timezone_set() applies across all sessions from the testing I've done, so you can't add it in "after login" for that user/session.
If I save UTC (+00:00 zone) in the MySQL database then every date/time field needs to be adjusted for the timezone when the user views the record. That would mean a "view as" custom field to convert from UTC to local time.
I've downloaded the zones/times and added to mysql from here: https://timezonedb.com/download - this allows me to add a timezone dropdown to the user record.
I'm not worried about daylight savings adjustments, an hour is OK.
Any ideas appreciated. Thanks.

admin 9/21/2017

It will work if you save timezone for each customer in login table and apply using date_default_timezone_set() function in AfterAppInit event.

H
headingwest author 9/26/2017



It will work if you save timezone for each customer in login table and apply using date_default_timezone_set() function in AfterAppInit event.


Thanks Sergey, seems to work. A couple of tips for those working with local time in PHP:

  • Most developers recommend using UTC time then adjusting for each user display, but this didn't work well for me as I only want local time
  • Some PHP functions eg time() and date() only get the UTC time, not the local time regardless of date_default_timezone_set() - so instead use PHPRunner now() which gets local time
  • to do time maths I'm currently testing strtotime() - eg:

//to add "timeahead" hours to "DueDate"

$ins['duedate'] = strtotime(now()) + ($data['timeahead']*60*60);
//or to add "daysahead"

$ins['duedate'] = strtotime(now()) + ($data['daysahead']*60*60*24);


Both seem to be working but proper testing is in a month or so - let me know if this fails.