note 113864 added to function.date-default-timezone-set
| From: | chris at clwill dot com | Date: | Tue, 10 Dec 2013 23:54:27 +0000 |
| Subject: | note 113864 added to function.date-default-timezone-set | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197901@lists.php.net to get a copy of this message | ||
I'm sure I'm not the only one who is distressed by the recent default behavior change to
E_NOTICE when the timezone isn't explicitly set in the program or in .ini. I insure that the
clock on the server IS correct, and I don't want to have to set it in two places (the system
AND PHP). So I want to read it from the system. But PHP won't accept that answer, and insists
on a call to this function. So, here's my answer:
<?php
function setTimezone($default) {
$timezone = "";
// On many systems (Mac, for instance) "/etc/localtime" is a symlink
// to the file with the timezone info
if (is_link("/etc/localtime")) {
// If it is, that file's name is actually the "Olsen" format timezone
$filename = readlink("/etc/localtime");
$pos = strpos($filename, "zoneinfo");
if ($pos) {
// When it is, it's in the "/usr/share/zoneinfo/" folder
$timezone = substr($filename, $pos + strlen("zoneinfo/"));
} else {
// If not, bail
$timezone = $default;
}
}
else {
// On other systems, like Ubuntu, there's file with the Olsen time
// right inside it.
$timezone = file_get_contents("/etc/timezone");
if (!strlen($timezone)) {
$timezone = $default;
}
}
date_default_timezone_set($timezone);
}
?>
Use it by calling it with a fallback default answer.
Yes, I know it doesn't work on Windows. Neither do I :) Perhaps someone wants to add that
functionality.
Hope this helps someone.
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.date-default-timezone-set.php
Edit -- https://master.php.net/note/edit/113864
Del: integrated -- https://master.php.net/note/delete/113864/integrated
Del: useless -- https://master.php.net/note/delete/113864/useless
Del: bad code -- https://master.php.net/note/delete/113864/bad+code
Del: spam -- https://master.php.net/note/delete/113864/spam
Del: non-english -- https://master.php.net/note/delete/113864/non-english
Del: in docs -- https://master.php.net/note/delete/113864/in+docs
Del: other reasons-- https://master.php.net/note/delete/113864
Reject -- https://master.php.net/note/reject/113864
Search -- https://master.php.net/manage/user-notes.php