Jump to content

List week days for selected week


knox203

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/104229-list-week-days-for-selected-week/
Share on other sites

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...

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.