Jump to content

Archived

This topic is now archived and is closed to further replies.

arche

daylight savings time

Recommended Posts

Hi, part of a small member system I am creating for my site allows users to change their timezone, then all sitewide times are in their local timezone. I've managed to do that, but my question is, what about daylight savings time? Is there a way to calculate it automatically, via php, or will I have to resort to asking my users to input their dst offset manually (those poor things), or should I not even bother with it?

Thanks in advance.

Share this post


Link to post
Share on other sites
I dont think there is a way of calculating a users DST with PHP as PHP cant communicate with the client computer you will have to provide a pull down menu that has all the DST's. You can see a full DST pulldown menu if you go to My Controls and select Board Settings.

There should be a pull down menu which will show all DSTs!

Share this post


Link to post
Share on other sites
[!--quoteo(post=361163:date=Apr 3 2006, 10:33 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 3 2006, 10:33 AM) [snapback]361163[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I dont think there is a way of calculating a users DST with PHP as PHP cant communicate with the client computer you will have to provide a pull down menu that has all the DST's. You can see a full DST pulldown menu if you go to My Controls and select Board Settings.

There should be a pull down menu which will show all DSTs!
[/quote]

there's something here about DST using javascript to work it out, but how useful it is (i havent read it fully or tested it) i don't know:
[a href=\"http://www.csgnetwork.com/timezoneproginfo.html\" target=\"_blank\"]http://www.csgnetwork.com/timezoneproginfo.html[/a]

otherwise, you could use an IP2country type script to detect where a user lives and use it to pull from a lookup table of timezones, but for all the effort you'd need to put in to get this to work, it'd be easier to allow the user to pick from a dropdown as wildteen suggested.

[b]EDIT:[/b] this looks interesting:

[a href=\"http://www.expertsrt.com/tutorials/Matt/PHPTimeZone.html\" target=\"_blank\"]http://www.expertsrt.com/tutorials/Matt/PHPTimeZone.html[/a]

Share this post


Link to post
Share on other sites
What you could do is make a [!--coloro:#33CC00--][span style=\"color:#33CC00\"][!--/coloro--]combination of php and javascript[!--colorc--][/span][!--/colorc--].
Use php to calculate the start and end dates of dst in different countries.
Use the server time to calculate the standard gmt without dst.
Then add or substract time to calculate the local time in any country.
For countries with more timezones you'd have to add the states information as well.
Not easy, but can be done. By declaring 7 different DST zones you pretty much got them all covered (you can extend to all but I wouldn't bother with a few days difference for just some countries).

// Get today
$eg_MKDate1 = mktime();
// Format date string
$eg_Date1 = @date("Y-m-j", $eg_MKDate1);

//Declare second sunday of march
$day1 = "2007-03-04";
//Declare last sunday of march
$day2 = "2006-03-26";
//Declare first sunday of april
$day3 = "2006-04-02";
//Declare last sunday of september
$day4 = "2006-09-24";
//Declare first sunday of october zoneDS
$day5 = "2006-10-01";
//Declare second sunday of october
$day6 = "2006-10-08";
//Declare last sunday of october zoneB
$day7 = "2006-10-29";
//Declare last sunday of october zoneBS
$day8 = "2006-10-29";
//Declare first sunday of april zoneBS
$day9 = "2007-04-01";

//Declare DST zone A, from last sunday march to last sunday october, plus 1 hour
//Declare DST zone B, from first sunday april to last sunday october, plus 1 hour
//Declare DST zone BS, from last sunday october to first sunday april, plus 1 hour
//Declare DST zone C, from second sunday october to second sunday march, plus 1 hour
//Declare DST zone D, from last sunday march to last sunday september, plus 1 hour
//Declare DST zone DS, from first sunday october to second sunday march, plus 1 hour
//Declare DST zone no, from no daylight saving time

$timez = $eg_Result1['Timezone'];
$usertz = $eg_Result7['DST'];
switch($usertz){
case "A":
if($day2 < $eg_Date1 && $eg_Date1 < $day7)
{
$zone = "-60";
}
else
{
$zone = "0";
}
break;
case "B":
if($day3 < $eg_Date1 && $eg_Date1 < $day7)
{
$zone = "-60";
}
else
{
$zone = "0";
}
break;
case "BS":
if($day8 < $eg_Date1 && $eg_Date1 < $day9)
{
$zone = "-60";
}
else
{
$zone = "0";
}
break;
case "C":
if($day6 < $eg_Date1 && $eg_Date1 < $day1)
{
$zone = "-60";
}
else
{
$zone = "0";
}
break;
case "D":
if($day2 < $eg_Date1 && $eg_Date1 < $day4)
{
$zone = "-60";
}
else
{
$zone = "0";
}
break;
case "DS":
if($day5 < $eg_Date1 && $eg_Date1 < $day1)
{
$zone = "-60";
}
else
{
$zone = "0";
}
break;
case "no":
$zone = "0";
break;
}

$timediff = $zone + $timez;

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Calculate the timedifference per country and insert it in below javascript as a (echo php) variable $timediff, this part goes into the body of your page.[!--colorc--][/span][!--/colorc--]
[!--coloro:#33FF33--][span style=\"color:#33FF33\"][!--/coloro--]Note: the timedifference has to be in minutes[!--colorc--][/span][!--/colorc--]

<!-- Adjust the placement of the clock in the line below -->
<span id="timecontainer" style="position:absolute;left:90;top:15;"></span></font></font>

<script type="text/javascript">
new showLocalTime("timecontainer", "server-php", <? echo $timediff ?>, "short")
</script>


[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]copy/paste below javascipt in the head of your page:[!--colorc--][/span][!--/colorc--]

<script type="text/javascript">

var weekdaystxt=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

function showLocalTime(container, servermode, offsetMinutes, displayversion){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.displayversion=displayversion
var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
this.localtime=this.serverdate=new Date(servertimestring)
this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
this.updateTime()
this.updateContainer()
}

showLocalTime.prototype.updateTime=function(){
var thisobj=this
this.localtime.setSeconds(this.localtime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

showLocalTime.prototype.updateContainer=function(){
var thisobj=this
if (this.displayversion=="long")
this.container.innerHTML=this.localtime.toLocaleString()
else{
var hour=this.localtime.getHours()
var minutes=this.localtime.getMinutes()
var seconds=this.localtime.getSeconds()
var ampm=(hour>=12)? "PM" : "AM"
var dayofweek=weekdaystxt[this.localtime.getDay()]
this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
}
setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
}

function formatField(num, isHour){
if (typeof isHour!="undefined"){ //if this is the hour field
var hour=(num>12)? num-12 : num
return (hour==0)? 12 : hour
}
return (num<=9)? "0"+num : num//if this is minute or sec field
}

</script>

[a href=\"http://en.wikipedia.org/wiki/Daylight_saving_time#Europe\" target=\"_blank\"]http://en.wikipedia.org/wiki/Daylight_saving_time#Europe[/a] will give you more information on which country is in what zone.

Share this post


Link to post
Share on other sites

×

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.