-1

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.

flash
  • 1,455
  • 11
  • 61
  • 132
  • 1
    You do know that php already has that built in, right? You can use `t` on the [date function](https://www.php.net/manual/en/function.date.php), which will also work on a DateTime object. https://3v4l.org/QT7dK – aynber Feb 04 '20 at 17:56
  • https://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date – Felippe Duarte Feb 04 '20 at 17:57
  • You could just use `foreach()` over the `$period` value you get from your first script. – Nigel Ren Feb 04 '20 at 17:58
  • 1
    @RiggsFolly It works, thanks. – flash Feb 04 '20 at 18:01
  • @NigelRen I am wondering if you can let me know in an answer how can we do that through `foreach()` loop over the `$period`. – flash Feb 04 '20 at 18:02
  • @flash was answering when the question was closed. Effectively the question can be rephrased as "*How to iterate over each day including the last day of the current month with DatePeriod*" to reopen it. But you can use https://3v4l.org/ffgBt for an example and explanation. – Will B. Feb 04 '20 at 18:34

1 Answers1

0

try this it is give number of days in current month

echo cal_days_in_month(CAL_GREGORIAN, date('m', time()), date('Y', time()));

OR

echo date('t');