php-carbon

Laravel Carbon subtract days from current date

点点圈 提交于 2019-12-03 06:29:05
问题 I am trying to extract objects from Model "Users" whose created_at date has been more than 30 days from today . Carbon::now() ==> I want as ==> Carbon::now() - 30days $users = Users::where('status_id', 'active') ->where( 'created_at', '<', Carbon::now()) ->get(); How can this be achieved ? 回答1: Use subDays() method: $users = Users::where('status_id', 'active') ->where( 'created_at', '>', Carbon::now()->subDays(30)) ->get(); 回答2: You can always use strtotime to minus the number of days from

Laravel Carbon subtract days from current date

ε祈祈猫儿з 提交于 2019-12-02 20:07:27
I am trying to extract objects from Model "Users" whose created_at date has been more than 30 days from today . Carbon::now() ==> I want as ==> Carbon::now() - 30days $users = Users::where('status_id', 'active') ->where( 'created_at', '<', Carbon::now()) ->get(); How can this be achieved ? Use subDays() method: $users = Users::where('status_id', 'active') ->where( 'created_at', '>', Carbon::now()->subDays(30)) ->get(); You can always use strtotime to minus the number of days from the current date: $users = Users::where('status_id', 'active') ->where( 'created_at', '>', date('Y-m-d', strtotime(

Laravel Carbon date diffInDays() on string error

别来无恙 提交于 2019-12-02 19:21:04
问题 I need to find the difference between the two dates. Say i have 2017-02-01 - 2017-01-01. The number of days between the two days is the output $formatted_dt1=Carbon::parse($a->date)->format('Y-m-d'); $formatted_dt2=Carbon::parse($c->dt)->format('Y-m-d'); $date_diff=$formatted_dt1->diffInDays($formatted_dt2); If I give the above code I get the error as FatalThrowableError in ReportsController.php line 67: Call to a member function diffInDays() on string 回答1: Carbon format() function will

Is there a way to change Rails default timestamps to Y-m-d H:i:s (instead of Y-m-d H:i:s.u) or have laravel ignore decimal portion of Y-m-d H:i:s.u?

安稳与你 提交于 2019-12-02 08:35:27
问题 I have two applications using the same postgres DB. A laravel application and a rails application. The current data in the DB is in Y-m-d H:i:s format but whenever rails adds a records, the format is Y-m-d H:i:s.u which includes milliseconds. This causes the following error on the laravel side if the created_at date is ever references in laravel InvalidArgumentException Trailing data ... Laravel models can mutate their date format, so I can include make it conform to Y-m-d H:i:s.u then I

PHP Carbon take number of minutes & convert to days

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 04:32:45
I am trying to convert a number on minutes into number of days using Carbon. $minutes = 1400; I want to do something like below (which of course, does not work): Carbon->minutes($minutes)->days(); I cannot find an example of this. Thanks for any help. Not tested, but a quick look at the docs suggests it might be aimed more at DateTime objects than differences in time, so you could do something like this: $now = Carbon::now(); $days = $now->diffInDays($now->copy()->addMinutes($minutes)); Maybe CarbonInterval::minutes($minutes)->cascade(); ? Will cascade down your value and format it for humans.

Get the 0-value timestamp date with Carbon

主宰稳场 提交于 2019-12-01 22:33:06
I am attempting to get the 0-value timestamp in an PHP API. I am currently using Carbon in a Laravel / Dingo API based system. Is there a simple way to get the 0-value timestamp (presumably something like 01/01/1970) without hardcoding the date? You could try: echo Carbon::createFromTimestamp(0)->toDateString(); // Displays: 1970-01-01 So you do not have to hardcode the date, just use a 0 timestamp. 来源: https://stackoverflow.com/questions/43652282/get-the-0-value-timestamp-date-with-carbon

Retrive all rows from last month (Laravel + Eloquent)

萝らか妹 提交于 2019-12-01 18:34:52
问题 I'm trying to get all records that belongs to last month, so far I managed to get all from last month but to date today, I'm not sure how I can get only for last month $revenueMonth = Callback::where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->sum('payment'); 回答1: More clear solution for your problem: $revenueMonth = Callback::whereMonth( 'created_at', '=', Carbon::now()->subMonth()->month ); 回答2: Try this solutions: $revenueMonth = Callback::where( 'created_at', '>=',

Carbon: diff two datetime objects by dates only

蓝咒 提交于 2019-12-01 05:46:56
Assuming I have the following code: $now = Carbon::now(); $dateTimeObject = Carbon::parse('2017-07-20 10:16:34'); how do I get the diff between the dates only , ignoring the time factor? So, if $now is 2017-07-27 09:11:12, and the date in the $dateTimeObject is 2017-07-20 -- the difference would be 7 . I need it to make sure the results of a specific operation are being stored in the database only once per day. Note: I tried the diffInDays() method, but it returns 0 if the values are e.g. 2016-10-12 23:56:43 and 2016-10-13 02:01:53 - so, close to midnight and at night. Do something like this:

Carbon: diff two datetime objects by dates only

折月煮酒 提交于 2019-12-01 04:24:23
问题 Assuming I have the following code: $now = Carbon::now(); $dateTimeObject = Carbon::parse('2017-07-20 10:16:34'); how do I get the diff between the dates only , ignoring the time factor? So, if $now is 2017-07-27 09:11:12, and the date in the $dateTimeObject is 2017-07-20 -- the difference would be 7 . I need it to make sure the results of a specific operation are being stored in the database only once per day. Note: I tried the diffInDays() method, but it returns 0 if the values are e.g.

Laravel 5 Carbon global Locale

有些话、适合烂在心里 提交于 2019-11-30 18:21:09
I'm trying to set the same global locale of laravel which is : config('app.locale') to work with Carbon. It seems like you can do it by using either : Carbon::setLocale('fr') or setlocale(LC_TIME, 'theLocale'); So I have tried using middleware or providers but was not successful. (why is this not a default feature of laravel?) I configured it in the AppServiceProvider. class AppServiceProvider extends ServiceProvider { public function boot() { // Localization Carbon \Carbon\Carbon::setLocale(config('app.locale')); } } So this is my bad, Carbon is actually using the php setlocale(); the Carbon: