Created
October 11, 2019 23:42
-
-
Save linkimfly/1547dd0215cbb885a1ff72a9d5d7c7e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Carbon\Carbon; | |
use Carbon\CarbonPeriod; | |
/* | |
|-------------------------------------------------------------------------- | |
| Web Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register web routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| contains the "web" middleware group. Now create something great! | |
| | |
*/ | |
Route::any('/carbonintervaltest/',function(){ | |
$startDate = Carbon::now(); | |
$endDate = Carbon::now()->addDays(2); | |
$dateRange = CarbonPeriod::create($startDate, $endDate); | |
$dates = []; | |
foreach ($dateRange as $dateKey => $date) | |
{ | |
$dates[$dateKey]['date'] = $date->format('Y-m-d'); | |
$dates[$dateKey]['week'] = $date->localeDayOfWeek; | |
$dates[$dateKey]['subdate'] = $date->format('m-d'); | |
$dates[$dateKey]['timelist'] = generateTime($date); | |
} | |
dd($dates); | |
}); | |
function generateTime(Carbon $date) | |
{ | |
$startOfDay = $date->startOfDay(); | |
$eightTen = generateTwoHoursDifference($startOfDay,8); | |
$eightTenPeriod = getPeriod($eightTen); | |
$tenTwelve = generateTwoHoursDifference($startOfDay, 10); | |
$tenTwelvePeriod = getPeriod($tenTwelve); | |
$twelveFourteen = generateTwoHoursDifference($startOfDay, 12); | |
$twelveFourteenPeriod = getPeriod($twelveFourteen); | |
$fourteenSixteen = generateTwoHoursDifference($startOfDay, 14); | |
$fourteenSixteenPeriod = getPeriod($fourteenSixteen); | |
$sixTeenEighteen = generateTwoHoursDifference($startOfDay, 16); | |
$sixTeenEighteenPeriod = getPeriod($sixTeenEighteen); | |
$eighteenTwenty = generateTwoHoursDifference($startOfDay, 18); | |
$eighteenTwentyPeriod = getPeriod($eighteenTwenty); | |
$twentyTwentyTwo = generateTwoHoursDifference($startOfDay, 20); | |
$twentyTwentyTwoPeriod = getPeriod($twentyTwentyTwo); | |
$totalTime = [ | |
$eightTenPeriod,$tenTwelvePeriod, | |
$twelveFourteenPeriod,$fourteenSixteenPeriod, | |
$sixTeenEighteenPeriod,$eighteenTwentyPeriod, | |
$twentyTwentyTwoPeriod]; | |
return $totalTime; | |
} | |
function getPeriod(array $arrayObCarbonObj) | |
{ | |
foreach ($arrayObCarbonObj as $singleCarbonObj) | |
{ | |
$eachHour[] = $singleCarbonObj->format('H'); | |
} | |
$period = implode(' ~ ', $eachHour); | |
$starthour = $eachHour[0]; | |
$endhour = $eachHour[1]; | |
$expired = !$arrayObCarbonObj['timeTwo']->subMinutes(30)->isFuture(); | |
return [ | |
'period' => $period, | |
'starthour' => $starthour, | |
'endhour' => $endhour, | |
'expired' => $expired | |
]; | |
} | |
function generateTwoHoursDifference(Carbon $date,int $start,int $increment = 2) | |
{ | |
$dateFirstObj = (new Carbon)->parse($date->toDateTimeString()); | |
$dateSecondtObj = (new Carbon)->parse($date->toDateTimeString()); | |
$firstTime = $dateFirstObj->addHours($start); | |
$secondTime = $dateSecondtObj->addHours($start+$increment); | |
$allTime = ['timeOne' => $firstTime , 'timeTwo' => $secondTime]; | |
return $allTime; | |
} | |
Route::get('/', static function () { | |
return view('welcome'); | |
}); | |
Auth::routes(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment