Unable to resolve leap seconds miscalculation on calendar

51 views Asked by At

I have a calendar that is showing me the wrong days of the date for the day of the week. More specifically, they are one day behind the actual date.

I've done a bunch of research and used the following SO question (php date('d') calculates same output for two consecutive days) but I can't get it to work on my script.

It's currently displaying the 26th of october twice, from there on the dates go wrong.

$firstday = mktime(0,0,0,$month,1,$year);
$day = date('Y-m-d',$firstday);
$fday = strtotime($day." last Sunday ",$firstday);
$currday_timestamp = mktime(0,0,0,date('m'),date('d'),date('Y'));

for($i=0;$i<7;$i++) {
            $firstweek[$i]['id'] = date("Y-m-d",$fday+(86400 * $i));
            $firstweek[$i]['val'] = date("d",$fday+(86400 * $i));
            if($currday_timestamp > $fday+(86400 * $i)) {
                $firstweek[$i]['flag'] = 1;    
            }
            $secondweek[$i]['id'] = date("Y-m-d",$fday+(86400 * ($i+7)));
            $secondweek[$i]['val'] = date("d",$fday+(86400 * ($i+7)));
            if($currday_timestamp > $fday+(86400 * ($i+7))) {
                $secondweek[$i]['flag'] = 1;    
            }
            $thirdweek[$i]['id'] = date("Y-m-d",$fday+(86400 * ($i+14)));
            $thirdweek[$i]['val'] = date("d",$fday+(86400 * ($i+14)));
            if($currday_timestamp > $fday+(86400 * ($i+14))) {
                $thirdweek[$i]['flag'] = 1;    
            }
            $fourthweek[$i]['id'] = date("Y-m-d",$fday+(86400 * ($i+21)));
            $fourthweek[$i]['val'] = date("d",$fday+(86400 * ($i+21)));
            if($currday_timestamp > $fday+(86400 * ($i+21))) {
                $fourthweek[$i]['flag'] = 1;    
            }
            $fifthweek[$i]['id'] = date("Y-m-d",$fday+(86400 * ($i+28)));
            $fifthweek[$i]['val'] = date("d",$fday+(86400 * ($i+28)));
            if($currday_timestamp > $fday+(86400 * ($i+28))) {
                $fifthweek[$i]['flag'] = 1;    
            }
            $sixthweek[$i]['id'] = date("Y-m-d",$fday+(86400 * ($i+35)));
            $sixthweek[$i]['val'] = date("d",$fday+(86400 * ($i+35)));
            if($currday_timestamp > $fday+(86400 * ($i+35))) {
                $sixthweek[$i]['flag'] = 1;    
            }
        }

How would I go about tackling this leap seconds problem? Is there a value I need to add to the seconds?

1

There are 1 answers

2
markt On BEST ANSWER

Sounds like this is a to do with the change from daylight saving time to standard time.

Simplest way is to add a couple of hours to your base time:

$time_offset = 2 * 60 * 60; // 2 hours
$firstday = mktime(0,0,0,$month,1,$year) + $time_offset;
$day = date('Y-m-d',$firstday);
$fday = strtotime($day." last Sunday ",$firstday) + $time_offset;
$currday_timestamp = mktime(0,0,0,date('m'),date('d'),date('Y')) + $time_offset;