首页 > 解决方案 > 如何在 php/html 中获取当前和下个月的日期?

问题描述

我有一个如下所示的 php 代码,我想在 php 中显示当前和下个月的日期。下面是我用来显示当前和下个月日期的两个函数。

html/php代码(当月函数和下月函数):

<!-- current month function START -->

<?php
function buildDateSelectionCurrentMonth($month)
{
    # array will hold our months
    $months = [
        str_pad($month, 2, "0", STR_PAD_LEFT),
        str_pad(($month + 1), 2, "0", STR_PAD_LEFT),
    ];

    # will hold our HTML
    $output = '';

    # got our months now, let's iterate over them

    foreach ($months as $index => $month) {
        $year = date('Y');

        // Adjust month and date if necessary:
        if ($month == 13) {
            $month = 1;
            $year++;
        }

        if ($index == 0) {
            # create timestamp for the first of a month
            # then get number of days for that month
            $numberOfDaysMonth = date('t', strtotime($year . '-' . $month . '-01'));

            # we can now loop over the days and fill the output string
            for ($i = 1; $i <= $numberOfDaysMonth; $i++) {
                $output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="class-sitting-date" name="class_sitting_date_current_month[]" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
            }
        }
    }

    return $output;
}

<!-- current month function END -->


<!-- next month function START -->
<?php
function buildDateSelectionNextMonth($month)
{
    # array will hold our months
    $months = [
        str_pad($month, 2, "0", STR_PAD_LEFT),
        str_pad(($month + 1), 2, "0", STR_PAD_LEFT),
    ];

    # will hold our HTML
    $output = '';

    # got our months now, let's iterate over them

    foreach ($months as $index => $month) {
        $year = date('Y');

        // Adjust month and date if necessary:
        if ($month == 13) {
            $month = 1;
            $year++;
        }

        if ($index == 0) {
            # create timestamp for the first of a month
            # then get number of days for that month
            $numberOfDaysMonth = date('t', strtotime($year . '-' . $month . '-01'));

            # we can now loop over the days and fill the output string
            for ($i = 1; $i <= $numberOfDaysMonth; $i++) {
                $output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="class-sitting-date" name="class_sitting_date_next_month[]" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
            }
        }
    }

    return $output;
}

?>
<!-- next month function END -->

html/php 代码(当月日期和下个月日期):

<!-- Current Month Dates START -->
<div class="sitting-days">
    <h4 name="dates-selection" style="text-align:center;"><a name="dates-selection">Select Date</a>
    </h4>
    <?php echo buildDateSelectionCurrentMonth(date('n')); ?>     // Line Y
</div>
<!-- Current Month Dates END -->


<!-- Next Month Dates START -->
<div class="sitting-days" style="margin-left:30px;">
    <h4 style="text-align:center;">Select Date</h4>
    <div class="sitting-days">
        <?php echo buildDateSelectionNextMonth(date('n')+1); ?>   // Line Z
    </div>
</div>
<!-- Next Month Dates END -->
                
                

在 Y 线和 Z 线,其当前显示 11 月和 12 月日期

当当前月份是 9 月时,它将显示 2020 年 9 月的日期和 2020 年 10 月的日期,反之亦然。

问题陈述:

只是为了好玩,在 Line Y 和 Line ZI 进行了以下更改:

在 Y 线:

echo buildDateSelectionCurrentMonth(12);

在 Z 线

echo buildDateSelectionNextMonth(13);

我这样做的原因是我想检查它是否会显示 2021 年 1 月的日期。

我注意到它没有在 Z 线显示 2021 年 1 月的日期,尽管它在 Y 线显示了 2020 年 12 月的日期。

我想知道我需要在上面的 php 代码中进行哪些更改,以便它January 2021 datescurrent month is December.

标签: phphtmldate

解决方案


当您调用buildDateSelectionNextMonth(13)时,$months数组最终包含:

Array
(
    [0] => 13
    [1] => 14
)

您调整月份和日期的代码仅说明 when $month == 13,而不是 14 时:

// Adjust month and date if necessary:
        if ($month == 13) {
            $month = 1;
            $year++;
        }

您还有两个几乎相同的功能:buildDateSelectionCurrentMonth()buildDateSelectionNexMonth(). 这是多余的,只会让事情变得更加困难。更奇怪的是,这些函数创建了一个包含两个月的数组,但由于这一行,它们最终只处理了这两个月中的第一个:

if ($index == 0) {

最好的办法是编写一个函数来输出给定月份的所需日期,如果那个月是 13 号,那么它将假定为明年 1 月:

function buildDateSelectionForMonth($month)
{
    # will hold our HTML
    $output = '';

    $year = date('Y'); // assume current year

    // Adjust month and date if necessary:
    if ($month == 13) {
        $month = 1;
        $year++;
    }

    # create timestamp for the first of a month
    # then get number of days for that month
    $numberOfDaysMonth = date('t', strtotime($year . '-' . $month . '-01'));

    # we can now loop over the days and fill the output string
    for ($i = 1; $i <= $numberOfDaysMonth; $i++) {
        $output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="class-sitting-date" name="class_sitting_date_next_month[]" readonly="readonly" value="' . sprintf( '%04d-%02d-%02d', $year, $month, $i ) . '">
</div>';
    }

    return $output;
}

要获取当前月份的日期:

buildDateSelectionForMonth( date('m') );

要获取下个月的日期:

buildDateSelectionForMonth( date('m') + 1 );

推荐阅读