Figuring out when DST Starts

### $reference_date format = m-Y
function get_day($describer,$weekday,$reference_date )
{
        $d = explode('-',$reference_date);

        switch($describer)
        {
                case 'first':  $offset = get_day_offset($reference_date, $weekday); break;
                case 'second': $offset = get_day_offset($reference_date, $weekday) + 7; break;
                case 'third':  $offset = get_day_offset($reference_date, $weekday) + 14; break;
                case 'fourth': $offset = get_day_offset($reference_date, $weekday) + 21; break;
                case 'last':   $reference_date = ($d[0]+1).'-'.($d[1]); $d = explode('-',$reference_date);
                $offset = get_day_offset($reference_date, $weekday) - 7; break;
        }

        $r = mktime( 0, 0, 0, $d[0], 1+$offset, $d[1] );

        return $r;  ### returns timestamp format
}

### $anchor format = m-Y
function get_day_offset($anchor,$target)
{
        $ts = explode('-',$anchor);
        $ts = mktime(0,0,0,$ts[0],'01',$ts[1]);

        $anchor = date("w",$ts);
        $target = strtolower($target);
        $days = array( 'sunday'=>0, 'monday'=>1, 'tuesday'=>2, 'wednesday'=>3, 'thursday'=>4, 'friday'=>5, 'saturday'=>6 );

        $offset = $days[$target] - $anchor;
        if($offset<0) $offset+=7;

        return $offset;  ### returns 0-6 for use in get_day();
}

###
# Use the functions get_day() and get_day_offset() to find when dst begins and ends
# Note: This does not account for the fact the DST begins at 2am. This assumes midnight.
###

$dst_begins = get_day("second", "sunday", "03-".date("Y"));
$dst_ends = get_day("first", "sunday", "11-".date("Y"));

### Convert the date to UNIX time
$pubDate_unix = date("U",strtotime("now")); ### This is GMT time

### See if the pubDate is in-between the DST beginning and ending times
if($pubDate_unix >= $dst_begins && $pubDate_unix < $dst_ends)
{
        ### 4*60*60 adds 4 hours to the unix time in seconds (converts GMT to EST-DST)
        $pubDate_unix = $pubDate_unix - (4 * 60 * 60);
}
else
{
        ### 5*60*60 adds 5 hours to the unix time in seconds (converts GMT to EST)
        $pubDate_unix = $pubDate_unix - (5 * 60 * 60);
}

Borrowed from http:docs.php.net/strtotime » jay at jaymunda dot com.

Page Comments (Click to edit)

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Powered by pmwiki-2.2.0-beta65