Skip to content

Instantly share code, notes, and snippets.

@LarryStanley
Created February 28, 2017 05:03
Show Gist options
  • Save LarryStanley/d88f35f14e654c0cc281d67703cf81ce to your computer and use it in GitHub Desktop.
Save LarryStanley/d88f35f14e654c0cc281d67703cf81ce to your computer and use it in GitHub Desktop.
場地查詢
public function getSiteInfo(Request $request) {
$sites = Site::where("site_category_id", $request->site_category_id)->get();
foreach ($sites as $key => $value) {
$sites[$key]->category;
$currentSite = $sites[$key];
$currentRental = $currentSite->currentRental
->where('rental_start_date', '<=', $request->rental_start_date)
->where('rental_end_date', '>=', $request->rental_end_date);
//檢查單一天、連續天
foreach ($currentRental as $index => $rental) {
if ($rental->rental_start_date <= $request->rental_end_date && $request->rental_start_date <= $rental->rental_end_date) {
$sites[$key]->overlap = true;
break;
}
}
//檢查連續週
$dow = $request->rental_week;
$step = 1;
$unit = 'W';
$start = $request->rental_start_date;
$end = $request->rental_end_date;
$start->modify($dow); // Move to first occurence
$end->add(new DateInterval('P1Y')); // Move to 1 year from start
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('D, d M Y'), PHP_EOL;
}
}
return $sites;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment