php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55527 DateTime::__construct throws Exception even when timezone is specified
Submitted: 2011-08-28 22:23 UTC Modified: 2015-08-24 15:25 UTC
Votes:4
Avg. Score:3.5 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: john at zerocrates dot org Assigned: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.8 OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: john at zerocrates dot org
New email:
PHP Version: OS:

 

 [2011-08-28 22:23 UTC] john at zerocrates dot org
Description:
------------
The DateTime constructor throws an Exception warning that it is not safe to rely 
on the system's timezone settings if you try to construct a DateTime object and 
haven't somehow specified a timezone, whether by using date_default_timezone_set, 
the date.timezone setting, or the $timezone argument of DateTime itself.

However, there is another way to specify the timezone for a DateTime object, by 
passing a time string which includes a timezone as the first argument to the 
constructor. When such a string is given, the $timezone argument is ignored and 
the timezone given in the string is always used.

However, this doesn't prevent the Exception from being thrown, even though a 
timezone is being specified. To avoid the Exception, you have to have a default 
timezone set, or you have to pass a DateTimeZone as the second argument, even 
though neither would actually be used for creating the object.

Test script:
---------------
<?php

ini_set('date.timezone', NULL);

try {
    $date = new DateTime('2011-08-28T00:00:00+0000');
    echo $date->format('O');
} catch (Exception $e) {
    echo 'Exception thrown.';
}

echo PHP_EOL;

date_default_timezone_set('America/New_York');
try {
    $date = new DateTime('2011-08-28T00:00:00+0000');
    echo $date->format('O');
} catch (Exception $e) {
    echo 'Exception thrown.';
}

Expected result:
----------------
+0000
+0000

Actual result:
--------------
Exception thrown.
+0000

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-10 00:22 UTC] john at zerocrates dot org
-Operating System: Linux +Operating System: All
 [2011-10-10 00:22 UTC] john at zerocrates dot org
Switching "OS" field to "All", this is OS-independent.
 [2014-10-02 08:17 UTC] headsup at hotmail dot com
After a few hundred lines of flawless code, whenever using the method POST (not GET) the function error_get_last() catches the following, despite of setting the timezone in the first line of the code with date_default_timezone_set('Europe/Lisbon');

Array
(
    [type] => 2
    [message] => Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for '1.0/DST' instead
    [file] => Unknown
    [line] => 0
)


Curiously, at the end of the script, after printing the array above, printing date_default_timezone_get() gets me 'Europe/Lisbon', as set in the first line.
 [2015-08-24 15:25 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-08-24 15:25 UTC] cmb@php.net
The exception is due to the warning that the default timezone has
not been set. This warning has been removed as of PHP 7[1], so the
issue is resolved, see <https://3v4l.org/4iVJ8>.

@headsup: this would be another issue, so please open a new
ticket.

[1] <https://wiki.php.net/rfc/date.timezone_warning_removal>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu May 08 09:01:27 2025 UTC