Jeff_Kugener Posted May 27, 2013 Share Posted May 27, 2013 Hello guys, I have a problem with the function (strtotime) So the function &week post the week count, for example, the week 1, week 2, week 3, week 4, week 5, week 6 and when the week 6 is over then jump back to the week 1.Example: http://dev.prodesign.lu/week/home.phpHowever, he is here on week 5 and yesterday he stood on the week 0 Actually, it should in turn go 1 - 2 - 3 - 4 - 5 - 6 Here my code: <?php$week = (strtotime(date('Y-m-d 00:00:00',time())) / 7 * 86400) % 6;switch($week) {case 1:echo "<b>Woche 1</b><br><br>Montag Woche $week<br />Dienstag Woche $week<br />Mittwoch Woche $week<br />Donnerstag Woche $week<br />Freitag Woche $week<br />Samstag Woche $week<br />Sonntag Woche $week<br />";break;//case 2:echo "<b>Woche 2</b><br><br>Montag Woche $week<br />Dienstag Woche $week<br />Mittwoch Woche $week<br />Donnerstag Woche $week<br />Freitag Woche $week<br />Samstag Woche $week<br />Sonntag Woche $week<br />";break;//case 3:echo "<b>Woche 3</b><br><br>Montag Woche $week<br />Dienstag Woche $week<br />Mittwoch Woche $week<br />Donnerstag Woche $week<br />Freitag Woche $week<br />Samstag Woche $week<br />Sonntag Woche $week<br />";break;//case 4:echo "<b>Woche 4</b><br><br>Montag Woche $week<br />Dienstag Woche $week<br />Mittwoch Woche $week<br />Donnerstag Woche $week<br />Freitag Woche $week<br />Samstag Woche $week<br />Sonntag Woche $week<br />";break;//case 5:echo "<b>Woche 5</b><br><br>Montag Woche $week<br />Dienstag Woche $week<br />Mittwoch Woche $week<br />Donnerstag Woche $week<br />Freitag Woche $week<br />Samstag Woche $week<br />Sonntag Woche $week<br />";break;//case 6:echo "<b>Woche 6</b><br><br>Montag Woche $week<br />Dienstag Woche $week<br />Mittwoch Woche $week<br />Donnerstag Woche $week<br />Freitag Woche $week<br />Samstag Woche $week<br />Sonntag Woche $week<br />";break;}?> Thx for help Quote Link to comment https://forums.phpfreaks.com/topic/278433-i-have-some-problem-with-strtotime/ Share on other sites More sharing options...
Solution Jessica Posted May 27, 2013 Solution Share Posted May 27, 2013 (edited) A. Use Code tags. (The <> button) B. This is WAY TOO MUCH CODE. $week = (strtotime(date('Y-m-d 00:00:00',time())) / 7 * 86400) % 6;You're creating the current timestamp, converting it to a string for midnight of this day, then converting that BACK into a timestamp. Then dividing it by 7 and multiplying it by 86400 (So you're trying to get a week's worth of seconds here, you already did some math), then getting the remainder when you divide it by 6. WHY ARE YOU DOING THIS INSANE THING? You need to go read the documentation for date() because you are using it way wrong. There is a format parameter to get the current week. It will be way more accurate than your method. C. You also need to read on what modulo does. Any number % 6 can NEVER produce/equal 6. D. You're repeating the same thing over and over regardless of what week it is. You're doing the opposite of what programming should be - you've made way more work for yourself. <?php $week = (date('W') % 6)+1; echo "<b>Woche {$week}</b><br> <br> Montag Woche {$week}<br /> Dienstag Woche {$week}<br /> Mittwoch Woche {$week}<br /> Donnerstag Woche {$week}<br /> Freitag Woche {$week}<br /> Samstag Woche {$week}<br /> Sonntag Woche {$week}<br />"; Or even: $days = array('Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'); $week = (date('W') % 6)+1; echo "<b>Woche {$week}</b><br><br>"; foreach($days AS $day){ echo "{$day} Woche {$week}<br>"; }It depends what you intend to do next really. Edited May 27, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/278433-i-have-some-problem-with-strtotime/#findComment-1432506 Share on other sites More sharing options...
Jeff_Kugener Posted May 27, 2013 Author Share Posted May 27, 2013 A. Use Code tags. (The <> button) B. This is WAY TOO MUCH CODE. $week = (strtotime(date('Y-m-d 00:00:00',time())) / 7 * 86400) % 6;You're creating the current timestamp, converting it to a string for midnight of this day, then converting that BACK into a timestamp. Then dividing it by 7 and multiplying it by 86400 (So you're trying to get a week's worth of seconds here, you already did some math), then getting the remainder when you divide it by 6. WHY ARE YOU DOING THIS INSANE THING? You need to go read the documentation for date() because you are using it way wrong. There is a format parameter to get the current week. It will be way more accurate than your method. C. You also need to read on what modulo does. Any number % 6 can NEVER produce/equal 6. Do you have an example? Quote Link to comment https://forums.phpfreaks.com/topic/278433-i-have-some-problem-with-strtotime/#findComment-1432511 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 OMFG. Do I have an example. Well for one thing all I told you do to was READ THE DOCUMENTATION. So the EXAMPLE is at php.net This is why I hate when I write code for other people. You're not even willing to do the work yourself to learn what you did wrong. Quote Link to comment https://forums.phpfreaks.com/topic/278433-i-have-some-problem-with-strtotime/#findComment-1432512 Share on other sites More sharing options...
Jeff_Kugener Posted May 27, 2013 Author Share Posted May 27, 2013 OMFG. Do I have an example. Well for one thing all I told you do to was READ THE DOCUMENTATION. So the EXAMPLE is at php.net This is why I hate when I write code for other people. You're not even willing to do the work yourself to learn what you did wrong. Okay, thx i look at php.net. Quote Link to comment https://forums.phpfreaks.com/topic/278433-i-have-some-problem-with-strtotime/#findComment-1432513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.