繁体   English   中英

给定开始日期获取当前月份之前的所有月份名称?

[英]Given start date get all the months name until current month?

给定开始日期如何获得本月之前的所有月份?

例如。 如果给定日期是 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";
} 
?>

给你,希望这项工作

$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";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM