[WIP] Setup of skeleton
This commit is contained in:
114
app/Repositories/Core/DateRange.php
Normal file
114
app/Repositories/Core/DateRange.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core;
|
||||
|
||||
use \Carbon\Carbon;
|
||||
use \League\Period\Period;
|
||||
|
||||
class DateRange
|
||||
{
|
||||
public static function getPeriodsLastMonth($nb)
|
||||
{
|
||||
$end = static::lastMonth();
|
||||
$begin = $end->copy()->subMonth($nb);
|
||||
return static::getPeriodsbyMonth($begin, $end);
|
||||
}
|
||||
|
||||
public static function getPeriodsLastWeek($nb)
|
||||
{
|
||||
$end = static::lastWeek();
|
||||
$begin = $end->copy()->subWeek($nb);
|
||||
return static::getPeriodsbyWeek($begin, $end);
|
||||
}
|
||||
|
||||
public static function getPeriodsLastDay($nb)
|
||||
{
|
||||
$end = static::lastDay();
|
||||
$begin = $end->copy()->subDay($nb);
|
||||
return static::getPeriodsbyDay($begin, $end);
|
||||
}
|
||||
|
||||
public static function byDay()
|
||||
{
|
||||
return [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()];
|
||||
}
|
||||
|
||||
public static function byWeek()
|
||||
{
|
||||
return [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()];
|
||||
}
|
||||
|
||||
public static function byMonth()
|
||||
{
|
||||
return [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()];
|
||||
}
|
||||
|
||||
public static function byQuarter()
|
||||
{
|
||||
return [Carbon::now()->startOfQuarter(), Carbon::now()->endOfQuarter()];
|
||||
}
|
||||
|
||||
public static function bySemester()
|
||||
{
|
||||
$quarter = Carbon::now()->quarter;
|
||||
switch ($quarter) {
|
||||
case 1:
|
||||
case 2:
|
||||
$date = Carbon::now()->startOfYear();
|
||||
break;
|
||||
case 3:
|
||||
$date = Carbon::now()->startOfQuarter();
|
||||
break;
|
||||
case 4:
|
||||
$date = Carbon::now()->subMonth(3)->startOfQuarter();
|
||||
break;
|
||||
}
|
||||
return [$date, $date->addMonth(6)];
|
||||
}
|
||||
|
||||
public static function byYear()
|
||||
{
|
||||
return [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()];
|
||||
}
|
||||
|
||||
public static function lastMonth()
|
||||
{
|
||||
$start = Carbon::parse('first day of last month');
|
||||
$start->addMonth()->startOfDay();
|
||||
return $start;
|
||||
}
|
||||
|
||||
public static function lastWeek()
|
||||
{
|
||||
return Carbon::parse('last monday');
|
||||
}
|
||||
|
||||
public static function lastDay()
|
||||
{
|
||||
return Carbon::parse('yesterday');
|
||||
}
|
||||
|
||||
public static function getPeriodsbyMonth($begin, $end)
|
||||
{
|
||||
return (static::getPeriods($begin, $end, 'MONTH'));
|
||||
}
|
||||
|
||||
public static function getPeriodsbyWeek($begin, $end)
|
||||
{
|
||||
return (static::getPeriods($begin, $end, 'WEEK'));
|
||||
}
|
||||
|
||||
public static function getPeriodsbyDay($begin, $end)
|
||||
{
|
||||
return (static::getPeriods($begin, $end, 'DAY'));
|
||||
}
|
||||
|
||||
public static function getPeriods($begin, $end, $interval)
|
||||
{
|
||||
$period = new Period($begin, new \DateTime($end));
|
||||
foreach ($period->getDatePeriod("1 $interval") as $day) {
|
||||
$daterange[] = Period::createFromDuration($day->format('Y-m-d H:i:s'), "1 $interval");
|
||||
}
|
||||
return ($daterange);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user