2018-03-06 09:55:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class Dates
|
|
|
|
{
|
2020-02-12 01:32:41 +08:00
|
|
|
/**
|
|
|
|
* Bitwise operator for setting days of week to integer field
|
|
|
|
*
|
|
|
|
* @param int $datefield initial datefield
|
|
|
|
* @param array $day_enums Array of values from config("enum.days")
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function setDays(int $datefield, array $day_enums): int
|
|
|
|
{
|
|
|
|
foreach ($day_enums as $day) {
|
|
|
|
$datefield |= $day;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $datefield;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bit check if a day exists within a integer bitfield
|
|
|
|
*
|
|
|
|
* @param int $datefield datefield from database
|
|
|
|
* @param int $day_enum Value from config("enum.days")
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function hasDay(int $datefield, int $day_enum): bool
|
|
|
|
{
|
|
|
|
return ($datefield & $day_enum) === $datefield;
|
|
|
|
}
|
|
|
|
|
2018-03-06 09:55:48 +08:00
|
|
|
/**
|
|
|
|
* 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-08-09 00:50:32 +08:00
|
|
|
$days = static::getDaysInMonth($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
|
|
|
];
|
|
|
|
}
|
2019-08-09 00:50:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of days in a month
|
|
|
|
* https://www.php.net/manual/en/function.cal-days-in-month.php#38666
|
|
|
|
*
|
|
|
|
* @param int $month
|
|
|
|
* @param int $year
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function getDaysInMonth($month, $year): int
|
|
|
|
{
|
|
|
|
$month = (int) $month;
|
|
|
|
$year = (int) $year;
|
|
|
|
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
|
|
|
|
}
|
2018-03-06 09:55:48 +08:00
|
|
|
}
|