'Laravel needs to display only the date not the time using the Carbon class
Currently, I am using
Carbon::now()
It Displays the date with the time
2015-03-10 23:23:46
But I only need a date
2015-03-10
Solution 1:[1]
Simply use this
Carbon::today()->toDateString()
Solution 2:[2]
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 3:[3]
This solution will work for you.
<td>
{{$message->created_at->todatestring()}}
</td>
Solution 4:[4]
This is correctly:
$date = Carbon::now();
echo $date->format('Y-m-d');
Solution 5:[5]
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()}}
Solution 6:[6]
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 7:[7]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gayashan Perera |
| Solution 2 | Kiran Maniya |
| Solution 3 | JJJ |
| Solution 4 | Adrian |
| Solution 5 | mightyteja |
| Solution 6 | Nikunj Dhimar |
| Solution 7 |
