Isaac Arcilla
Isaac Arcilla

Reputation: 13

How to format timestamp in laravel?

I have this timestamp created_at

public function getCreatedAtAttribute($value)
{
    return Carbon::parse($value)->format('M d, Y h:i:s');
}

How can I format this like Oct 9, 2021 at 1:23 PM with an at string?

Upvotes: 0

Views: 684

Answers (3)

yadu siva das
yadu siva das

Reputation: 628

   return Carbon::parse($value)->format('M d, Y').' at '.Carbon::parse($value)->format('g:i A');  

Upvotes: 0

OMR
OMR

Reputation: 12218

you can chain format as you need:

public function getCreatedAtAttribute($value)
{
  
     return $value->format('M d, Y ').' at '.$value->format('h:i:s');
}

Upvotes: 3

Gert B.
Gert B.

Reputation: 2347

You can add the string by escaping the characters:

return Carbon::parse($value)->format('M d, Y \a\t H:i');

Upvotes: 4

Related Questions