I have a fiddle in which I want to calculate the number of days of the current month.
The snippets of code which I have used in the fiddle is:
<?php
$current_month_first_day = new DateTime('first day of this month'); // first day of the current month
$current_month_last_day = date('t'); // last day of the current month
$interval = new DateInterval('P1D');
$period = new DatePeriod($current_month_first_day, $interval, $current_month_last_day - 1);
print_r($period); // Line A
?>
Line A prints:
DatePeriod Object
(
[start] => DateTime Object
(
[date] => 2020-02-01 18:03:30.000268
[timezone_type] => 3
[timezone] => Europe/Amsterdam
)
[current] =>
[end] =>
[interval] => DateInterval Object
(
[y] => 0
[m] => 0
[d] => 1
[h] => 0
[i] => 0
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] =>
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
[recurrences] => 29
[include_start_date] => 1
)
[recurrences]
prints 29
in the debug above which means there are 29 days in the current month.
php code:
<!-- YES/NO START -->
<div class="choose-options">
<h4 style="text-align:center;">Yes/No</h4>
<div class="yes-no-option" style="display:inline-grid;">
<?php for ($i=0; $i<=29; $i++ ) { ?> <!-- I have harcoded 29 but it should be coming through php variable --> <!-- Line B -->
<select name="house_sitting_date_yes_no[]" class="house-yes-no" style="height:24px; margin-bottom:20px;">
<option value="nada">Please choose an option</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<?php } ?>
</div>
</div>
<!-- YES/NO END -->
Problem Statement:
I wondering what changes I should make in the php code at Line B so that it loop through 29 times (depending on the number of days of the current month. At the moment, I have hardcoded 29 but it should be coming through php variable).
The reason why I want to calculate the number of days as I want to run the for loop 29 times at Line B (or 30 times or 31 times) depending on the number of
days current month has.