Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timezone problems...

Status
Not open for further replies.

shadedecho

Programmer
Oct 4, 2002
336
US
Ok, I have php 4.1.1 running on a RH 7.1 system. the "date" command at the shell correctly returns my TZ adjusted time (in my case, CST, so -6). However, ALL of the time and date functions (not just the gmxxx() functions) in PHP are returning GMT time, none of them are respecting the timezone setting of the operating system.

Why is this? How do I correct this?
 
most easy is to save every date as gmt, and when queried calculate it back to you're timezone.

you can make the daterecalculation a function in an include file so you don't have to make it on every page you want a date


 
hos2--
It's not an issue of using or viewing stored dates... It's an issue of having the "getdate()" function in PHP find out the current date and then printing that date in output on a webpage. It switches over and thinks it is the next day, 6 hours early, because PHP is seeing the date in GMT time.

sleipnir--
I do not know for sure... but I don't think so, because as i said, if i issue the "date" command from the command line in a shell (logged in as root) it reports time and date info, correctly, in CST timezone format, not GMT.

The point is that PHP is seeing all dates from the system as GMT, even though the system seems to report things as CST correctly. Is this some sort of PHP.INI configuration issue or something like that? I've not been able to see any variable to set to fix this problem.
 
Do you want your server time (in local TZ) output on the webpage or the user's TZ? If you want the user's TZ, the date() function works great.

If you want your server's TZ, you need to use gmdate() and do a little math... here's an example:

$tz_offset = 10; /* 10 hrs ahead of GMT */
$hour = gmdate("H") + $tz_offset;
$minutes = gmdate("i");
$month = gmdate("M");
$year = gmdate ("Y");
if (hour>=23) {
$hour = $hour - 24;
$day = $day + 1; }
echo "This page dynamically created at
$hour$minutes $day $month $year<BR>\n&quot;;
echo &quot;All times are Guam local time, GMT+10\n&quot;;

That should work - it does for me. You'll have to format it how you want it and add in a little more math for cases such as last day of the month, etc. I suggest turning it into a function.

Lowell
=========================
Hafa Adai from Guam, USA!

 
oops... one slight correction...

$tz_offset = 10; /* 10 hrs ahead of GMT */
$hour = gmdate(&quot;H&quot;) + $tz_offset;
$minutes = gmdate(&quot;i&quot;);
$month = gmdate(&quot;M&quot;);
$year = gmdate (&quot;Y&quot;);
if (hour>=24) {
$hour = $hour - 24;
$day = $day + 1; }
echo &quot;This page dynamically created at
$hour$minutes $day $month $year<BR>\n&quot;;
echo &quot;All times are Guam local time, GMT+10\n&quot;;


Lowell
=========================
Hafa Adai from Guam, USA!
 
getdate and date and all should NOT be getting the client's time and date... that makes no sense, they are functions that run on the server system.

BUT, even if they did, i am in the same timezone as my server is, and my server when logged in to a shell and using the &quot;date&quot; command, reports correctly CST.

However, the output of getdate() or gmdate() or any others for that matter, they all show GMT time/date in the browser page, ie, they are all 6 hours ahead of my local CST time. So, obviously they are not picking up the &quot;client&quot; timezone, myself being the client in CST timezone.

I dont wanna have to go through the mess of correcting the GMT time, when getdate() according to PHP is supposed to be timezone specific, meaning it SHOULD be returning the CST time/date like the server is.

anyone with other thoughts?
 
Based on my limited testing, date() seems to reflect the date/time of the system that is running the client browser, not the server. Granted I have not played with it a lot.

If you find the a better answer, I'd be interested in hearing it. I spent a full day researching this a few weeks ago and decided to go with a converion function because it worked and I didn't think I would get a better solution without a substantial amount of additional research. Cost vs. Benefit so to speak.

Lowell
=========================
Hafa Adai from Guam, USA!

Lowell
=============================
Hafa Adai from Guam, USA
Where America's Day begins.
 
ZONE=&quot;America/Chicago&quot;
UTC=false
ARC=false


America/Chicago because that is the closest location to where I am in the current time zone, that was on the list/map during OS installation. But it IS the correct timezone, CST..

I don't know what the UTC and ATC signify. Can you enlighten me?
 
could this be a bug with php 4.1.1? i searched their bug system and found no exact matches to this problem.

wonder if recompiling PHP would help?
 
problem is, i don't want to do either... I would really like to find out WHY this is doing this... and to be honest, i really think its a new thing... i don't it used to have this problem.
 
well i have no idea when the problem started... i haven't used a date-oriented script on this server in more than a year... but i had one before that printed the date, and i could swear that it was always working... that script is no longer even around, but it was simple code so its easily reproduceable.

the answer to your question though is that there really haven't been any major changes of note in quite a long time... to this server, at least... mostly because i dont wanna mess with a workin thing. this is the first time i've noticed a problem.
 
OK, so after some testing, here's what I found out.

PHP's getdate() function IS working correctly. it IS getting the timezone information as it should, and displaying dates correctly.

What is happening is that the use of a certain function is &quot;changing&quot; PHP's interpretation of timezone information (somehow, I don't really know how) so that before the call to this function, things work fine, and after it, PHP is now in GMT mode.

The problem is, it is the compare() function from the PEAR:Date class. I did some reading in their comments, and they talk about converting timezones and such, and say that it may not work correctly on systems which do not have &quot;zoneinfo&quot; on them. which as i found out is a system utility, one that my RH7.1 does not have installed.

So, even though I don't know what PEAR:Date is doing, i guess it is setting something incorrectly as far as environment variables for the timezone to GMT.

and btw, this is not happening persistently at all.. things are reset to normal after each page load. so its just some temporary in memory change. kinda like changing the php error reporting level temporarily.

I worked around this problem by calling getdate() earlier on in the execution of the PHP script, and assigning that value to a variable, which i then use in place of the later getdate() calls, and all works as expected... relatively speaking anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top