1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| <?php namespace App\Traits; use Carbon\Carbon;
trait TimeGroupTrait {
public function getDate($start_time, $end_time, $type = 'year') { $start = Carbon::parse($start_time); $end = Carbon::parse($end_time); switch ($type) { case 'year': return $this->timeMonth($start, $end); break; case 'month': return $this->timeWeek($start, $end); break; case 'week': return $this->timeDay($start, $end); break; default: return $this->timeMonth($start, $end); break; } } public function timeDay($startDate, $endDate) { $startDate = $startDate->subDay(); $days = $endDate->diffInDays($startDate) + 1; $currentday = Carbon::now()->day; $currentyear = Carbon::now()->year; for ($i = 0; $i < $days; $i++) { $date = $startDate->copy()->addDays($i); $dayStart = $date->startOfDay(); $dayEnd = $date->copy()->addDay()->startOfDay()->subSecond(); if ($dayStart->month > $currentday && $dayStart->year == $currentyear) { continue; } $time[] = [ 'start_time' => $dayStart->toDateTimeString(), 'end_time' => $dayEnd->toDateTimeString(), 'at_time' => $dayStart->toDateString(), ]; } $date = $startDate->copy()->subDays($i); $dayStart = $date->startOfDay(); $dayEnd = $date->copy()->addDay()->startOfDay()->subSecond(); return $time; } public function timeWeek($startDate, $endDate) { $startDate = $startDate->subWeek(); $startDate = Carbon::parse($startDate); $endDate = Carbon::parse($endDate); $numOfWeeks = $endDate->diffInWeeks($startDate); $currentWeek = Carbon::now()->week; $currentyear = Carbon::now()->year; for ($i = 0; $i <= $numOfWeeks; $i++) { $weekStart = $startDate->copy()->startOfWeek()->addWeeks($i); $weekEnd = $startDate->copy()->endOfWeek()->addWeeks($i); if ($weekStart->week > $currentWeek && $weekStart->year == $currentyear) { continue; } $time[] = [ 'start_time' => $weekStart->toDateTimeString(), 'end_time' => $weekEnd->toDateTimeString(), 'at_time' => $weekStart->format("m.d") . "-" . $weekEnd->format('m.d'), ]; } return $time; } public function timeMonth($startDate, $endDate) { $startDate = $startDate->subMonth(); $startDate = Carbon::parse($startDate); $endDate = Carbon::parse($endDate); $currentMonth = Carbon::now()->month; $currentyear = Carbon::now()->year; $numOfMonths = $endDate->diffInMonths($startDate); for ($i = 0; $i <= $numOfMonths; $i++) { $monthStart = $startDate->copy()->startOfMonth()->addMonths($i); $monthEnd = $startDate->copy()->endOfMonth()->addMonths($i); if ($monthStart->month > $currentMonth && $monthStart->year == $currentyear) { continue; } $time[] = [ 'start_time' => $monthStart->toDateTimeString(), 'end_time' => $monthEnd->toDateTimeString(), 'at_time' => $monthStart->format('Y-m'), ]; } return $time; } }
|