PHP function for Time Zone conversion

I recently found that PHP doesn’t have an inbuilt function for conversion of time/datebetween multiple time zones and neither could I find a  third-party function for the purpose.  However, I did find the PEAR class which has inbuilt support for multiple time-zones but it cannot be used by “including”  itself in the php page. PEAR class can be used only after installation, which may not be feasible in each and every case.
To avoid the hassles of installation I have written a set of 3 functions in PHP which wil allows you to –

1.  Convert GMT to local time zone 2. Convert local time zone to GMT

3.  Convert between two different time zones.

/**
 *Convert the time in GMT timestamp into  user's local time zone timestamp
 * @param time $gmttime
 * @param string $timezoneRequired
 * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
 * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
 * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
 * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format
 *By: Subhranil Dalal , Mindfire Solutions                            
*/
 

function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
    $system_timezone = date_default_timezone_get();

    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d h:i:s A");

    $local_timezone = $timezoneRequired;
    date_default_timezone_set($local_timezone);
    $local = date("Y-m-d h:i:s A");

    date_default_timezone_set($system_timezone);
    $diff = (strtotime($local) - strtotime($gmt));

    $date = new DateTime($gmttime);
    $date->modify("+$diff seconds");
    $timestamp = $date->format("m-d-Y H:i:s");
    return $timestamp;
}
 
/**
*Use: ConvertGMTToLocalTimezone('2009-02-05 11:54:00.000','Asia/Calcutta');
*Output: 02-05-2009 17:24:00 || IST = GMT+5.5
*/
 /**
 *Convert the time in user's local time zone timestamp into GMT timestamp  
 * @param time $gmttime
 * @param string $timezoneRequired
 * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
 * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
 * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
 * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format 
 *By: Subhranil Dalal , Mindfire Solutions                             Date:06/02/2009
*/

function ConvertLocalTimezoneToGMT($gmttime,$timezoneRequired)
{
    $system_timezone = date_default_timezone_get();
 
    $local_timezone = $timezoneRequired;
    date_default_timezone_set($local_timezone);
    $local = date("Y-m-d h:i:s A");
 
    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d h:i:s A");
 
    date_default_timezone_set($system_timezone);
    $diff = (strtotime($gmt) - strtotime($local));
 
    $date = new DateTime($gmttime);
    $date->modify("+$diff seconds");
    $timestamp = $date->format("m-d-Y H:i:s");
    return $timestamp;
}
 
/**
*Use: ConvertLocalTimezoneToGMT('2009-02-05 17:24:00.000','Asia/Calcutta');
*Output: 02-05-2009 11:54:00 ||  GMT = IST-5.5
*/
 
 
 
 
/**
 *Convert the time in user's local time zone timestamp into another time zone timestamp
 * @param time $gmttime
 * @param string $timezoneRequired
 * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
 * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
 * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
 * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format 
 *By: Subhranil Dalal , Mindfire Solutions                             Date:06/02/2009.
*/


function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
{
    $system_timezone = date_default_timezone_get();
    $local_timezone = $currentTimezone;
    date_default_timezone_set($local_timezone);
    $local = date("Y-m-d h:i:s A");
 
    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d h:i:s A");
 
    $require_timezone = $timezoneRequired;
    date_default_timezone_set($require_timezone);
    $required = date("Y-m-d h:i:s A");
 
    date_default_timezone_set($system_timezone);

    $diff1 = (strtotime($gmt) - strtotime($local));
    $diff2 = (strtotime($required) - strtotime($gmt));

    $date = new DateTime($time);
    $date->modify("+$diff1 seconds");
    $date->modify("+$diff2 seconds");
    $timestamp = $date->format("m-d-Y H:i:s");
    return $timestamp;
}
 
 
 
/**
*Use: ConvertLocalTimezoneToGMT('2009-02-05 17:24:00.000','Asia/Calcutta','America/Chicago');
*Output: 02-05-2009 05:54:00 ||  IST = GMT+5.5, CST = GMT-6 || IST - CST = 11.5
*/
150 150 Burnignorance | Where Minds Meet And Sparks Fly!