473,725 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Daylight Savings Time GMT Alogorithm

For posterity's sake, here is an algorithm I created to take a GMT
time and convert it to U.S. central time, accounting for daylight
saving time. Note: this algorithm can be modified to work for any
other U.S. timezone by changing the number of second subtracted at the
end.
<?

$in_dst="false" ;

if( (date(m)>=4) && (date(m)<=10) ) // IS CURRENT DATE INSIDE OF
APRIL->OCT WINDOW?
{
if( (date(m)>4) && (date(m)<10) ) //IS CURRENT DATE INSIDE
MAY->SEPT WINDOW?
{
$in_dst="true";
}
elseif($date(m) =="4") //IS IT APRIL?
{
if( (date(j)<=7) && (date(D)=="Sun" ) ) //IS IT THE FIRST SUN OF
THE MONTH?
{
if(date(H)>"1") //IS IT PAST 2:00AM?
{
$in_dst="true";
}
}
}
elseif($date(m) =="10") //IS IT OCT?
{
if( (date(j)<=25) && (date(D)=="Sun" ) ) //IS IT THE LAST SUN OF
THE MONTH?
{
if(date(H)>"1") //IS IT PAST 2:00AM?
{
$in_dst="false" ;
}
}
}
}
if($in_dst=="tr ue") //INSIDE OF DST
{
$date=date("Y:m :dH:i:s",time()-18000); //CHANGE NUMBER OF SECONDS
HERE TO MODIFY
}
else //OUT OF DST
{
$date=date("Y:m :dH:i:s",time()-21600); //CHANGE NUMBER OF SECONDS
HERE TO MODIFY
}

?>
Please post any possible bugs you guys find.
Jul 17 '05 #1
3 3666
>For posterity's sake, here is an algorithm I created to take a GMT
time and convert it to U.S. central time, accounting for daylight
saving time. Note: this algorithm can be modified to work for any
other U.S. timezone by changing the number of second subtracted at the
end.
This algorithm appears to only deal with times in the current year,
or at least only the years between the last and the next dinking
with the law controlling when daylight savings time transitions
happen. History is filled with such dinking. Also, it only deals
with the United States rules when the Central time zone includes a
number of other countries. Canada and Mexico may follow the USA
here but I'm not so sure about South America.


<?

$in_dst="false ";

if( (date(m)>=4) && (date(m)<=10) ) // IS CURRENT DATE INSIDE OF
APRIL->OCT WINDOW?
{
if( (date(m)>4) && (date(m)<10) ) //IS CURRENT DATE INSIDE
MAY->SEPT WINDOW?
{
$in_dst="true";
It is daylight savings time in May through September, inclusive.
This part seems OK. }
elseif($date(m) =="4") //IS IT APRIL?
{
if( (date(j)<=7) && (date(D)=="Sun" ) ) //IS IT THE FIRST SUN OF
THE MONTH?
{
if(date(H)>"1") //IS IT PAST 2:00AM?
{
$in_dst="true";
}
}
It is daylight savings time on the first Sunday in April after 2AM
until the end of that day. It is never daylight savings time on
any other day in April before or after the first Sunday.
Does something seem wrong here?
}
elseif($date(m) =="10") //IS IT OCT?
{
if( (date(j)<=25) && (date(D)=="Sun" ) ) //IS IT THE LAST SUN OF
THE MONTH?
{
if(date(H)>"1") //IS IT PAST 2:00AM?
{
$in_dst="false" ;
}
It is never daylight savings time at any time in October.
Does something seem wrong here?
}
}
}
if($in_dst=="t rue") //INSIDE OF DST
{
$date=date("Y:m :dH:i:s",time()-18000); //CHANGE NUMBER OF SECONDS
HERE TO MODIFY
}
else //OUT OF DST
{
$date=date("Y:m :dH:i:s",time()-21600); //CHANGE NUMBER OF SECONDS
HERE TO MODIFY
}

?>
Please post any possible bugs you guys find.

Jul 17 '05 #2
Fox


Bathroom_Monkey wrote:

For posterity's sake, here is an algorithm I created to take a GMT
time and convert it to U.S. central time, accounting for daylight
saving time. Note: this algorithm can be modified to work for any
other U.S. timezone by changing the number of second subtracted at the
end.

<?

$in_dst="false" ;

if( (date(m)>=4) && (date(m)<=10) ) // IS CURRENT DATE INSIDE OF
APRIL->OCT WINDOW?
{
if( (date(m)>4) && (date(m)<10) ) //IS CURRENT DATE INSIDE
MAY->SEPT WINDOW?
{
$in_dst="true";
}
elseif($date(m) =="4") //IS IT APRIL?
{
if( (date(j)<=7) && (date(D)=="Sun" ) ) //IS IT THE FIRST SUN OF
THE MONTH?
{
if(date(H)>"1") //IS IT PAST 2:00AM?
{
$in_dst="true";
}
}
}
elseif($date(m) =="10") //IS IT OCT?
{
if( (date(j)<=25) && (date(D)=="Sun" ) ) //IS IT THE LAST SUN OF
THE MONTH?
{
if(date(H)>"1") //IS IT PAST 2:00AM?
{
$in_dst="false" ;
}
}
}
}

if($in_dst=="tr ue") //INSIDE OF DST
{
$date=date("Y:m :dH:i:s",time()-18000); //CHANGE NUMBER OF SECONDS
HERE TO MODIFY
}
else //OUT OF DST
{
$date=date("Y:m :dH:i:s",time()-21600); //CHANGE NUMBER OF SECONDS
HERE TO MODIFY
}

?>

Please post any possible bugs you guys find.


this is a lot of code...

