php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45498 strftime('%z') not obeying date_default_timezone_set()
Submitted: 2008-07-13 10:24 UTC Modified: 2008-07-13 11:18 UTC
From: pager at marck dot us Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.6 OS: Windows XP Home
Private report: No CVE-ID: None
 [2008-07-13 10:24 UTC] pager at marck dot us
Description:
------------
strftime is not showing the correct time zone as set by date_default_timezone_set

Please note strftime reflects the correct time for the new time zone setting, but not the correct time zone.

Reproduce code:
---------------
<?php
  
 error_reporting(E_ALL);

 $before = strftime('%H:%M %z');
  
 date_default_timezone_set('America/New_York');

 $after = strftime('%H:%M %z');
  
 echo("date('e'): ". date('e'). ", strftime('%z'): ". strftime('%z'));

 echo ", strftime('%H:%M %z') before: $before, strftime('%H:%M %z') after: $after";
  
?> 

Expected result:
----------------
date('e'): America/New_York, strftime('%z'): Eastern Daylight Time, strftime('%H:%M %z') before: 05:21 Eastern Daylight Time, strftime('%H:%M %z') after: 06:21 Eastern Daylight Time

Actual result:
--------------
date('e'): America/New_York, strftime('%z'): Central Daylight Time, strftime('%H:%M %z') before: 05:21 Central Daylight Time, strftime('%H:%M %z') after: 06:21 Central Daylight Time

// note: on my setup Central Daylight Time would be expected if  date_default_timezone_set had not been used

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-13 10:48 UTC] derick@php.net
This is not a bug - strftime() uses the Operating System timezone information which applications can not override. You want the date() function to handle this correctly. The date() function does not rely on the Operating System for any formatting.
 [2008-07-13 11:00 UTC] pager at marck dot us
strftime is obeying the script setting for time format strings, and the OS for time zone format strings.

How is that not a bug?

One or the other has to be wrong.

My example reports 06:21 Central Daylight Time when it's 05:21 Central Daylight Time for that combination of settings. How is that ever correct behavior?
 [2008-07-13 11:03 UTC] derick@php.net
That's because PHP can calculate the date/time parameters for you, but it can't touch the timezone that the OS uses internally as this is not part of the struct that's passed to the system's strftime() call. Just use date() and forget about strftime().
 [2008-07-13 11:18 UTC] pager at marck dot us
PHP should account for the fact that strftime can't read the script's time zone setting, and pass a struct which reflects times that are consistent with the time zone that strftime will display.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 21:01:33 2024 UTC