Nov 30, 2014

Convert Date Time to GMT/UTC

If you want to convert any date time to GMT/UTC for given timezone (observing DST/not).

/**
     * coverts the given date time to GMT/UTC based on timezone provided
     * @param string $date
     * @param string $timezone
     * @return string
     */
    function getGMT($date, $timezone){
        date_default_timezone_set("UTC");
        $daylight_savings_offset_in_seconds = timezone_offset_get( timezone_open( $timezone ), new DateTime() );
        return $new_date = date('Y-m-d H:i:s', strtotime('-'.$daylight_savings_offset_in_seconds.' seconds', strtotime($date)));
    }



$date = "2014-11-30 23:50:00"; //yyyy-mm-dd
//$date = "11/30/2014 23:50:00"; //mm/dd/yyyy
//$date = "30-11-2014 23:50:00"; //dd-mm-yyyy
$timezone = "Asia/Kolkata";
//$timezone = "Australia/Adelaide";
//$timezone = "America/New_York";

echo getGMT($date, $timezone);

No comments:

Post a Comment

Want to tell something about this post. Please feel free to write...