Jump to content

Recommended Posts

Posted

Hey!

I have added date which shows server time in my website. Unfortunately it shows almost every moment different time. So I can't code a site where I and the others can see when the users have been online and so on. Why is this happening?

Posted

It's hard to help you with only the information you provided. Can you give us an example of what your talking about and maybe the code?

 

BTW, the server is on Pacific Standard Time.

 

Posted

It's hard to help you with only the information you provided. Can you give us an example of what your talking about and maybe the code?

 

BTW, the server is on Pacific Standard Time.

 

Okay, so I have added to my website a PHP code: $datetime = date("Y-m-d H:i:s")

 

When I put "echo" -command to the beginning it shows in my website time like "2011-11-29 21:24:48", but the problem is that time changes almost every time. I mean when I refresh the page it shows 2011-11-29 23:22:44 or 2011-11-30 06:21:21 and so on. So the problem must be at server. I don't even know how that is possible, because it shows the time of server. In my home computer it shows the time exactly correct and in other free web hosting site the time goes normally forward. Does your server change time constantly or something?

Posted

For me it jumps 2 hours back and forward on your example.

If I refresh it many times then usually it shows time in GMT-0800 (PST), but sometimes in GMT-0600 (CST), while byron's page display time in GMT-0500 (EST).

 

Are you sure your script does not change the time zone used for some reason? putenv("TZ=XYZ");

Do you have the same results if you'll echo just the time with having nothing more in the script?

Eventually add to the script the following to confirm if the timezone is not switched:

echo getenv("TZ");

Posted

I added putenv("TZ=XYZ") and getenv("TZ") to my website and I tested it again. The result was that the putenv and getenv did not change. It was only the time which changed... This is so unbelievable problem that I don't know how this could be solved. This is very strange. I can't make reliable system where I save users last activity to database etc.

 

You can try again:

http://intr.heliohost.org/index.php

 

 

Edit:Now I got for example:

2011-11-29 16:37:19

putenv("TZ=XYZ"): 1

getenv("TZ"): XYZ

Posted

There is no such time zone like "XYZ". ; ) Sorry, that was just an example on my side.

You may want to put "TZ=EST" or "TZ=America/New_York" etc.

 

Place putenv("TZ=your_desired_timezone"); at the very beginning of your script and then the time for you should be displayed correctly (unless in some other place of your script you will change timezone again).

 

All valid time zones you can find here:

http://php.net/manual/en/timezones.php

 

 

But I'm curious... if YOU (your script) don't change timezone randomly... why is this changing by itself?... I may test it later.

Posted

Oh, yeah, I'm so tired now because I'm from Finland and here is night at the moment :). I have never used those timezone PHP commands before.

 

But I have now added these in my website:

putenv("TZ=Europe/Helsinki"): 1

getenv("TZ"): Europe/Helsinki

2011-11-30 00:54:12

 

When I now test my website the time does not change or then I didn't test very well yet. But it seems to work now because of changing timezone at the very beginning of my script with that putenv("TZ=Europe/Helsinki"); :). I continue testing.

Posted

Midnight for me in Poland. ; )

 

Anyway... additional note for you: Stevie's (the server your website is on) clock "jumps" back and forward 24 seconds (it's either correct or 24 seconds behind). Admins don't know why is that so far.

Default timezone being switched may be related to the same problem. I think I'll test it when I'll find a time and inclination.

Posted

Aah okay, thanks for that info :). BTW: I have now tested my site quite many times and it really seems to work now. Thank you very much for the help :)! I think it really needed only that putenv code.

Posted

An easier way to set the time zone for your site would be to add this to your root htaccess file:

 

SetEnv TZ Europe/Helsinki

 

That's how I set my time zone to EST.

 

SetEnv TZ US/Eastern

 

Then you don't need to set it in any of your php scripts.

 

Posted

Are you going through the cpanel file manager? You should be able to edit the htaccess file just like any other file.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...