function scopeTourScheduleBetween($q, \Carbon\Carbon $date1 = null, \Carbon\Carbon $date2 = null) { //////////////////////////////////////////////////// // Compare date1 & date2 for which one is earlier // //////////////////////////////////////////////////// if ($date1 && $date2) { if ($date1->gt($date2)) { $tmp = $date2; $date2 = $date1; $date1 = $tmp; } } /////////////// // Run Query // /////////////// if ($date1 && $date2) { $q->where(function ($q) use($date1, $date2, $budget_query) { $q->where(function ($q) use($date1, $date2, $budget_query) { // No departure until $query['schedules']['$elemMatch']['departure_until'] = ''; $query['schedules']['$elemMatch']['departure'] = ['$gte' => $date1->format('Y-m-d'), '$lte' => $date2->format('Y-m-d')]; $q->whereRaw($query); })->orWhere(function ($q) use($date1, $date2, $budget_query) { // with departure until $query['schedules']['$elemMatch']['departure_until'] = ['$gte' => $date1->format('Y-m-d')]; $query['schedules']['$elemMatch']['departure'] = ['$lte' => $date2->format('Y-m-d')]; $q->whereRaw($query); }); }); } elseif ($date1) { $q->where(function ($q) use($date1, $budget_query) { $q->where(function ($q) use($date1, $budget_query) { // No departure until $query['schedules']['$elemMatch']['departure_until'] = ''; $query['schedules']['$elemMatch']['departure'] = ['$gte' => $date1->format('Y-m-d')]; $q->whereRaw($query); })->orWhere(function ($q) use($date1, $budget_query) { // with departure until $query['schedules']['$elemMatch']['departure_until'] = ['$gte' => $date1->format('Y-m-d')]; $q->whereRaw($query); }); }); } elseif ($date2) { $q->where(function ($q) use($date1, $budget_query) { $q->where(function ($q) use($date1, $budget_query) { // No departure until $query['schedules']['$elemMatch']['departure_until'] = ''; $query['schedules']['$elemMatch']['departure'] = ['$lte' => $date2->format('Y-m-d')]; $q->whereRaw($query); })->orWhere(function ($q) use($date1, $budget_query) { // with departure until $query['schedules']['$elemMatch']['departure_until'] = ['$lte' => $date2->format('Y-m-d')]; $q->whereRaw($query); }); }); } }
public function interval($start, $end, $interval) { $result = array(); $date = new Carbon($start); $endDate = new Carbon($end); while ($endDate->gt($date)) { $date->modify($interval); $result[] = $date->copy(); } return $result; }
public function funkySinceDate() { $date = new Carbon($this->customer_since); if ($date->gt(Carbon::create(2009))) { return $date->year; } elseif ($date->between(Carbon::create(2000), Carbon::create(2009))) { return 'Oh-' . $date->year % 10; } elseif ($date->between(Carbon::create(1990), Carbon::create(1999))) { return 'ninety-' . $date->year % 10; } elseif ($date->between(Carbon::create(1980), Carbon::create(1989))) { return 'eighty-' . $date->year % 10; } elseif ($date->between(Carbon::create(1970), Carbon::create(1979))) { return 'UNIX-' . $date->year % 10; } else { return 'forever'; } }
public function validateAfterField($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'after_field'); $value2 = array_get($this->data, $parameters[0]); // make them Carbon dates if (!$value instanceof Carbon) { if (strtotime($value) === false) { return false; } $value = new Carbon($value); } if (!$value2 instanceof Carbon) { if (strtotime($value2) === false) { return false; } $value2 = new Carbon($value2); } return $value->gt($value2); }
public function checkPurchasability(Carbon $now = null) { $this->checkForQuantity(); if (!is_null($this->purchasable) && !$this->purchasable) { throw new NotForSaleException($this->getDesignation(), 2); } if (!is_null($this->min_purch_quantity) && $this->quantity < $this->min_purch_quantity) { throw new PurchaseQuantityException($this->getDesignation(), $this->min_purch_quantity, 1); } if (!is_null($this->max_purch_quantity) && $this->quantity > $this->max_purch_quantity) { throw new PurchaseQuantityException($this->getDesignation(), $this->max_purch_quantity, 2); } if (is_null($now)) { $now = new Carbon('now'); } if (!is_null($this->start_time) & !is_null($this->end_time)) { if (!$now->between($this->start_time, $this->end_time)) { throw new OutsideSaleTimeException($this->getDesignation(), $this->start_time, $this->end_time, 1); } } else { if (!is_null($this->start_time) & is_null($this->end_time)) { if ($now->lt($this->start_time)) { throw new OutsideSaleTimeException($this->getDesignation(), $this->start_time, null, 2); } } else { if (is_null($this->start_time) & !is_null($this->end_time)) { if ($now->gt($this->end_time)) { throw new OutsideSaleTimeException($this->getDesignation(), $now, $this->end_time, 3); } } } } if ($this->isSoldOut()) { throw new NotForSaleException($this->getDesignation(), 1); } return true; }
/** * Return a DateTime from a date string and a time string * * @param $date * @param $time * @return Carbon */ public static function toDt($date, $time) { switch ($date) { case "Aujourd'hui": $dt = Carbon::today(); break; case "Hier": $dt = Carbon::yesterday(); break; default: list($day, $month) = preg_split('/\\s/', $date); $dt = new Carbon(); $dt->day($day); $dt->month(self::getMonthNumber($month)); } // Set up the time list($hour, $minute) = preg_split('/:/', $time); $dt->hour = $hour; $dt->minute = $minute; if ($dt->gt(Carbon::tomorrow()->subSecond())) { $dt->year--; } return $dt->format('Y-m-d H:m'); }
/** * Determines if the instance is between two others * * @param Carbon $dt1 * @param Carbon $dt2 * @param boolean $equal Indicates if a > and < comparison should be used or <= or >= * * @return boolean */ public function between(Carbon $dt1, Carbon $dt2, $equal = true) { if ($dt1->gt($dt2)) { $temp = $dt1; $dt1 = $dt2; $dt2 = $temp; } if ($equal) { return $this->gte($dt1) && $this->lte($dt2); } else { return $this->gt($dt1) && $this->lt($dt2); } }
public function contains(Carbon $date_time) { return $date_time->gt($this->after) && $date_time->lt($this->before); }
/** * @param $data * @return array */ private function buildPayload($data) { $payload = ['id' => (int) $data[0], 'name' => trim($data[1]), 'account_type_id' => (int) $data[2], 'account_status_id' => (int) $data[3], 'contact_name' => trim($data[16])]; $unformattedAddress = ['line1' => trim($data[7]), 'line2' => trim($data[8]), 'city' => trim($data[9]), 'state' => trim($data[10]), 'county' => trim($data[11]), 'zip' => trim($data[12]), 'country' => trim($data[13]), 'latitude' => trim($data[14]), 'longitude' => trim($data[15])]; $formattedAddress = $this->addressFormatter->formatAddress($unformattedAddress, false); $payload = array_merge($payload, $formattedAddress); /** * We don't do a ton of validation here, as the API call will fail if this data is invalid anyway. */ if (trim($data[4])) { $payload['account_groups'] = explode(",", trim($data[4])); } if (trim($data[5])) { $payload['sub_accounts'] = explode(",", trim($data[5])); } if (trim($data[6])) { $carbon = new Carbon($data[6]); $now = Carbon::now(); if ($carbon->gt($now)) { $payload['next_bill_date'] = trim($carbon->toDateString()); } } if (trim($data[17])) { $payload['role'] = trim($data[17]); } if (trim($data[18])) { $payload['email_address'] = trim($data[18]); } if (trim($data[19])) { $payload['email_message_categories'] = explode(",", trim($data[19])); } else { $payload['email_message_categories'] = []; } $phoneNumbers = []; if (trim($data[20])) { $phoneNumbers['work'] = ['number' => trim(preg_replace("/[^0-9]/", "", $data[20])), 'extension' => trim($data[21])]; } if (trim($data[22])) { $phoneNumbers['home'] = ['number' => trim(preg_replace("/[^0-9]/", "", $data[22])), 'extension' => null]; } if (trim($data[23])) { $phoneNumbers['mobile'] = ['number' => trim(preg_replace("/[^0-9]/", "", $data[23])), 'extension' => null]; } if (trim($data[24])) { $phoneNumbers['fax'] = ['number' => trim(preg_replace("/[^0-9]/", "", $data[24])), 'extension' => null]; } if (count($phoneNumbers) > 0) { $payload['phone_numbers'] = $phoneNumbers; } return $payload; }
/** * {@inheritdoc} */ public function hasExpired() { $now = new Carbon(); return $now->gt($this->expiresAt()); }