给出开始日期如何获得直到当前月份的所有月份?
例如如果给定的日期是2019年1月1日
输出应为:
Jan 2019
Feb 2019
Mar 2019
Apr 2019
May 2019
Jun 2019
Jul 2019
Aug 2019
Sep 2019
Oct 2019
Nov 2019
Dec 2019
Jan 2020
Feb 2020
Mar 2020
以稍微不同的方法解决了它。谢谢大家的提示和回答。
<?php
$startTime = strtotime("1 January 2019");
$startYear = date("Y", $startTime);
$currentYear = date("Y");
$yearDiff = $currentYear - $startYear;
$currentMonth = date("m", time());
for($i=0; $i < intval($currentMonth) + ($yearDiff * 12) ; $i++)
{
$t = strtotime("+". $i . " months", $startTime);
$monthName = date("M", $t) ." ". date('Y', $t);
echo $monthName . "<br/>\n";
}
?>
答案 0 :(得分:3)
走了,希望这项工作
$start = new DateTime('2019-01-01');
$end = new DateTime('2020-03-02');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("F Y") . "<br>\n";
}