By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

PHP Daylight Savings Time GMT Alogorithm

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
>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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.