php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44399 date (PHP 5) does not support O, but it's missing in the documentation
Submitted: 2008-03-10 20:32 UTC Modified: 2008-03-12 15:51 UTC
From: robertbienert at gmx dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant 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: robertbienert at gmx dot net
New email:
PHP Version: OS:

 

 [2008-03-10 20:32 UTC] robertbienert at gmx dot net
Description:
------------
As of PHP 5, the date() function does not support 'O' anymore. Instead you have got to hack around with DateTime and Timezone objects (that seem to be undocumented, though), but there is nothing about it in the documentation, and PHP 5 is already some years old.

Reproduce code:
---------------
My current hack (written as comment to the date functions manual) follows:

$dt = new DateTime();
$tz = $dt->getTimezone();

$hours  = sprintf('%02d', (int)$tz->getOffset($dt) / 3600);
$mins   = sprintf('%02d', $tz->getOffset($dt) % 3600);
$sign   = $tz->getOffset($dt) > 0 ? '+' : '-';

echo $sign, $hours, $mins;

Expected result:
----------------
The code above works as expected, but I would also expect that the date() manual tells me, that 'O' does not work anymore on PHP 5, why it does not work _and_ what the new way is.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-10 20:41 UTC] danbrown@php.net
Tested this on 5.0.4 and 5.2.4 and receive the expected result.
 [2008-03-10 21:01 UTC] robertbienert at gmx dot net
Well, my error handler caught a E_COMPAT when using date('O') on PHP 5. The message told be to use the TZ environment variable or code like I presented.
 [2008-03-11 04:51 UTC] bjori@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

There is no such errorlevel in PHP.
 [2008-03-11 11:25 UTC] robertbienert at gmx dot net
OK, my fault, the error is called E_STRICT (int value 2048). And here follows a snippet from my code that emitted and caught the "error":

<?php
// custom error handler
function error($no, $msg, $file, $line) {
        $msg = str_replace(array("\t", "\r\n", "\n", "\r"), ' ', $msg);

        echo 'Error (', $no, '): ', $msg, ' in ', $file, ' @line ', $line, "\n";

        exit;
}
set_error_handler('error');

echo 'PHP version: ', PHP_VERSION, "\n";  // is 5.2.0-8+etch10 for me

// now calling date to get _only_ the timezone offset
$offset = date('O');

// this line is never reached (because of the exit, but it is a usual way to exit on errors):
echo 'current timezone offset: ', $offset, "\n";
?>

The (plain) error message from PHP 5 follows:

date(): It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable 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/Berlin' for 'CET/1.0/no DST' instead

And it is E_STRICT as my error handler reports (got it in the $no variable). Hope this helps reproducing things.
 [2008-03-11 11:52 UTC] gwynne@php.net
This does not suggest that 'O' is unsupported. The error message you're seeing was introduced in PHP 5.1, and indicates that PHP had to guess your current timezone. This warning will appear regardless of the date string you try to use. The 'O' character is in fact fully supported by date() through all current versions of PHP. See <http://www.php.net/manual/en/function.date-default-timezone-set.php> for more information.
 [2008-03-11 12:17 UTC] robertbienert at gmx dot net
So this is in general date() related or am I wrong? Well, why does my hack still work (obtaining a guessed timezone) and especially: Where is this documentated?
 [2008-03-12 08:34 UTC] tularis@php.net
This is in general related to all date/time functionality in PHP and is noted on the page linked by Gwynne. Next time, please read the responses people give you before asking for explenations.
 [2008-03-12 15:51 UTC] robertbienert at gmx dot net
So you don't think that it may be helpfull to have a notice on <http://www.php.net/manual/de/ref.datetime.php>, too? When I catch this error, the first thing is having a look at the date() manual ? hm, nothing, than the Date/Time functions ? hm, nothing and then? I guess that most user will throw E_STRICT away instead of further investigation of the errors reason.

BTW: The (good!) object-orientated date/time interface could be mentioned more clearly in the manual, currently it is hidden by functions with very long names.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Jun 17 22:00:01 2026 UTC