1

I am trying to get the timezone format to just show -6 rather than -06:00 but can't figure this out.

My current code outputs the format (since I am in central time) as -21600

SO I am trying to convert this using the following...

$time_offset_get = new \DateTime('now', new DateTimeZone(America/Chicago));
$conn_timezone_offset = $time_offset_get->format('P');

But of course, this ($conn_timezone_offset) is giving me -06:00 Is there another format code that will do this correctly or am I am doing this wrong altogether.

Overall, I am just trying to convert a timezone offset name like America/Chicago or a 3 digit timezone code into a number offset without the colon using PHP.

user2284703
  • 367
  • 3
  • 15
  • `America/Chicago` *cannot* be represented by a number, because sometimes it's -6, and sometimes it's -5. Please read [the timezone tag wiki](https://stackoverflow.com/tags/timezone/info), especially the part titled "Time Zone != Offset". – Matt Johnson-Pint Nov 23 '17 at 23:11
  • I know this... I have additional settings that will add or subtract based on whether it is daylight savings. I'm just trying to get the initial timezone in number digits only. – user2284703 Nov 23 '17 at 23:39
  • Daylight savings is quite different in different time zones, and at different times in history even within the same time zone. That's the whole point of keeping time zones as an identifier rather than a number. – Matt Johnson-Pint Nov 23 '17 at 23:41
  • So what do you suggest? I need the simple version in order to make the javascript clock work properly which requires an offset such as zone = -6; – user2284703 Nov 23 '17 at 23:48
  • If it helps any, I can also accept an answer in javascript or jquery as it would just require a few coding changes on my part first, – user2284703 Nov 23 '17 at 23:49
  • If you just need the client's local time, you send the UTC time to the client, and let the client convert it to local time using a `Date` object. If you need a *specific* time zone, see https://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone – Matt Johnson-Pint Nov 23 '17 at 23:52
  • unfortunately, my objective isn't clear. The user will have the ability to view time in alternate time zones. So simply setting it to display the local time will not work. But thanks for the link. That will come in helpful as well. – user2284703 Nov 23 '17 at 23:55
  • I maintain moment-timezone. I think you'll find that to be essential in achieving the stated goal i you're going to do it client-side. You could do it in pure PHP also, but you won't be able to avoid using full time zone identifiers. – Matt Johnson-Pint Nov 23 '17 at 23:58

1 Answers1

1

Use DateTime::getOffset

Returns the timezone offset in seconds from UTC on success or FALSE on failure.

To get the offset in hours, simply divide the value by 3600.

$now = new \DateTime('now', new DateTimeZone('America/Chicago'));
$offset = $now->getOffset();
if ($offset !== false) {
    $offset = round($offset / 3600);
} else {
    // the error handling here
}
camelsWriteInCamelCase
  • 1,655
  • 1
  • 10
  • 15