Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.