Jump to content

Brendan Kidwell

Members
  • Posts

    3
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Brendan Kidwell's Achievements

Newbie

Newbie (2/6)

0

Reputation

  1. Correct. My code is wrong. I was searching for this answer today and didn't even remember I'd written this. I have revised my answer just now: // Add these lines to your config.php file. // Tell PHP to use this timezone as the default when a timezone API call doesn't specify a zone. // Change to your local time zone name: https://www.php.net/manual/en/timezones.php date_default_timezone_set('America/New_York'); // Initialize database connection with session time zone based on PHP's time zone. $config->dbInitCommand = (function() { $charset = 'utf8'; // What is the timezone offset (in minutes) from UTC for the current time in the current timezone? $minutes = (new \DateTime())->getOffset() / 60; $sign = ($minutes >= 0) ? '+' : '-'; $h = str_pad(intdiv(abs($minutes), 60), 2, '0', STR_PAD_LEFT); $m = str_pad(abs($minutes) % 60, 2, '0', STR_PAD_LEFT); $dbTimeZone = "{$sign}{$h}:{$m}"; return "SET NAMES '{$charset}', time_zone = '{$dbTimeZone}'"; })(); // Tell ProcessWire to use this timezone. // (You'll find this in your config file already under the comment "Installer: Time zone setting".) $config->timezone = 'America/New_York';
  2. This works better for me, and accounts for daylight savings time switches -- it looks at PHP's timezone offset for the current date and time in the current timezone: // Add these lines to your config.php file. // Tell PHP to use this timezone as the default when a timezone API call doesn't specify a zone. // Change to your local time zone name: https://www.php.net/manual/en/timezones.php date_default_timezone_set('America/New_York'); // Initialize database connection with session time zone based on PHP's time zone. $dbTimeZone = (function() { // What is the timezone offset (in minutes) from UTC for the current time in the current timezone? $minutes = (new \DateTime())->getOffset() / 60; $sign = ($minutes >= 0) ? '+' : ''; $h = str_pad(intdiv($minutes, 60), 2, '0'); $m = str_pad($minutes % 60, 2, '0'); return $sign . $h . ':' . $m; })(); $config->dbInitCommand = "SET NAMES '{charset}', time_zone = '$dbTimeZone' "; // Tell ProcessWire to use this timezone. // (You'll find this in your config file already under the comment "Installer: Time zone setting".) $config->timezone = 'America/New_York'; I checked it and I see that the actual timestamps being recorded in the database are in UTC timezone, as they should be. They get formatted with the correct LOCAL timezone offset when displayed in the admin screens.
×
×
  • Create New...