inveritas Posted March 2, 2019 Share Posted March 2, 2019 I'm working on a project that has a lot of different timezone options, and all I have from the users is their UTC offset. After weeks of doing tweaks, I come to new issues every time I add something, one would think it would be a lot easier to just get the correct time and day :) The latest issue is that when I try to rewrite a date to a better fomat, it disregards the GMT offset. Like this: 4/14/19, 12:00 PM GMT+2 (=$date) becomes ... 14-04-2019 10:00 when using this code: $bp_date_to_format = date_create_from_format('Y-m-d\TH:i:sP', $date); echo date_format($bp_date_to_format, 'd-m-Y H:i'); I figured it would just rewrite the format, keeping the exact time. It isn't. We do have all these sorts of localization functions as well, but because there doesn't seem to be one single standard (each 3rd party API connection we have uses its own default datetime format so it seems). // Remove UTC Text $UTC_offset = str_replace('UTC', '', $timezone); // Get Offset in Minutes if (stripos($UTC_offset, ':') !== false) { // Calculate seconds from offset list($hours, $minutes) = explode(':', $offset); $seconds = $hours * 60 * 60 + $minutes * 60; } else { $seconds = $UTC_offset * 60 * 60; } // Get User timezone name from seconds $timezone = timezone_name_from_abbr('', $seconds, 1); if ($timezone === false) { $timezone = timezone_name_from_abbr('', $seconds, 0); } // Set new TZ return date_default_timezone_set($timezone); Isn't there any "one way"solution that can be used? It's confusing to say the least. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/308415-date-time-and-timezone-based-code-issues/ Share on other sites More sharing options...
requinix Posted March 2, 2019 Share Posted March 2, 2019 It's possible you're not aware that PHP has its own default timezone, and if you try to format a date (timestamp) then it'll format according to the PHP timezone. $bp_date_to_format = date_create_from_format('Y-m-d\TH:i:sP', $date); echo date_format($bp_date_to_format, 'd-m-Y H:i'); On the first line PHP will parse the date as you'd expect, and come up with a time that is equivalent to 12pm GMT+2. On the second line you will format it, and since PHP is using the UTC timezone (also the default) it will give you 10pm GMT+0. First of all, a GMT offset and a timezone are not necessarily the same thing. Remember daylight savings? A timezone like Asia/Beirut will be GMT+2 in half of the year and GMT+3 in the other half. If people can choose their timezone according to location (as opposed to choosing by offset) then you need to stick with that everywhere so you can handle DST changes automatically. PHP has an API for dates. It's DateTime and related classes. (Or the procedural versions of the same class methods, but the OOP style is recommended.) On the server side you have two options: 1. Always deal with dates in UTC, and convert to/from the user's timezone when you want to show/get a date 2. Set PHP's default timezone to be the user's timezone, and if you need to work timezone-less dates (eg, MySQL's DATETIME) then you convert to/from UTC. Quote Link to comment https://forums.phpfreaks.com/topic/308415-date-time-and-timezone-based-code-issues/#findComment-1564984 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.