php-carbon

Remove 0, if return 0 year or 0 month DateFormat

非 Y 不嫁゛ 提交于 2019-12-24 09:51:07
问题 $createdTime = Carbon::parse($specialist->created_at) ->diff(Carbon::now()) ->format('%y yr, %m mo and %d days'); This returns 0 yr, 0 mo and 8 days . I want to return only 8 days . Or if 1 yr, 0 mo and 8 days , return 1 yr and 8 days 回答1: Since diff() returns a DateInterval instance you could run conditional checks to get your expected result. $createdTime = Carbon::parse($specialist->created_at) ->diff(Carbon::now()); $final_string = ''; $year = ''; $month = ''; $days = ''; if ($createdTime

Using PHP Carbon, remove time in seconds where two time periods overlap

不问归期 提交于 2019-12-24 07:49:41
问题 An outage may range from a couple of minutes extending to multiple days. I store this in an outages table that has start date/time and end date/time. On saving an outage record, the duration in seconds is calculated using Carbon. $outage->duration = $outage->start_time->diffInSeconds($outage->end_time, true); I now need to remove any time that is between 5pm and 7:59:59 am (i.e. outside of business hours) but I have no idea where to start and appreciate any help :) 回答1: What you can do is

A textual month could not be found ,Trailing data Carbon - laravel

与世无争的帅哥 提交于 2019-12-23 22:27:01
问题 I did try different ways, but I have not get the proper time format. $news->created_at = Carbon::parse($news->created_at)->format('M, d, Y'); $news->created_at = date('d M Y',strtotime($news->created_at)) $news->created_at = date('d M Y',$news->created_at) $news->created_at = Carbon::createFromFormat("d M Y",strtotime($news->created_at)); $news->created_at = $news->created_at->format('M, d, Y'); And the errors are, Unexpected data found The separation symbol could not be found

How can I add date with another date in carbon laravel?

老子叫甜甜 提交于 2019-12-23 19:49:32
问题 In my laravel project, I want to increase the date time with the previous date time. Here is my code $expire=$order[0]->expire_date; $new=Carbon::now()->addMonths(6); The results of these two lines are 2018-01-28 21:23:56 2018-06-28 21:43:29 What I want to do is sum the both dates. $expire+$new How can I achieve this? Any idea or thought? Thanks. Edit with more clear view I want to find the remaining days of the expire date from now and then want to add this remaining date with the new date.

Laravel 4 carbon date format change only once

有些话、适合烂在心里 提交于 2019-12-23 03:07:08
问题 Hello i am usng laravel 4 for a app page, i love the carbon date functions but i have a problem with a date format. i have the date format something like: dd/mm/yyyy And i use : \Carbon\Carbon::createFromTimeStamp(strtotime(Auth::user()->created_at))->diffForHumans() to show how old its something, but that works fine when the date format its yyy-mm-dd how can i only change one time the date format and not every time. 回答1: You may try this: $date = Auth::user()->created_at; \Carbon\Carbon:

Laravel search for records 7 days old only

╄→尐↘猪︶ㄣ 提交于 2019-12-22 10:34:56
问题 i am trying to pull records that are 7 days old only , not older or earlier. But its not working, i'm using Carbon. ->where(DB::raw('date(AppDate)'), Carbon::now()->subDays(7)) 回答1: I have a solution, but its not using Carbon. ->whereRaw('DATE(AppDate) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)') 回答2: You can use whereDate for that : ->whereDate('created_at', Carbon::now()->subDays(7)) ->get(); In the documentation : The whereDate method may be used to compare a column's value against a date PS :

timestampTz fields in Laravel

懵懂的女人 提交于 2019-12-21 23:59:08
问题 Laravel 5.4 supports the Postgres TIMESTAMP WITH TIME ZONE field type in migrations: $table->timestampTz('scheduled_for'); Laravel can be set up to convert date fields ( DATE , DATETIME , TIMESTAMP ) into Carbon objects (and does so by default for the created_at and updated_at TIMESTAMP fields), but putting scheduled_for into the $dates field causes an error with the timezone-aware version: InvalidArgumentException with message 'Trailing data' Looking in the database and tinker, the field's

Can't catch Exception thrown by Carbon

断了今生、忘了曾经 提交于 2019-12-21 07:16:41
问题 The following code throws an exception that I can't catch for whatever reason. try { $this->date = \Carbon\Carbon::parse($this->date)->toDateString(); } catch (Exception $err) { $this->date = \Carbon\Carbon::parse("January 1st 1900")->toDateString(); } Now, if I put this in my routes file in a function closure, it works fine. It's only throwing an exception when it's called from the Model. exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (Summer 2015)

Convert Unix Timestamp to Carbon Object

左心房为你撑大大i 提交于 2019-12-20 10:16:41
问题 I have unix timestamp in table, wants to show to user using Carbon. How can I achieve ? e.g. 1487663764.99256 To 2017-02-24 23:23:14.654621 回答1: Did you check the carbon docs? Something like this? I think this is what youre looking for: Carbon::createFromTimestamp(-1)->toDateTimeString(); Checkout http://carbon.nesbot.com/docs/#api-instantiation 回答2: There are several ways to create Carbon instances described in the Carbon documentation, which is linked at the bottom of the project's README.

Laravel Cashier DateTime::__construct(): Failed to parse time string (@) at position 0 (@):

青春壹個敷衍的年華 提交于 2019-12-20 04:35:34
问题 I have this code when displaying list of invoices, this is similar or maybe exactly the same to the ones in the official Laravel Cashier documentation. I am getting this weird DateTime::__construct(): Failed to parse time string (@) at position 0 (@): Unexpected character error and I'm not sure what's the @ character doing in replace of a supposed to be date. @foreach (Auth::user()->invoices() as $invoice) <tr> <td>{{ $invoice->date()->toFormattedDateString() }}</td> <td>{{ $invoice->total()