PHP function to Convert Timezone Name to Offset timezoneToOffset()

PHP Function to convert timezone name to timezone offset using DateTime Php Class. 


These are sample converted timezones by this function

America/Caracas     -04:30
America/St_Johns     -02:30
Asia/Calcutta     +05:30
Asia/Colombo     +05:30
Asia/Kabul     +04:30
Asia/Kathmandu     +05:45
Asia/Katmandu     +05:45
Asia/Kolkata     +05:30
Asia/Rangoon     +06:30
Asia/Tehran     +04:30
Australia/Adelaide     +09:30
Australia/Broken_Hill     +09:30
Australia/Darwin     +09:30
Australia/Eucla     +08:45
Australia/LHI     +10:30
Australia/Lord_Howe     +10:30
Australia/North     +09:30
Australia/South     +09:30
Australia/Yancowinna     +09:30
Canada/Newfoundland     -02:30
Indian/Cocos     +06:30
Iran     +04:30
NZ-CHAT     +12:45
Pacific/Chatham     +12:45
Pacific/Marquesas     -09:30
Pacific/Norfolk     +11:30

Source Code:

<?php
/*
* Convert Timezone Name to Offset
*
*/

function timezoneToOffset($timezone = 'UTC'){
    $time = new DateTime('now', new DateTimeZone($timezone));
    return $time->format('P');
}


Output / Usage:

<?php 

echo timezoneToOffset('Asia/kolkata');

// returns +5:30

Comments: (Your feedback is valuable to us)

Donald Trump 9 months ago Reply

You're gonna make php great again!


Donald Trump 9 months ago Reply

You're gonna make php great again!