You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Helper;
use Carbon\Carbon;
class Time
{
public static function getDateList($start, $end)
{
$startTime = strtotime($start . ' 00:00:00');
$endTime = strtotime($end . ' 00:00:00');
$dateList = [];
while ($startTime <= $endTime) {
$dateList[] = date('Y-m-d', $startTime);
$startTime += 24 * 60 * 60;
}
return $dateList;
}
public static function dateTimeRange(array $dateRange, $withTime = true)
{
if (!isset($dateRange[0]) || !isset($dateRange[1]) || count($dateRange) != 2) {
throw new \Exception('dateTimeRange param is error');
}
$dateRangeWithTime = false;
if (
self::checkFormatTime($dateRange[0], 'Y-m-d') &&
self::checkFormatTime($dateRange[1], 'Y-m-d')
) {
$dateRangeWithTime = false;
} elseif (
self::checkFormatTime($dateRange[0], 'Y-m-d H:i:s') &&
self::checkFormatTime($dateRange[1], 'Y-m-d H:i:s')
) {
$dateRangeWithTime = true;
} else {
throw new \Exception('dateTimeRange format is error');
}
if ($withTime && !$dateRangeWithTime) {
return [$dateRange[0] . ' 00:00:00', $dateRange[1] . ' 23:59:59'];
} elseif (!$withTime && $dateRangeWithTime) {
return [date('Y-m-d', strtotime($dateRange[0])), date('Y-m-d', strtotime($dateRange[1]))];
}
return $dateRange;
}
public static function checkFormatTime($timeString, $format = 'Y-m-d H:i:s') {
$timestamp = strtotime($timeString);
if (!$timestamp) {
return false;
}
if(date($format, $timestamp) === $timeString) {
return true;
}
return false;
}
public static function getMonthLastDate($month)
{
$firstDate = $month . '-01';
return date('Y-m-d', strtotime($firstDate . ' +1 month -1 day'));
}
public static function getMonthDateRange($month)
{
$carbon = Carbon::parse($month);
return [
$carbon->firstOfMonth()->format('Y-m-d'),
$carbon->lastOfMonth()->format('Y-m-d')
];
}
public static function getLastMonth()
{
return Carbon::now()->subMonth()->format('Y-m');
}
public static function getNextMonth()
{
return Carbon::now()->addMonth()->format('Y-m');
}
}