PHP time vs server time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

PHP time vs server time

Post by charp »

I have PHP scripts that depend on the server time being accurate. After the early switch over to DST, I noticed that all my pages using this script were off by one hour. I assumed that the server time had not been properly set. I emailed tech support for my hosting company to ask that they fix the server's time, but they insist that the server time is correct and my script is wrong.

Is there something else going on here that I don't understand? I even posted a test page with this:

Code: Select all

<?php 
echo date('l, F j :: h:i A'); 
?>
That script shows the server time to be off by one hour. The hosting servers, BTW, are in the same state so I'm certain we're supposed to be in the same time zone. Is there some sort of PHP patch that needs to be applied to adjust for the early shift to Daylight Saving Time? I'm baffled that the hosting company's tech support doesn't know why the two times are off by an hour.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I don't believe it's a php patch, but the server will need to be patched...

Assuming you're in the same state, timezone, and that your state/area observes dst, and that your time is indeed correct, then you should inform your hosts of their inability to read the current time.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The server's zoneinfo database is probably wrong.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You can fix this in your coding.

Code: Select all

$timestamp = time();  //or $time_stamp_from_database
echo date('l, F j :: h:i A', $timestamp+2*60*60);
I believe that would echo a date that's 2 hours off, just switch it to 60*60 to get it one hour off and back like it used to be.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

d11wtq wrote:The server's zoneinfo database is probably wrong.
Not all places do DST.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post by charp »

Thanks everyone! I know that PHP reads the time off the server, so how could my script suddenly be one hour off. The web server is definitely in the same time zone, so that's not the issue. Let's see what tech support does when I insist that something is not right. I'll be sure to include the suggestions about the server patch and the zoneinfo database.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please post the output of

Code: Select all

<?php
if ( !defined('DATE_RFC850') ) {
	defined('DATE_RFC850', 'l, d-M-y H:i:s T');
}

echo 'version: ', phpversion(), "<br />\n";
echo 'uname: ', php_uname('srm'), "<br />\n";
echo 'time: ', time(), "<br />\n";
echo 'date: ', date(DATE_RFC850), "<br />\n";
echo 'gmdate: ', date(DATE_RFC850), "<br />\n";
echo 'ENV[TZ]: ', isset($_ENV['TZ']) ? $_ENV['TZ'] : 'n/a', "<br />\n";
echo 'date.timezone: ', get_cfg_var('date.timezone'), "<br />\n";
echo 'default tz: ', function_exists('date_default_timezone_get') ? date_default_timezone_get() : 'n/a', "<br />\n";
echo 'locatime-file: ', exec('ls -lad /etc/localtime'), "<br />\n";
echo 'system date: ', exec('date'), "<br />\n";
The timezone for
Location: Rancho Cucamonga, Calif. USA
should be US/Pacific which is currently Pacific Daylight Time (PDT)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Re: PHP time vs server time

Post by AKA Panama Jack »

Code: Select all

<?php 
echo date('l, F j :: h:i A T'); 
?>
Add the T to to your date and it will tell you the time zone.

It is shows CST then it is Central Standard Time. If it shows CDT then it is Central Daylight Time. If it doesn't show CDT then the server doesn't support the new Daylight Savings Time. If the center letter is an S then the server isn't running on the new Daylight Savings Time.

Code: Select all

<?php 
echo date('l, F j :: h:i A I'); 
?>
You can also use I and if it shows a 1 then the server supports the new Daylight Savings Time. If it shows a 0 then it doesn't.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

scottayy wrote:
d11wtq wrote:The server's zoneinfo database is probably wrong.
Not all places do DST.
No, that's what the zoneinfo database is for ;) It's a huge database which maps all the timezones to the DST switchovers.
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post by charp »

Great info here, people. Here's are the results of the suggestions given:

Volka's code:

Code: Select all

if (!defined('DATE_RFC850')) {
defined('DATE_RFC850', 'l, d-M-y H:i:s T');
}

echo 'version: ', phpversion(), "<br />\n";
echo 'uname: ', php_uname('srm'), "<br />\n";
echo 'time: ', time(), "<br />\n";
echo 'date: ', date(DATE_RFC850), "<br />\n";
echo 'gmdate: ', date(DATE_RFC850), "<br />\n";
echo 'ENV[TZ]: ', isset($_ENV['TZ']) ? $_ENV['TZ'] : 'n/a', "<br />\n";
echo 'date.timezone: ', get_cfg_var('date.timezone'), "<br />\n";
echo 'default tz: ', function_exists('date_default_timezone_get') ? date_default_timezone_get() : 'n/a', "<br />\n";
echo 'locatime-file: ', exec('ls -lad /etc/localtime'), "<br />\n";
echo 'system date: ', exec('date'), "<br />\n";
echo'<br />';
echo date('l, F j :: h:i A T'); 
echo'<br />';
echo date('l, F j :: h:i A I');
Produced this:
Warning: Wrong parameter count for defined() in /home/XXXX/public_html/timetest.php on line 23
version: 4.4.1
uname: FreeBSD
time: 1174066518
date: FriAMPSTE_RMarchC850
gmdate: FriAMPSTE_RMarchC850
ENV[TZ]: n/a
date.timezone:
default tz: n/a
locatime-file: -r--r--r-- 1 root wheel 2819 Mar 12 09:31 /etc/localtime
system date: Fri Mar 16 09:35:18 PST 2007
Panama Jack's code:

