Jump to content

Simple PHP timezone / daylight saving question


johnsmith153

Recommended Posts

I just need exact clarification on this as a few things have confused me.

 

My server time is apparently set to Mountain Time USA (which means nothing to me).

 

I am in, and all the visitors to the website I am dealing with are in, the UK.

 

Over the weekend, our daylight saving ended, and we move from BST (British Summer Time) back to GMT. (our clocks went back 1 hour)

 

I have set date.timezone="Europe/London" in my php.ini

 

If I use the PHP settings to detect daylight saving, will this detect the timezone settings (Europe/London) or the server? (Mountain Time USA)

if (date("I",time()) == 1) $UKlocaltime = gmdate("m/d/Y H:i",time() +3600);
else $UKlocaltime = gmdate("m/d/Y H:i",time());
echo $UKlocaltime;

The above code gives me the correct time now, but what I dont want is for Mountain Time USA to change daylight saving and this affect my time, OR for the UK DST to come into force in 6 months and affect things then.

 

It is easier I resolve this now, rather then wait and see in the future.

 

The key thing in that my site shows exactly the correct UK time at all times, including when our clocks go forward in 6 months.

 

When I change server in the future, probably to another part of America, will this affect me again?

 

Link to comment
Share on other sites

don't use a manual offset like that, as it won't work during DST times (as you have already found out)

 

the proper thing to do is the edit the php.ini file (which you say you have done). this should change all date functions to use that timezone. make sure you restart apache after you edit the php.ini file.

 

if that doesn't work, and you are using PHP 5.1 or newer, you can use this function:

http://us.php.net/manual/en/function.date-default-timezone-set.php

to set the timezone at the time of script execution. it would look something like this:

<?php
  date_default_timezone_set('Europe/London');
  print date("m/d/Y H:i",time());
?>

Link to comment
Share on other sites

Thanks for the help rhodesa.

 

Based on your advice, this is what I will do:

 

My server is set to Europe/London in php.ini.

 

I have NOT used date_default_timezone_set("Europle/London") on any of my pages - I dont feel I need to. (??)

 

All pages wil have:

echo date("m/d/Y H:i",time());

 

This does show the time correctly now, but then as we are not in DST in the UK, I expect it should.

 

A few questions if I could:

 

(1) When Mountain Time USA (which is what the server is on) changes DST status, will this change my time?

(2) When DST comes back into force in the UK in March 2009 (clocks go forward 1 hour), will this be picked up and still show the correct time then?

 

I suppose the key thing is: Does PHP know when DST will come into force or not - in my timezone?? It is the last Sunday in March every year - at 02:00. Does PHP know this?

 

If I wanted Mountain Time USA I would be confident the server guys would change the time by an hour as DST comes in etc.

Link to comment
Share on other sites

PHP should handle all the DSTs correctly now. and, if the dates are correct, you do NOT need to use date_default_timezone_set()

 

also, as a note, you don't need to pass time() as the second argument for date(), as it is assumed. but that is me just being picky now :)

Link to comment
Share on other sites

Excellent. Thanks for all your help. I appreciate your time.

 

One last thing. Just so I can guarantee I understand this and wont have problems.

 

I prefer my database not to store actual text for times/dates on some occasions. Mainly so I can perform various other DB searches etc.

 

If I simply do this when I store the data in my database:

$storeInDB = time();

 

Then when I come to recall from database - could be any time/date - ie if DST status has changed or not etc.

 

echo date("m/d/Y H:i",$valueFromDB);

 

It works now, but obvisouly times stored now may be set with DST in force, and then displayed at a time when DST is not in force, and the other way around.

 

Should I go ahead and use this on all my pages?

Link to comment
Share on other sites

I have spoke to someone who thinks this solution may be wrong.

 

They are saying if you store time() (which is always GMT) - then display using the php.ini timezone setting - then unless the php.ini timezone setting is GMT, it would not be right.

 

Thay are saying:

 

(1) store value as time() - but make a manual adjustment based on the actual time in comparison to GMT. - ie if you are 5 hours ahead of GMT then +18000 onto time (5hrs) - in my case I would add 3600 if daylight saving is in force (+1hr) - but not add anything if daylight saving is not in force as we are in GMT 6months of the year)

 

(2) then, use gmdate (as gmdate is GMT - just like time() is GMT) to display

 

What does everyone think?

 

It just doesnt seem to make sense to store a non timezone dependant value then display it at times when the daylight saving may or may not be in force.

 

If I test now it works, but then both PHP time() and the actual time in my timezone ARE both GMT - so you would expect that. When my timezone moves forward 1 hour to GMT+1 - I may get problems.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.