I have a list of check boxes containing dates. Here's a preview of what I have so far:
// This code gets all the Sunday's in a month:
function getSundaysForTheMonth($y, $m)
{
return new DatePeriod(
new DateTime("first sunday of $y-$m"),
DateInterval::createFromDateString('next sunday'),
new DateTime("last day of $y-$m 23:59:59")
);
}
This is how I'm displaying it:
// Get current Year and Month
$currentYear = date('Y');
$currentMonth = date('m');
// Get month name
$beginMonthName = date("F", mktime(0, 0, 0, $currentMonth, 10));
echo "Select which Sunday(s) of the month of ". $beginMonthName . ". $currentYear . " ": \n<BR>";
$i=0;
// Display all Sundays for 3 months
foreach (getSundaysForTheMonth($currentYear, $currentMonth) as $sunday) {
$thisSunday = $sunday->format("m - d - Y");
echo "<input type=\"checkbox\" name=\"date".$i."\" value=\".$thisSunday. \">".$thisSunday."<BR>";
$i++;
}
The idea was to do it with a foreach instead of this way:
<input type="checkbox" name="date0" value="2020-04-12 ">2020-04-12<BR>
<input type="checkbox" name="date1" value="2020-04-12 ">2020-04-19<BR>
<input type="checkbox" name="date2" value="2020-04-12 ">2020-04-26<BR>
<input type="checkbox" name="date3" value="2020-04-12 ">2020-03-03<BR>
Now I'm trying to get those values. I'm thinking the code should look similar to this but a bit different, since the input name has different names (date0, date1, date2, ...).
<?php
if (isset($_POST['date'])) {
foreach ($date as $sunday){
echo $sunday."<br />";
// Store $sunday in an array
}
} else {
echo "No selections";
}
?>
Any ideas on how I can make this work?
My goal is to store it in an array, in which will be put into a database.
Thanks.