user10665294
user10665294

Reputation:

Laravel - Getting Current Month and Year only

I am new to Laravel I'm just learning new things, but as for my project. I need to get the current month and year for today. How can I do it? for what I have now is a label and I want to put the current month and year on the label. Thank you

Here is my LABEL

{{Form::label('title', 'Title')}}

Upvotes: 1

Views: 20555

Answers (4)

Yves Kipondo
Yves Kipondo

Reputation: 5603

Laravel come with a prebuilt component to manage Date, and it's know under the name Carbon it have many method to access and modify DateTime php object. To access the month and year of the current day you can access the property month and year directly on the Carbon Object

$today = Carbon::now();
$today->month; // retrieve the month
$today->year; // retrieve the year of the date

In you Controller you can pas the $today object to the method use to render the related view with purpose to have access to that object on a get value of attribute that you want to show in the label field

Upvotes: 2

Sheetal Mehra
Sheetal Mehra

Reputation: 518

You can also try this.

{{ Form::label('title', Carbon\Carbon::now()->format('F Y') }}

Upvotes: 0

hktang
hktang

Reputation: 1833

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.

Upvotes: 2

Angad Dubey
Angad Dubey

Reputation: 5452

You can use the Carbon library which is available out of the box with Laravel:

{{ Form::label('title', \Carbon\Carbon::now()->format('M, Y') }}

You can format using native PHP DateTime Format

Update:

As @Josh mentioned, you can also use the laravel helper now()

The now function creates a new Illuminate\Support\Carbon instance for the current time

{{ Form::label('title', now()->format('M, Y') }}

Upvotes: 8

Related Questions