Laravel - Getting Current Month and Year Only

Laravel - Getting Current Month and Year only

If you want to always display the current day's month and year, you can use PHP's built-in date function. For example, the below will give you a label November 2018.

{{Form::label('title', date('F Y') )}}

See PHP Documentation on the date function

If you want more powerful manipulation, use Carbon.

Carbon::now() - only month

$now = Carbon::now();
echo $now->year;
echo $now->month;
echo $now->weekOfYear;

Update:

even this works since Laravel 5.5^

echo now()->month

Is there a better way to get the 12 months base from the current month and year in laravel using carbon

use ->subMonth() or subMonths(n)

You can subtract one month or the number of months from Carbon date.

For example

    $n = // no. of months that you can get from a loop;

$currentDate = now()->subMonths($n);
$userAnswer->where('skip', 0)
->where('company_id', $companyId)
->whereMonth('created_at', $currentDate->month)
->whereYear('created_at', $currentDate->year);

Laravel Eloquent Select Between current month and previous 3 months

This will do the trick I guess

$dateS = Carbon::now()->startOfMonth()->subMonth(3);
$dateE = Carbon::now()->startOfMonth();
$TotalSpent = DB::table('orders')
->select('total_cost','placed_at')
->whereBetween('placed_at',[$dateS,$dateE])
->where(['deleted' => '0', 'delivery_address_id' => $DeliveryAddress->id])
->sum('total_cost');

startOfMonth() begins with 1st date of the month

Laravel5.7: Get sum() for current month using model returned in view

You need to use the methods whereMonth and whereYear to filter the transactions by the current month and year. After that, you can use the database's sum method to sum all time_spent.

Your method should look like this:

public function transactions($year, $month)
{
return Transaction::whereYear('created_at', $year)
->whereMonth('created_at', $month)
->sum('time_spent');
}

This will run a query similar to this (it depends on which database you're using):

SELECT SUM(transactions.time_spent) FROM transactions WHERE YEAR(transactions.created_at) = 2018 AND MONTH(transactions.created_at) = 11;

To get the current month, you should call the method like this:

$month = date('m');
$year = date('Y');

$transactions = $info->transactions($month, $year);

Laravel: get table of month's dates via Carbon

That's definitely possibly. Since it sounds pretty interactive to me, I would probably combine it with a JS framework like Vue.js. The benefit of using Vue in this case, is that you won't visibly have to submit a form (meaning that the page will refresh) when selecting a work shift: you can easily do this under the hood by sending an Ajax call. It is however not necessary to accomplish what you want.

Using Carbon, you can get the days in the current month by doing:

$period = Carbon\CarbonPeriod::create(Carbon\Carbon::now()->startOfMonth(), Carbon\Carbon::now()->endOfMonth());
foreach($period as $date)
{
$dates[] = $date->format('d-m-Y');
}

How to get all records for current year?

You can do this:

Articles::whereBetween('created_at', [
Carbon::now()->startOfYear(),
Carbon::now()->endOfYear(),
]);

Another way is to use whereYear() as in @xhulio answer. But I'd recommend you to use this code instead as it's more readable:

Articles::whereYear('created_at', date('Y'))->get();


Related Topics



Leave a reply



Submit