473,411 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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=="true") //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 3644
>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=="true") //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=="true") //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("first sunday", strtotime("april 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 "standardize" 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
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...
10
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 ...
1
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...
6
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...
6
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...
3
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...
3
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...
37
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.