Formatting a Carbon date instance
Solution 1
First parse the created_at field as Carbon object.
$createdAt = Carbon::parse($item['created_at']);
Then you can use
$suborder['payment_date'] = $createdAt->format('M d Y');
Solution 2
Date Casting for Laravel 6.x and 7.x
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => 'datetime:Y-m-d',
'updated_at' => 'datetime:Y-m-d',
'deleted_at' => 'datetime:Y-m-d h:i:s'
];
It easy for Laravel 5 in your Model add property protected $dates = ['created_at', 'cached_at']
. See detail here https://laravel.com/docs/5.2/eloquent-mutators#date-mutators
Date Mutators: Laravel 5.x
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
}
You can format date like this $user->created_at->format('M d Y');
or any format that support by PHP.
Solution 3
$suborder['payment_date'] = Carbon::parse($item['created_at'])->format('M d Y');
Solution 4
If you are using eloquent model (by looking at your code, i think you are), you dont need to convert it into array. Just use it as object. Becaus elike Thomas Kim said, by default it is a Carbon instance
So it should be
$suborder['payment_date'] = $item->created_at->format('Y-m-d')
But if it is not then, you need convert it to Carbon object as Milan Maharjan answer
$createdAt = Carbon::parse($item['created_at']);
Solution 5
Declare in model:
class ModelName extends Model
{
protected $casts = [
'created_at' => 'datetime:d/m/Y', // Change your format
'updated_at' => 'datetime:d/m/Y',
];
d3bug3r
010011010111100100100000011011100110000101101101011001010010000001101001011100110010000001010011011010010111011001100001
Updated on July 18, 2022Comments
-
d3bug3r almost 2 years
I have an array that returns the following date time:
$item['created_at'] => "2015-10-28 19:18:44"
How do I change the date to
M d Y
format in Laravel using Carbon?Currently it returns with an error
$suborder['payment_date'] = $item['created_at']->format('M d Y');
-
Marc Pont over 5 yearsI tried this approach but when the field is null it converts into a weird date:
'date_confirmation' => string '-0001-11-30 00:00:00' (length=20)
. You know why is doing this and how I can solve it? -
Roland over 5 yearsThis is a per-usage approach. I look for a more general approach, means a config entry and All
Carbon\Carbon
attributes are formatted the same (I already useprotected $dates = ['my_date_at'];
). -
Roland over 5 yearsNo need for this,
created_at
is converted toCarbon\Carbon
automatically, ifprotected $timestamps = false;
is NOT set. You also need a$table->timestamps();
in your migration file. -
Roland over 5 yearsThis is already a better solution as it is a per-model-approach, yet a bit incomplete. For Laravel's internal columns
created_at
andupdated_at
there is no need forprotected $dates = [];
in yourModel
. But if you have custom columns like I have, you have add those to that array and don't addprotected $timestamps = true;
to your model as this is the default. -
Roland over 5 years
Payment->created_at
is then renamed topayment_date
and no usage of directPayment
class (Eloquent model) which allows onlyarray $item
as type-hint. Maybe not what you want? -
Roland over 5 yearsGood point with
Payment $payment
usage, so no need for converting it to array, you can always use direct Eloquent model objects in your blade templates. But still I would prefer not renamingcreated_at
topayment_date
. -
Roland over 5 yearsMay I ask why I'm getting down-votes for this answer? I just got -2 which feels a bit unfair because my critics are at 2nd thought good.
-
Roland over 5 yearsThis is only true for records been created after
created_at
has been added, e.g. by adding$table->timestamps()
to your migration file, for records before it,created_at
will beNULL
and then this will trigger an error about a method call on a non-object call.