Laravel needs to display only date not the time using the Carbon class

LaravelLaravel 5Laravel 4

Laravel Problem Overview


Currently, I am using

> Carbon::now()

It Displays date with the time

> 2015-03-10 23:23:46

But I only need a date

2015-03-10

Laravel Solutions


Solution 1 - Laravel

http://carbon.nesbot.com/docs/#api-formatting

$dt = Carbon::now();
echo $dt->toDateString(); // Equivalent: echo $dt->format('Y-m-d');

Solution 2 - Laravel

Simply use this

Carbon::today()->toDateString()

Solution 3 - Laravel

Get Today's Date in Y-m-d formta as given below,

$dt = Carbon::today()->toDateString(); // Use today to display only date

if you have used $dt = Carbon::today() only, it will return the timestamp also.

Solution 4 - Laravel

This solution will work for you.

<td>
	{{$message->created_at->todatestring()}}
</td>

Solution 5 - Laravel

This is correctly:

$date = Carbon::now();
echo $date->format('Y-m-d');

Solution 6 - Laravel

Or you can use this,

echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString();
 // 1975-05-21 22:00:00

Example,

{{ $post->created_at->toDayDateTimeString()}}

Carbon Docs Source Link

Solution 7 - Laravel

This is very important when you have a critical case for checking due date. Here what you can simply use in your case.

Carbon::now()->format('Y-m-d')

Solution 8 - Laravel

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

// for the current date
Carbon::now() 

// OR 
now()

# output
# 2021-07-02 00:00:00

// OR
Carbon::now()->toDateString() 

// OR 
now()->toDateString()

# output
# 2021-07-02

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuhammadView Question on Stackoverflow
Solution 1 - LaravelTjkoopaView Answer on Stackoverflow
Solution 2 - LaravelGayashan PereraView Answer on Stackoverflow
Solution 3 - LaravelSurahmanView Answer on Stackoverflow
Solution 4 - Laravelumar farooqView Answer on Stackoverflow
Solution 5 - LaravelAdrianView Answer on Stackoverflow
Solution 6 - LaravelHunter EyesView Answer on Stackoverflow
Solution 7 - LaravelNikunj DhimarView Answer on Stackoverflow
Solution 8 - LaravelNeeraj TangariyaView Answer on Stackoverflow