knox203 Posted May 5, 2008 Share Posted May 5, 2008 Hello, I need to figure out how I can list out the 7 days of a selected week (in YYYY-MM-DD format). It seems like it should not be something too difficult to do, but I am having a hard time wrapping my head around getting it to work. So far I don't really have any code to do this, I tried modifying a couple code snippets I found on the net, but with no success. Basically a person will select a month from a drop down, then a week number for that month (1, 2, 3, 4) from another drop down. Once submitted it will return the 7 days for that week. Please help, thanks! - Adam Quote Link to comment https://forums.phpfreaks.com/topic/104229-list-week-days-for-selected-week/ Share on other sites More sharing options...
revraz Posted May 5, 2008 Share Posted May 5, 2008 What about months with 5 weeks? When does a week start, Sunday or Monday? Quote Link to comment https://forums.phpfreaks.com/topic/104229-list-week-days-for-selected-week/#findComment-533608 Share on other sites More sharing options...
Barand Posted May 5, 2008 Share Posted May 5, 2008 If the month starts on Wednesday, which 7 days do you want? Quote Link to comment https://forums.phpfreaks.com/topic/104229-list-week-days-for-selected-week/#findComment-533609 Share on other sites More sharing options...
rarebit Posted May 5, 2008 Share Posted May 5, 2008 Take a date, print it and then add a day to it. Maybe a bit of mod 7. If need be use checkdate to see if its valid, but I think by taking a time and adding 86400 to it to increment by a day and using normal date should do it... Quote Link to comment https://forums.phpfreaks.com/topic/104229-list-week-days-for-selected-week/#findComment-533611 Share on other sites More sharing options...
Daniel0 Posted May 5, 2008 Share Posted May 5, 2008 Here is something for you to work with: <?php // Find weeks in month: $month = 5; $year = 2008; $firstDay = mktime(0, 0, 0, $month, 1, $year); $numDays = date('t', $firstDay); $weeks = array(); $i = 1; do { $day = mktime(0, 0, 0, $month, $i, $year); $weeks[] = date('W', $day); $i += 7; } while ($i <= $numDays); print_r($weeks); echo "<hr />"; //--------------------------- // Find days in week: $year = 2008; $week = 18; $firstDay = strtotime('+' . $week * 7 . ' days', mktime(0, 0, 0, 1, 1, $year)); $month = date('n', $firstDay); $firstDayNum = date('j', $firstDay); $lastDayNum = $firstDayNum + 6; for ($i = $firstDayNum; $i <= $lastDayNum; $i++) { echo date('l, j', mktime(0, 0, 0, $month, $i, $year)) . '<br />'; } ?> I haven't checked that the output is correct, but in my head it should work perfectly. Edit: Random useless fact: This is the first time ever I have seen the use for a do-while loop... Quote Link to comment https://forums.phpfreaks.com/topic/104229-list-week-days-for-selected-week/#findComment-533619 Share on other sites More sharing options...
knox203 Posted May 5, 2008 Author Share Posted May 5, 2008 Thanks so much for the replies guys! Daniel, I'm sorry to change things up after you wrote me some code, but I actually now need the Monday-Friday for each week of each month. Is there a way to use the date() function to list the Mondays for the selected month, then I could just add 4 days to that? ... Maybe it's not that easy? Thanks! - Adam Quote Link to comment https://forums.phpfreaks.com/topic/104229-list-week-days-for-selected-week/#findComment-533623 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.