Skip to content

Instantly share code, notes, and snippets.

@furkanmustafa
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furkanmustafa/8710c43ba8299ca10a32 to your computer and use it in GitHub Desktop.
Save furkanmustafa/8710c43ba8299ca10a32 to your computer and use it in GitHub Desktop.
Easy push/pop default timezone stack for php
<?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