Last active
August 29, 2015 14:01
-
-
Save furkanmustafa/8710c43ba8299ca10a32 to your computer and use it in GitHub Desktop.
Easy push/pop default timezone stack for php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Timezone Stack https://gist.github.com/furkanmustafa/8710c43ba8299ca10a32 | |
class TimezoneStack { | |
public static $stack = []; | |
static function Init() { | |
self::$stack[] = @date_default_timezone_get(); | |
} | |
static function Push($timezone) { | |
if (!date_default_timezone_set($timezone)) | |
return false; | |
self::$stack[] = $timezone; | |
return true; | |
} | |
static function Pop() { | |
if (count(self::$stack) == 1) | |
return false; | |
$timezone = array_pop(self::$stack); | |
date_default_timezone_set($timezone); | |
return $timezone; | |
} | |
static function Using($timezone, $function) { | |
self::Push($timezone); | |
$function(); | |
self::Pop(); | |
} | |
} | |
// Alias functions, just to make similar to date_default_timezone_set | |
function date_default_timezone_push($timezone_identifier) { | |
return TimezoneStack::Push($timezone_identifier); | |
} | |
function date_default_timezone_pop() { | |
return TimezoneStack::Pop(); | |
} | |
// Example: | |
// | |
// TimezoneStack::Using('Asia/Tokyo', function() use ($myObject) { | |
// $myObject->date = strtotime($formdata['date']); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment