2018-03-06 09:55:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class Dates
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get the list of months, given a start date
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 09:55:48 +08:00
|
|
|
* @param Carbon $start_date
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 09:55:48 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-06-20 01:02:06 +08:00
|
|
|
public static function getMonthsList(Carbon $start_date): array
|
2018-03-06 09:55:48 +08:00
|
|
|
{
|
|
|
|
$months = [];
|
|
|
|
$now = date('Y-m');
|
|
|
|
$last_month = $start_date;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$last_value = $last_month->format('Y-m');
|
2018-03-20 09:50:40 +08:00
|
|
|
$months[$last_value] = $last_month->format('Y F');
|
2018-03-06 09:55:48 +08:00
|
|
|
$last_month = $last_month->addMonth();
|
2018-03-20 09:50:40 +08:00
|
|
|
} while ($last_value !== $now);
|
2018-03-06 09:55:48 +08:00
|
|
|
|
|
|
|
return $months;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the start/end dates for a given month/year
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 09:55:48 +08:00
|
|
|
* @param $month YYYY-MM
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 09:55:48 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-06-20 01:02:06 +08:00
|
|
|
public static function getMonthBoundary($month): array
|
2018-03-06 09:55:48 +08:00
|
|
|
{
|
|
|
|
[$year, $month] = explode('-', $month);
|
2019-06-20 01:02:06 +08:00
|
|
|
$days = \cal_days_in_month(CAL_GREGORIAN, $month, $year);
|
2018-03-06 09:55:48 +08:00
|
|
|
|
|
|
|
return [
|
|
|
|
"$year-$month-01",
|
2018-08-27 00:40:04 +08:00
|
|
|
"$year-$month-$days",
|
2018-03-06 09:55:48 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|