如何计算当前月份在php中的天数?

问题描述 投票:0回答:1

我有一个fiddle,我要在其中计算当月的天数。

我在小提琴中使用的代码段是:

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

A行打印:

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]在上面的调试中显示29,这表示当月有29天。

php代码:

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

问题陈述:

我想知道我应该在B行]的php代码中进行哪些更改,以使其在29次]中循环(取决于当月的天数。在此刻,我已经硬编码了29,但是应该通过php variable)来实现。我想根据在[[B行(或30次或31次)中运行for循环29次来计算天数的原因当前月份的天数。 我有一个小提琴,我想在其中计算当月的天数。我在小提琴中使用的代码段是:

php count days
1个回答
1
投票
尝试这个是给出当月的天数

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

© www.soinside.com 2019 - 2024. All rights reserved.