获取两个日期之间的所有月份
getting all months between 2 dates
我创建了一个函数,returns 一个包含每个月的数组,从提供的碳日期开始到当前日期结束。
尽管该函数正在执行它应该执行的操作,但它看起来很丑陋。很明显,我的编程技能还没有达到应有的水平。当然必须有更好的方法来实现我想要的。
我的代码如下所示:
class DateUtilities {
protected $months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
public function getMonthListFromDate(Carbon $date)
{
$monthArray = array();
$today = Carbon::today();
$currentYear = $today->copy()->format('Y');
$currentMonth = strtolower($today->copy()->format('F'));
$startYear = $date->copy()->format('Y');
$startMonth = strtolower($date->copy()->format('F'));
for($i = $startYear; $i <= $currentYear; $i ++) {
foreach($this->months as $monthIndex => $month) {
if (($monthIndex >= array_search($startMonth, $this->months) && $i == $startYear) ||
($monthIndex <= array_search($currentMonth, $this->months) && $i == $currentYear) ||
($i != $startYear && $i != $currentYear)) {
$formattedMonthIndex = ($monthIndex + 1);
if($formattedMonthIndex < 10) {
$monthArray['0' . $formattedMonthIndex . '-' . $i] = $month . ' ' . $i;
} else {
$monthArray[$formattedMonthIndex . '-' . $i] = $month . ' ' . $i;
}
}
}
}
return $monthArray;
}
}
结果是:
array:25 [▼
"03-2013" => "march 2013"
"04-2013" => "april 2013"
"05-2013" => "may 2013"
"06-2013" => "june 2013"
"07-2013" => "july 2013"
"08-2013" => "august 2013"
"09-2013" => "september 2013"
"10-2013" => "october 2013"
"11-2013" => "november 2013"
"12-2013" => "december 2013"
"01-2014" => "january 2014"
"02-2014" => "february 2014"
"03-2014" => "march 2014"
"04-2014" => "april 2014"
"05-2014" => "may 2014"
"06-2014" => "june 2014"
"07-2014" => "july 2014"
"08-2014" => "august 2014"
"09-2014" => "september 2014"
"10-2014" => "october 2014"
"11-2014" => "november 2014"
"12-2014" => "december 2014"
"01-2015" => "january 2015"
"02-2015" => "february 2015"
"03-2015" => "march 2015"
]
谁能帮我改进这段代码?
编辑:
在得到了很好的提示之后,我得到了以下结果:
class DateUtilities {
public function getMonthListFromDate(Carbon $start)
{
$start = $start->startOfMonth();
$end = Carbon::today()->startOfMonth();
do
{
$months[$start->format('m-Y')] = $start->format('F Y');
} while ($start->addMonth() <= $end);
return $months;
}
}
谢谢大家的帮助!!
使用 DateTime 可以轻松实现这一点。
- 为每个开始日期和结束日期创建一个日期时间对象
- 设置1个月的间隔
- 获取一组开始日期和结束日期之间间隔1个月的日期
示例:
public function getMonthListFromDate(Carbon $date)
{
$start = new DateTime(); // Today date
$end = new DateTime($date->toDateTimeString()); // Create a datetime object from your Carbon object
$interval = DateInterval::createFromDateString('1 month'); // 1 month interval
$period = new DatePeriod($start, $interval, $end); // Get a set of date beetween the 2 period
$months = array();
foreach ($period as $dt) {
$months[] = $dt->format("F Y");
}
return $months;
}
查看实际效果:http://3v4l.org/smS3N
这是一个更短更简单的示例。您可以轻松地使其适应 Carbon class 的约定:
$start = strtotime('last month', strtotime('2013-03'));
$now = time();
while(($start = strtotime('next month', $start)) <= $now) {
$result[date('m-Y', $start)] = strtolower(date('F Y', $start));
}
晚会晚了,但值得补充的是 Carbon 已经涵盖了这一点。使用 CarbonPeriod 会将整个 class 减少到 ...
use Carbon\Carbon;
use Carbon\CarbonPeriod;
class DateUtilities
{
public function getMonthListFromDate(Carbon $start)
{
foreach (CarbonPeriod::create($start, '1 month', Carbon::today()) as $month) {
$months[$month->format('m-Y')] = $month->format('F Y');
}
return $months;
}
}
while ($start->lte($end)) {
$months[$start->format('m-Y')] = $start->format('F Y');
$start->addMonth();
}
return $months;
使用 CarbonPeriod class 做同样的事情
$period = \Carbon\CarbonPeriod::create('2018-06-01', '1 month', '2019-06-01');
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
我创建了一个函数,returns 一个包含每个月的数组,从提供的碳日期开始到当前日期结束。
尽管该函数正在执行它应该执行的操作,但它看起来很丑陋。很明显,我的编程技能还没有达到应有的水平。当然必须有更好的方法来实现我想要的。
我的代码如下所示:
class DateUtilities {
protected $months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
public function getMonthListFromDate(Carbon $date)
{
$monthArray = array();
$today = Carbon::today();
$currentYear = $today->copy()->format('Y');
$currentMonth = strtolower($today->copy()->format('F'));
$startYear = $date->copy()->format('Y');
$startMonth = strtolower($date->copy()->format('F'));
for($i = $startYear; $i <= $currentYear; $i ++) {
foreach($this->months as $monthIndex => $month) {
if (($monthIndex >= array_search($startMonth, $this->months) && $i == $startYear) ||
($monthIndex <= array_search($currentMonth, $this->months) && $i == $currentYear) ||
($i != $startYear && $i != $currentYear)) {
$formattedMonthIndex = ($monthIndex + 1);
if($formattedMonthIndex < 10) {
$monthArray['0' . $formattedMonthIndex . '-' . $i] = $month . ' ' . $i;
} else {
$monthArray[$formattedMonthIndex . '-' . $i] = $month . ' ' . $i;
}
}
}
}
return $monthArray;
}
}
结果是:
array:25 [▼
"03-2013" => "march 2013"
"04-2013" => "april 2013"
"05-2013" => "may 2013"
"06-2013" => "june 2013"
"07-2013" => "july 2013"
"08-2013" => "august 2013"
"09-2013" => "september 2013"
"10-2013" => "october 2013"
"11-2013" => "november 2013"
"12-2013" => "december 2013"
"01-2014" => "january 2014"
"02-2014" => "february 2014"
"03-2014" => "march 2014"
"04-2014" => "april 2014"
"05-2014" => "may 2014"
"06-2014" => "june 2014"
"07-2014" => "july 2014"
"08-2014" => "august 2014"
"09-2014" => "september 2014"
"10-2014" => "october 2014"
"11-2014" => "november 2014"
"12-2014" => "december 2014"
"01-2015" => "january 2015"
"02-2015" => "february 2015"
"03-2015" => "march 2015"
]
谁能帮我改进这段代码?
编辑:
在得到了很好的提示之后,我得到了以下结果:
class DateUtilities {
public function getMonthListFromDate(Carbon $start)
{
$start = $start->startOfMonth();
$end = Carbon::today()->startOfMonth();
do
{
$months[$start->format('m-Y')] = $start->format('F Y');
} while ($start->addMonth() <= $end);
return $months;
}
}
谢谢大家的帮助!!
使用 DateTime 可以轻松实现这一点。
- 为每个开始日期和结束日期创建一个日期时间对象
- 设置1个月的间隔
- 获取一组开始日期和结束日期之间间隔1个月的日期
示例:
public function getMonthListFromDate(Carbon $date)
{
$start = new DateTime(); // Today date
$end = new DateTime($date->toDateTimeString()); // Create a datetime object from your Carbon object
$interval = DateInterval::createFromDateString('1 month'); // 1 month interval
$period = new DatePeriod($start, $interval, $end); // Get a set of date beetween the 2 period
$months = array();
foreach ($period as $dt) {
$months[] = $dt->format("F Y");
}
return $months;
}
查看实际效果:http://3v4l.org/smS3N
这是一个更短更简单的示例。您可以轻松地使其适应 Carbon class 的约定:
$start = strtotime('last month', strtotime('2013-03'));
$now = time();
while(($start = strtotime('next month', $start)) <= $now) {
$result[date('m-Y', $start)] = strtolower(date('F Y', $start));
}
晚会晚了,但值得补充的是 Carbon 已经涵盖了这一点。使用 CarbonPeriod 会将整个 class 减少到 ...
use Carbon\Carbon;
use Carbon\CarbonPeriod;
class DateUtilities
{
public function getMonthListFromDate(Carbon $start)
{
foreach (CarbonPeriod::create($start, '1 month', Carbon::today()) as $month) {
$months[$month->format('m-Y')] = $month->format('F Y');
}
return $months;
}
}
while ($start->lte($end)) {
$months[$start->format('m-Y')] = $start->format('F Y');
$start->addMonth();
}
return $months;
使用 CarbonPeriod class 做同样的事情
$period = \Carbon\CarbonPeriod::create('2018-06-01', '1 month', '2019-06-01');
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}