note 117130 added to function.date-default-timezone-set
| From: | Jesse dot Chisholm at gmail dot com | Date: | Sun, 19 Apr 2015 00:10:40 +0000 |
| Subject: | note 117130 added to function.date-default-timezone-set | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202298@lists.php.net to get a copy of this message | ||
I found a need to change the timezone based on a DB record, so it would display properly for each
record. So I wrapped some of the other posts into this small class:
<?php
class TZStack
{
private $stack = array();
public function __construct(){}
public function push($newTZ)
{
array_push($this->stack, date_default_timezone_get());
if (isset($newTZ) && ("" != $newTZ))
{
date_default_timezone_set($newTZ);
}
}
public function pop()
{
if (count($this->stack) > 0)
{
date_default_timezone_set(array_pop($this->stack));
}
}
}
// the global TZ stack for the rest of the code to use.
$tzStack = new TZStack();
// In case I need to know the TZ at the web server. :)
$script_tz = date_default_timezone_get();
?>
Then in my per-record code:
<?php
foreach($records as $item)
{
$tzStack->push($item['TZ']);
$TZ = date("T", time());
// code that displays this record, using $TZ.
$tzStack->pop();
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 2602:306:bda3:e040:9cef:73d6:4f1e:7fc4
----
Manual Page -- http://php.net/manual/en/function.date-default-timezone-set.php
Edit -- https://master.php.net/note/edit/117130
Del: integrated -- https://master.php.net/note/delete/117130/integrated
Del: useless -- https://master.php.net/note/delete/117130/useless
Del: bad code -- https://master.php.net/note/delete/117130/bad+code
Del: spam -- https://master.php.net/note/delete/117130/spam
Del: non-english -- https://master.php.net/note/delete/117130/non-english
Del: in docs -- https://master.php.net/note/delete/117130/in+docs
Del: other reasons-- https://master.php.net/note/delete/117130
Reject -- https://master.php.net/note/reject/117130
Search -- https://master.php.net/manage/user-notes.php