Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kickthemooon's avatar

Carbon only day of date and month only letters

So I a published_at date time property in my posts. Now id like to echo only the day for example 20 from the published_at property and I need to echo the month separately in the short form , jan, feb etc.

?

0 likes
16 replies
d3xt3r's avatar

An example :

$t = Carbon::now();
$t->day;
$t->month // or $t->format('proper date format accepted by PHP')
2 likes
d3xt3r's avatar

If Post is your Eloquent model, first thing is to do is to add

protected $dates = ['published_at'];

Now access all the properties as a Carbon instance.

1 like
kickthemooon's avatar

But I get an error like this:

Trying to get property of non-object
d3xt3r's avatar

I am not sure how you are accessing it.

try dd($post) => should see a post instance and then dd($post->published_at) => should see a carbon instance

If you have everything set up properly above should work. Also, check for the datatype for published_at column

1 like
kickthemooon's avatar

I do have a mutator:

public function getPublishedAtAttribute($value)
    {
        return Carbon::parse($value)->format('d.m.Y H:i:s');
    }

Not sure if this can be the cause of problems...

kickthemooon's avatar

And the property is a datetime property not just date....

d3xt3r's avatar

Yes for sure this will create a problem. You don't need a accessor, if working with carbon, its taken care for you. You accessor converts it to string and hence the error.

1 like
kickthemooon's avatar

hmm, but i need this mutator, im from europe, i need to show dates in a certain format..., d.m.Y.

vtalbot's avatar

Your mutator getPublishedAtAttribute transform your published_at into a string.

d3xt3r's avatar
d3xt3r
Best Answer
Level 29

You could do something like

public function getPublishedAtFormattedAttribute($value)
    {
        return $this->published_at->format('d.m.Y H:i:s');
    }

$post->published_at_formatted;

// or 
$post->published_at->format('d.m.Y H:i:s');
1 like

Please or to participate in this conversation.