how about:

function
isDST(/* assumes "now" */)
{
return stristr(date("T "), "daylight") != false;

}

this will return the current local setting of the *server* (therefore it
will not work on all servers -- see below)

or

given a timestamp:

$yr = date("Y", $timestamp);

$apr = strtotime("firs t sunday", strtotime("apri l 1 $yr")); // sunday >= 4/1

$oct = strtotime("last sunday", strtotime("nov 1 $yr)); // sunday before 11/1

$isDST = $timestamp > $apr && $timestamp < $oct;

you can make timestamp adjustments for timezone if you want... however,
time calcs in the same zone should suffice..

remember -- there are (at least) two areas (not entire states) in the US
that do not have DST -- one is in Arizona (MST all year) and the other
is in Indiana (EST)... so now you have the problem of location as well!
....and this doesn't even begin to scratch the surface.

DST start/end times have varied over the years and may or may not remain
static for the near future. In the 60's, I remember school driven
petitions to "standardiz e" daylight savings time (I was in Vermont at
the time) -- the result was the Uniform Time Act 1966 [with
exemptions!]. Different states (or even regions) would go into and out
of DST on different dates. Before WWII (WT, or war time), observance was
erratic and before and since, until (relatively) recently, it was
observed on a state by state basis.

And then there are all countries outside the US... some observe "Summer
Time" (portions of Great Britain - but does not affect GMT) or other
variants of DST and some do not. Ask any astrologer about DST and watch
them run to the aspirin bottle! There have been books published on the
subject detailing when and where and by how much savings time was used
[not *always* 1 hour! -- DST to the british means *double summer time*
-- google "double summer time" ]...

have fun!
Jul 17 '05 #3
Thanks for the followup posts guys. Also, I was not totally clear in
my description of the algorithm, here's a better description: The
algorithm *returns* the current date/time, accounting for DST in
CST(U.S.)

My website is hosted by a server that does not allow for changing of
timezones so it was either stick with GMT and confuse my users, or
create an algorithm to take care of it for me.

Here are the rules the algorithm is based on:
DST/CST/U.S. Starts on the first Sunday in April at 2:00AM (spring
forward!)
DST/CST/U.S. Ends on the last Sunday in October at 2:00AM (fall back!)

Fox: ack, you're right, I should have used timestamps- they're so much
easier to work with than date() for this type of thing!
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7214
by: hourang | last post by:
ok im getting tired of looking for an answer and coming up short with scripts that dont work. i have a application that uses GMT for all its times and needs the clients timeoffset for showing the correct times for everything. i use date.getTimezoneOffset() which works fine and shows the daylight savings time correct value of -4 since im in eastern standard time and its daylight savings time right now. well eastern standard time is...
10
5261
by: Marc Pelletier | last post by:
Hello, I am writing an application that does some simple astronomical calculations. One of the variables I need is the number of hours passed in this year. I've written the following function public static double GetHoursofYear( DateTime aTime ) { DateTime StartYear = new DateTime( aTime.Year, 1, 1 ); return ( aTime.ToOADate() - StartYear.ToOADate() ) * 24;
1
1378
by: Daniel | last post by:
Is there a patch for congress new 2007 daylight savings time change? http://geography.about.com/cs/daylightsavings/a/dst.htm This could have economic impact like y2k did. Is there a patch for windows 95/98/me/xp/2000/2003/etc. to handle new daylight savings time rules? are there any patches for .net? win32? etc.? what all else is impacted by this in microsoft products and apis?
6
1423
by: Daniel | last post by:
Do any microsoft buildtime dependancies C#/C/c++/VB/etc. need to be updated for the 2007 policy on daylight savings time? I know the OS needs to be updated but I care now about C libraries, .net binaries, etc. that microsoft maintains which have coded in them logic around datetime. e.g. the C# and c++ datetime objects that microsoft maintains, do these have any logic that needs to be updated reguardless of the OS being updated? Or is all...
6
4018
by: sugapablo | last post by:
I have an old machine running Mandrake 9.2 and PHP 4.3.1 hosting several websites. With the coming changes to daylight savings time in March 2007, what are my options in correcting the current calculation on this machine? Ideal option: RPM patch. Worst option: upgrade OS (it's a very old machine).
3
6003
by: J | last post by:
Hello. Our webserver is running Windows 2000 Server iis5 and was wondering if anyone knew if the new Daylight Savings Time rule will affect any classic ..asp pages? I think I'm mainly concerned with the date functions like DateDiff() or Now() that is used in these classic .asp pages that our webserver hosts? Thanks in advance. J
3
4325
by: mmuras | last post by:
I did not see any discussions / threads for this but if there is one please let me know: -First, I am one of only two individuals within my company's IT Dept. -We have a Windows Server 2003 R2 Standard Ed. Box for our Domain controller. -We are having issues STILL today in April with the Microsoft Windows Daylight Savings Issues ***Symptoms:
37
4686
by: David T. Ashley | last post by:
I have Red Hat Enterprise Linux 4. I was just reading up about UTC and leap seconds. Is it true on my system that the Unix time may skip up or down by one second at midnight when there is a leap second? By "Unix time" I mean the integer returned by time() and similar functions. I'm concerned about the "down" case. Some of the software I've written
0
1520
by: =?Utf-8?B?S2VsbHk=?= | last post by:
Each fall and winter I deal witht he same problem. When the daylight savings time comes into effect the time stamp for files on my external portable hard drive are 1 hour out from the time stamps for the files on my Desktop hard drive I then need to delete my files off my external portable HD and then recopy the "updated time stamp files" external portable HD. Is there anything I can do to prevent this from happening. I have tried...
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.