Category : php-carbon

I’m trying to return a date parsed to the current server timezone (is stored in UTC) based on the day, the month and the year only. For that reason I have a function that looks like this: private function getFormattedDate(string $stringDate): array { $date = Carbon::createFromFormat( ‘Y-m-d’, $stringDate, config(‘app.timezone’) ); return [ ‘date’ => $date->timestamp, ..

Read more

I write code to check current time between 2 times $start and $end like this: $current = new Carbon(); $start = product::select(‘dateS’)->where(‘id’,$req->id)->first(); $end = product::select(‘dateE’)->where(‘id’,$req->id)->first(); $val= var_dump($current->between($start->dateS, $end->dateE)); It work and when i use dd($val) it will show: bool(false) null or bool(true) null Next, i want to do like this but it only returns else ..

Read more

I have 2 times in table +—————————+——————— | id | start_date | End_date | +———————+————————–+ | 1 | 020-12-14 16:56:0 | 2020-12-16 03:02:00| +——+——————–+——————–+ I use Carbon to check current time is within the start and end times? But it not work. Error: Expected null, string, DateTime or DateTimeInterface $current = new Carbon(); $start = ..

Read more

i would like pass a variable in another function, i use __construct function for it, but my browser return "Unresolvable dependency resolving [Parameter #0 [ $startingAt ]]". Thanks for your help ! My Controller : protected $startingAt; public function __construct($startingAt) { $this->startingAt = $startingAt; } public function index() { return view(‘index’); } public function startingAt() ..

Read more

I am using Laravel Framework 6.16.0 and want to parse a date with carbon "Dec 14 02:04 PM": $fillingDate = "Dec 14 02:04 PM"; $filling_Date = Carbon::parse($fillingDate, ‘UTC’); // result is only a string "Dec 14 02:04 PM" When using the above structure I only get the string back. However, I would like to get ..

Read more

I have to read from an external database, where some columns are in UTC and others are in a different timezone. I know, bad DB design… My model identifies the date columns in $casts, e.g. protected $casts = [ ‘exampleDbUtc’ => ‘datetime’, ‘exampleDbLocal’ => ‘datetime’, ] I get $item->exampleDbUtc Carbon: 2020-11-20 20:30:00.0 Europe/Amsterdam (+01:00) // ..

Read more

I have a function with a bit complicated query for GPS calculation. It works fine but now I added a date parameter which is a received from frontend in format dd.mm.YYYY. Here is my function: public function searchRoutes(Request $request) { $dateFrom = Carbon::createFromFormat(‘d.m.Y H:i:s’, $request->date . "00:00:00"); $dateTo = Carbon::createFromFormat(‘d.m.Y H:i:s’, $request->date . "23:59:59"); $routes ..

Read more

I have a Laravel collection of Carbon dates: $dates = collect([ Carbon::parse(‘2020-09-18’), Carbon::parse(‘2020-09-21’), Carbon::parse(‘2020-09-22’), Carbon::parse(‘2020-10-13’), Carbon::parse(‘2020-10-14’), Carbon::parse(‘2020-10-15’), Carbon::parse(‘2020-11-02’), Carbon::parse(‘2020-11-03’), Carbon::parse(‘2020-11-04’), Carbon::parse(‘2020-11-05′) ]); I want to split these into chunks where the dates are consecutive weekdays. I’ve managed to use chunkWhile to split the collection where the dates are either before or after: $chunks = $dates->chunkWhile(function ..

Read more

I have added a datetime in the database schema: $table->dateTime(‘send_at’)->nullable(); I have set the attribute to a Carbon instance in the seeder: $invoice->send_at = Carbon::now(); When I try to get the type of the attribute inside the controller it returns a string: dd(gettype($data[‘invoices’][0]->send_at)); What is going on? And how can I be sure that it’s ..

Read more

Assume having a Eloquent model called Request with the column next_check autocasted as date (protected $dates = [‘next_check’];). APP_TIMEZONE is initial Europe/Helsinki (+02:00) $ artisan tinker Psy Shell v0.10.4 (PHP 7.4.11 — cli) by Justin Hileman >>> $pr = Request::find(1); >>> $pr->next_check = CarbonCarbon::now(); >>> $pr->save(); >>> Request::find(1)->next_check; => IlluminateSupportCarbon @1603973824 {#2900 date: 2020-10-29 14:17:04.0 ..

Read more

im having trouble mass updating data with tinker and Carbon as it does not iterate through the values input >>> $d = Child::get(‘Birthday’) //1st command [!] Aliasing ‘Child’ to ‘AppChild’ for this Tinker session. => IlluminateDatabaseEloquentCollection {#4195 all: [ AppChild {#4196 Birthday: "2015-03-26", }, AppChild {#4197 Birthday: "2014-08-16", }, AppChild {#4198 Birthday: "2018-02-05", }, AppChild ..

Read more