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

BryanWesley's avatar

How to print one date for many posts.

Hi !

I'm trying to select every post per day where the day is not empty. Then print the day only once for multiple posts.

It should give something like that:

2016 Jan 01

  • post 1
  • post 2
  • post 3
  • post 4

(nothing on 2016 Jan 02, so forget it)

2016 Jan 03

  • post 5
  • post 6
  • post 7

Thank you everyone !

0 likes
16 replies
Snapey's avatar

You could query and group by date, but what I often do is get all the records ordered by date and then insert a date break each time the day element of the item changes

<?php $currentDate = ''; ?>
 @foreach($posts as $post)
    
    @if(!$post->created_at->isSameDay($currentDate))
        <?php $currentDate = $post->created_at ?>
        <h3>{{ $post->created_at->toFormattedDateString() }}</h3>
    @endif 
    {{ $post->title }}<br />

@endforeach

(untested)

1 like
BryanWesley's avatar

@Snapey What's happening in 'is same day' ?

Is it a scope ? How does it work ?

Thank you ! :)

jlrdw's avatar

But what type of relation do you have doesn't matter who's post you are printing for that day. In other words do you only want to see Joe Blows post or anyone's post as long as it's on that day the relations can be tricky I would go back and make sure you have proper relationship setup look at the basic tutorial that's built right into the documentation, not the same but might Guide you. Also look at the link I gave you above still not the exact same but it does print out a report Style. A little bit of playing with queryBuilder and you will probably get it.

I actually have a couple more good examples but I'm on mobile now not desktop.

BryanWesley's avatar

@jlrdw & @Snapey, Here is what i've done till now, for the last 30 days:

@for($date=Carbon::now();$date>=Carbon::now()->subDay(30);$date->subDay(1))
    <h3>{{$date->format('Y-m-d')}}</h3>
    
    @foreach($posts as $post)
        @if(Carbon::createFromFormat('Y-m-d H:i:s', $post->updated_at)->format('Y-m-d') === $date->format('Y-m-d'))
            <p>{{$post->title}}</p>
        @endif
    @endforeach 

@endfor

Well it works, but the problem is that it's printing the date even though there is no ads for this date.

jlrdw's avatar

In your foreach you have to check if null or empty and skip it refer back to Snapey reply. Probably need a if else construct.

jlrdw's avatar

I edited my answer to you will probably need an if else construct. Testing for empty or not

1 like
d3xt3r's avatar

Say suppose you have n posts, what @Snapey did has time complexity of O(n) and what you are doing has complexity of O(30n). If you are not sure, stick with what he has shown :)

Snapey's avatar

If you loop over the posts, then you only will be dealing with the days of the posts, not all the days if you loop over the dates.

IsSameDay is a Carbon comparison http://carbon.nesbot.com/docs/#api-comparison

So, first time around, currentDate is empty so the comparison fails. You therefore move the carbon created_at value to the current date and output your date.

You then print a post title.

Next time around, if the created date is the same day (IsSameDay) as the previous post (stored in current day) then skip printing the date and just put out the post name.

I just thought, leverage Carbon rather than converting everything to strings.

1 like
BryanWesley's avatar

@Snapey, thank you very much ! Now I get it ! haha

Thank you everyone for your time ! I appreciate ! :)

BryanWesley's avatar

@Snapey, one little problem, we are giving en empty STRING to isSameDay(), it has to be an instance of Carbon

Snapey's avatar

ah, ok, sorry. You can initialise it with a date that is unlikely to be one of your post dates;

$currentDate=\Carbon\Carbon::createFromDate(1970,01,01);

Please or to participate in this conversation.