Code: Select all

<?php
echo date('l, F j :: h:i A T');
?>

<?php
echo date('l, F j :: h:i A I');
?>
Produced this:
Friday, March 16 :: 09:35 AM PST
Friday, March 16 :: 09:35 AM 0
Based on these results, I see that the server is set to PST and is not honoring the shift to DST. The system time shown in the results above also shows PST, but does have the correct time. Apparently, the hosting company merely moved the server time ahead by one hour without making the proper adjustment for DST.

Thanks again everyone. Now I have some concrete details to show tech support that something needs to be fixed on their end.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops, copy&paste errors render my script almost useless.
second defined should be define. second date() should be gmdate()

Code: Select all

if (!defined('DATE_RFC850')) {
  define('DATE_RFC850', 'l, d-M-y H:i:s T');
[...]
echo 'gmdate: ', gmdate(DATE_RFC850), "<br />\n";
charp wrote:Produced this:
Friday, March 16 :: 09:35 AM PST
Friday, March 16 :: 09:35 AM 0
Based on these results, I see that the server is set to PST and is not honoring the shift to DST. The system time shown in the results above also shows PST, but does have the correct time. Apparently, the hosting company merely moved the server time ahead by one hour without making the proper adjustment for DST.
If it was 9:35 am that day then yes, they just shifted the clock.
With a proper zoneinfo database you get

Code: Select all

date_default_timezone_set('US/Pacific');
echo date(DATE_RFC850, 1174066518);
Friday, 16-Mar-07 10:35:18 PDT
(sorry, date_default_timezone_set is not available for php 4.4.1)
locatime-file: -r--r--r-- 1 root wheel 2819 Mar 12 09:31 /etc/localtime
what a pitty, not a symbolic link :(
but maybe zdump is available.

Code: Select all

echo "start<br >\n";
system('zdump -v /etc/localtime | grep 2007'), "<br />\n";
echo "end<br >\n";
When I do this on a (hopefully) recent US/Pacific file I get

Code: Select all

zdump -v /usr/share/zoneinfo/US/Pacific | grep 2007
/usr/share/zoneinfo/US/Pacific  Sun Mar 11 09:59:59 2007 UTC = Sun Mar 11 01:59:59 2007 PST isdst=0
/usr/share/zoneinfo/US/Pacific  Sun Mar 11 10:00:00 2007 UTC = Sun Mar 11 03:00:00 2007 PDT isdst=1
/usr/share/zoneinfo/US/Pacific  Sun Nov  4 08:59:59 2007 UTC = Sun Nov  4 01:59:59 2007 PDT isdst=1
/usr/share/zoneinfo/US/Pacific  Sun Nov  4 09:00:00 2007 UTC = Sun Nov  4 01:00:00 2007 PST isdst=0
which seems to be ok according to
http://en.wikipedia.org/wiki/Energy_Policy_Act_of_2005 wrote:The bill amends the Uniform Time Act of 1966 by changing the start and end dates of daylight saving time from 2007. Clocks were set ahead one hour on the second Sunday of March (March 11, 2007) instead of the first Sunday of April (April 1, 2007).
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post by charp »

Volka,

With your correct code, I get this:
version: 4.4.1
uname: FreeBSD
time: 1174086926
date: Friday, 16-Mar-07 15:15:26 PST
gmdate: Friday, 16-Mar-07 23:15:26 GMT
ENV[TZ]: n/a
date.timezone:
default tz: n/a
locatime-file: -r--r--r-- 1 root wheel 2819 Mar 12 09:31 /etc/localtime
system date: Fri Mar 16 15:15:26 PST 2007
Using the code for the zdump:
start
/etc/localtime Sun Mar 11 09:59:59 2007 UTC = Sun Mar 11 01:59:59 2007 PST isdst=0 gmtoff=-28800 /etc/localtime Sun Mar 11 10:00:00 2007 UTC = Sun Mar 11 03:00:00 2007 PDT isdst=1 gmtoff=-25200 /etc/localtime Sun Nov 4 08:59:59 2007 UTC = Sun Nov 4 01:59:59 2007 PDT isdst=1 gmtoff=-25200 /etc/localtime Sun Nov 4 09:00:00 2007 UTC = Sun Nov 4 01:00:00 2007 PST isdst=0 gmtoff=-28800 end
Looking at the output on the first bit, I realize that the system time was also off. The first time around, I thought I saw it as the correct time, but it's 1 hour off as well. So, now I'm really confused about why the tech guys think the server is on the correct time.

As for the zdump, I think it's showing that the server was set to switch to DST on the correct date. If it helps anyone looking at this stuff, the server is located in Los Angeles.

I'm going to email tech support again with some of this code. Let's see what they think.
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post by charp »

It took several days, but my request for tech support was bumped to the third tier. I just received a reply that the problem was fixed, but I find that it was only sort of fixed. The code suggested in this thread was showing that the server time was off, but now it is showing the correct time. Well, all but one line of code was showing the correct time. After a bit of inspection, I found that PHP will now show the correct time only if the following line is included:

Code: Select all

putenv('TZ=PST8PDT');
Here's all of the code I've been using to test this time thing:

Code: Select all

<?php
echo date('l, F j :: h:i A');
echo'<br />';
putenv('TZ=PST8PDT');
echo date("Y-m-d h:i:sa", time());
echo'<br />';
echo date('l, F j :: h:i A'); 

echo'<br />';
echo'<br />';
if (!defined('DATE_RFC850')) {
  define('DATE_RFC850', 'l, d-M-y H:i:s T');
}

echo 'version: ', phpversion(), "<br />\n";
echo 'uname: ', php_uname('srm'), "<br />\n";
echo 'time: ', time(), "<br />\n";
echo 'date: ', date(DATE_RFC850), "<br />\n";
echo 'gmdate: ', gmdate(DATE_RFC850), "<br />\n";
echo 'ENV[TZ]: ', isset($_ENV['TZ']) ? $_ENV['TZ'] : 'n/a', "<br />\n";
echo 'date.timezone: ', get_cfg_var('date.timezone'), "<br />\n";
echo 'default tz: ', function_exists('date_default_timezone_get') ? date_default_timezone_get() : 'n/a', "<br />\n";
echo 'locatime-file: ', exec('ls -lad /etc/localtime'), "<br />\n";
echo 'system date: ', exec('date'), "<br />\n";
echo'<br />';
echo'<br />';
echo date('l, F j :: h:i A T'); 
echo'<br />';
echo'<br />';
echo date('l, F j :: h:i A I');

echo'<br />';
echo'<br />';
echo "start<br >\n";
system('zdump -v /etc/localtime | grep 2007'). "<br />\n";
echo "end<br >\n";
?>
Here are the current results of the above:

Code: Select all

Thursday, March 22 :: 02:12 PM
2007-03-22 03:12:27pm
Thursday, March 22 :: 03:12 PM

version: 4.4.1
uname: FreeBSD
time: 1174601547
date: Thursday, 22-Mar-07 15:12:27 PDT
gmdate: Thursday, 22-Mar-07 22:12:27 GMT
ENV[TZ]: n/a
date.timezone:
default tz: n/a
locatime-file: -r--r--r-- 1 root wheel 837 Mar 22 13:43 /etc/localtime
system date: Thu Mar 22 15:12:27 PDT 2007


Thursday, March 22 :: 03:12 PM PDT

Thursday, March 22 :: 03:12 PM 1

start
/etc/localtime Sun Mar 11 09:59:59 2007 UTC = Sun Mar 11 01:59:59 2007 PST isdst=0 gmtoff=-28800 /etc/localtime Sun Mar 11 10:00:00 2007 UTC = Sun Mar 11 03:00:00 2007 PDT isdst=1 gmtoff=-25200 /etc/localtime Sun Nov 4 08:59:59 2007 UTC = Sun Nov 4 01:59:59 2007 PDT isdst=1 gmtoff=-25200 /etc/localtime Sun Nov 4 09:00:00 2007 UTC = Sun Nov 4 01:00:00 2007 PST isdst=0 gmtoff=-28800 end
Notice that the first time shown above is one hour behind. If I remove putenv('TZ=PST8PDT');, all of the times are off by one hour and times are shown as PST instead of PDT.

All of this leads me to two questions:

1. What the heck is going on??
2. Can anyone explain to me the meaning of TZ=PST8PTD??

Thanks!
Last edited by charp on Thu Mar 22, 2007 6:55 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It would seem they didn't update the server with the new daylight saving rules.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if there were a <pre>....</pre> it would be easier to read the output from system().

Code: Select all

echo "<pre>start\n";
system('zdump -v /etc/localtime | grep 2007'). "<br />\n";
echo "\nend</pre>\n";
would have printed
start
/etc/localtime Sun Mar 11 09:59:59 2007 UTC = Sun Mar 11 01:59:59 2007 PST isdst=0 gmtoff=-28800
/etc/localtime Sun Mar 11 10:00:00 2007 UTC = Sun Mar 11 03:00:00 2007 PDT isdst=1 gmtoff=-25200
/etc/localtime Sun Nov 4 08:59:59 2007 UTC = Sun Nov 4 01:59:59 2007 PDT isdst=1 gmtoff=-25200
/etc/localtime Sun Nov 4 09:00:00 2007 UTC = Sun Nov 4 01:00:00 2007 PST isdst=0 gmtoff=-28800
end
It's the new zoneinfo.
Post Reply