Unix timestamp and week calculation problem

Hello

I’m coding a weekly calendar in php with links for previous and next week. It all worked fine until today when I found out that in week 43 something happens.

The case is as follows. I get a unix time stamp and the calendar shows the week for that time. For example the time for the first view is taken from the time() function. The calendar calculates the beginning of the week and plots it out. I have two links: previous week and next week. Since one week is 606024*7 seconds I simply add for the next week link and subtract for previous week link. Now the time is taken from the links (not the time() function) to show the right week. This all worked the whole summer.

Now when I give the code the timestamp 1414360800 (or an other one in week 43) it calculates the next week link one hour wrong!? How is that? Every other week (that I have tested) this is right. What am I not thinking of?

I cannot give you the whole code but this test shows (at least on my computer) the wrong link time.

This timestamp is from week 42 (2014) and gives the right link time
[php]
$test=1413151200 //The timestamp given
$day_of_the_week = strftime(’%w’, $test); //Find out what day it iss in the week
if ($day_of_the_week == 0) $day_of_the_week = 7; //Since in Finland we start the week on monday
$first_day = mktime(0, 0, 0, strftime(’%m’, $test), strftime(’%d’, $test)-$day_of_the_week +1, strftime(’%Y’, $test)); //Make a timestamp for the first day of the week at 00:00
$oneweek = 606024*7; //One week is 7 days x 24 hours x 60 minutes x 60 seconds
$next = $first_day + $oneweek; //Next week link should be these added together
echo $next; //The next timestamp (i.e. link)
echo ‘
’;
echo strftime(’%d-%m-%Y %R’, $first_day);
echo ‘
’;
echo strftime(’%d-%m-%Y %R’, $next);
[/php]
Prints:
1413756000
13-10-2014 00:00
20-10-2014 00:00

This is from week 43 and the next week link i one hour off
[php]
$test=1414360800;
$day_of_the_week = strftime(’%w’, $test);
if ($day_of_the_week == 0) $day_of_the_week = 7;
$first_day = mktime(0, 0, 0, strftime(’%m’, $test), strftime(’%d’, $test)-$day_of_the_week +1, strftime(’%Y’, $test));
$oneweek = 606024*7;
$next = $first_day + $oneweek;
echo $next;
echo ‘
’;
echo strftime(’%d-%m-%Y %R’, $first_day);
echo ‘
’;
echo strftime(’%d-%m-%Y %R’, $next);
[/php]

Gives the print:
1414360800
20-10-2014 00:00
26-10-2014 23:00

Since this is not in the next week the link wont take me there.

What have I done wrong?

I just had a quick scan through this post and the first thing that sprung to mind (as it happens on October 26th) is daylight saving when the clocks move backward by 1 hour.

Let me know how you get on :wink:

Ahh, thanks! I would never have thought of that. :o

Yup, I’ll let you know!

So, I solved the problem by doing this, which in fact is a lot simpler to what I was doing in the first place…

[php]$nextweek = mktime(0, 0, 0, strftime(’%m’, $first_day), strftime(’%d’, $first_day)+7, strftime(’%Y’, $first_day));[/php]

Thank you for pointing out the problem!

//Jonas

You’re welcome, happy to help :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service