Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Created May 16, 2016 23:19
Show Gist options
  • Save SmugZombie/b5dd3e61dc9a68128b082639f84a4f6b to your computer and use it in GitHub Desktop.
Save SmugZombie/b5dd3e61dc9a68128b082639f84a4f6b to your computer and use it in GitHub Desktop.
// Converts a UTC timezone to a GMT - or + timezone - github.com/smugzombie
// Accepts a timestamp like this: Y-m-d H:i:s
// And a timezone like this: (0,1,8,-8,-12)
// Example $time_out = convertTimestamp("2016-05-16 20:13:44","-7"); // 2016-05-16 21:13:44
function convertTimestamp($time_in, $time_zone){
$time_zone = floatval($time_zone); //Ensure Timezone is a number, if not replace with 0 (UTC)
if(strpos($time_zone, "-") === false){$modifier = "+";}else{ $modifier = "";} //See if modifier is needed, add + if positive
$time_out = date('Y-m-d H:i:s',strtotime("$time_in $modifier$time_zone hour")); // Convert
$tz = "GMT $modifier$time_zone";
if($time_zone == "0.0" || $time_zone == "0"){ $tz = "UTC"; }
return $time_out." ($tz)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment