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

ivan2nn's avatar

Where extract the day from date property

I am following the guide to implement a "published_at" property. public function getStoryDateAttribute() { return date('d-m-Y', strtotime($this->attributes['story_date'])); } In the blade view I need to extract the different values in each element

<time datetime={!! $event->story_date !!}>
                    <span class="day">4</span>
                    <span class="month">Jul</span>

I would like to know where I should implement a function that allows me to get just the day, or the month... Directly in the view, or in the controller?.. If it is in the controller how can i access it from the view?

thank you

0 likes
5 replies
ohffs's avatar

If you add the 'story_date' to the $dates array on your model then you can use it as a Carbon object and extract the info, format it etc. Eg :

<time datetime="{{ $event->story_date->format('d-m-Y') }}">
<span class="day">{{ $event->story_date->day }}</span>
ivan2nn's avatar

Thanks ohffs,

so do i have to take out the Accessor from the code?

This is the mutator i have:

public function setStoryDateAttribute($event_date)
{
    $this->attributes['story_date'] = Carbon::createFromFormat('d-m-Y', $event_date);
}

How can transform the 'm' in an 'M' in the view ... I tried

{{ $event->story_date->format('d-M-Y')->month }}

but it does not comes out. Do i have to save it already as 'd-M-Y' in the mutator? (But that would be the format that will be save din the DB right?)

ohffs's avatar

Yeah - I'd take out the accessor. I don't think you'll need the setter either. Hopefully it should all work from there :-)

ivan2nn's avatar

Nope, it gives me error (probably because he's expecting Y-m-d and i give it d-m-Y? ).. anyway I can live leaving that mutator, but what about extracting the month in the 3 letters format ( format 'M' )... is it possible?

Snapey's avatar

Convert it once to a carbon instance and then you can use Carbon getters to get it in the format you want at each use

for instance

                    <span class="day">{{ $event->story_date ->day }}</span>
                    <span class="month">{{ $event->story_date->format('M') }}</span>
1 like

Please or to participate in this conversation.