显示循环日期列表

时间:2017-07-04 02:25:51

标签: php date

我有这个代码..它将显示当年和第一个月和第二个月的第一天的月份列表

$cur_year = date("Y");
$next_year = $cur_year+1;

for($m=1; $m<=12; ++$m)
{
$monthName=date('F', mktime(0, 0, 0, $m, 1)).'';
$monthNumber=date('m', mktime(0, 0, 0, $m, 1)).'';
$nextmonth=$monthNumber+1;

echo "$monthName<br />";
echo "$cur_year-$monthNumber-01<br />";
echo "$cur_year-$nextmonth-01<br /><br />";
}

我希望像这样显示

January
2017-01-01
2017-02-01

February
2017-02-01
2017-03-01

up to December

December
2017-12-01
2018-01-01

当前的输出

January
2017-01-01
2017-2-01

February
2017-02-01
2017-3-01

up to December

December
2017-12-01
2017-13-01

3 个答案:

答案 0 :(得分:2)

在PHP中使用date()函数

<?php
$cur_year  = date('Y');
$next_year = $cur_year + 1;

for ($m = 1; $m <= 12; ++$m)
{
    $monthName  = date('F', mktime(0, 0, 0, $m, 1));
    $currentMonthText = date('Y-m-d', mktime(0, 0, 0, $m, 1));
    $nextMonthText    = date('Y-m-d', mktime(0, 0, 0, $m + 1, 1));

    echo $monthName, '<br />', PHP_EOL;
    echo $currentMonthText, '<br />' . PHP_EOL;
    echo $nextMonthText, '<br />' . PHP_EOL;
}

答案 1 :(得分:0)

您可以使用&#34; sprintf()&#34;。

您编辑一行。

echo "$cur_year-".sprintf("%02d",$nextmonth)."-01<br /><br />";

答案 2 :(得分:0)

试试这段代码:

<img class="adjust" src="https://unsplash.it/3000/3000">