How to print selected number of week in PHP

✔ Recommended Answer

If you want to loop through a php list with foreach, and use both keys and values in your code, I recommend to use both "key" and "value" tags in creation of foreach.

<select name="day" class="form-select">   <?php        /*---- Create your foreach with keys and values in here ------*/            foreach ($daysOfWeek as $key => $value) {                /*---- Echo in here (with testing values) ----*/                    echo "<option";                        if ($value == $day) { // "$day" is numeric I assume                            echo 'selected="selected"';                        }                    echo " value='" . $value . "'>" . $key . "</option>";            }   ?></select>

Source: stackoverflow.com

Answered By: JamalThaBoss

Method #2

To print a selected number of weeks in PHP, you can use the date() function along with the strtotime() function. Here's an example code that prints the dates of the next 4 weeks:

php
<?php $weeks = 4; // number of weeks to print for ($i = 0; $i < $weeks; $i++) { $start = strtotime('+'.$i.' week'); $end = strtotime('+'.$i.' week +6 days'); echo 'Week '.($i+1).': '.date('M j, Y', $start).' - '.date('M j, Y', $end).'<br>'; } ?>

This code uses a for loop to iterate over the specified number of weeks. For each week, it calculates the start and end dates using the strtotime() function with a relative date string. Then it formats the dates using the date() function and prints them along with the week number.

You can adjust the $weeks variable to change the number of weeks to print, and modify the date format string in the date() function to display the dates in a different format if desired.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755