Jump to content

Group posts by day of the week? "Datetime" field (mon, tue, ...)


Recommended Posts

Posted

Hello,

I'm having some problems grouping tabbed posts by day. So clikcking on expl. monday would show all posts starting on monday.

I have datetime field "Date_start" added in "my calendar-post" template.

It should look like this

Events.thumb.jpg.2f831a75b5ca569285f3dddef65926cb.jpg

 

But now shows all the days so if I have 5 events starting on MONDAY it shows 5 MON in tabs. Like:

Events_wrong.thumb.jpg.ff3e1b471bd004788503cec37e7d7958.jpg

Not sure how to group these by Start_date.

My code now:

Spoiler

                <div class="tabs movies">
                    <ul>
                    <!-- Events by day START -->
                    
                        <?php foreach($pages->get(1029)->children() as $child) :?>
                            <li><a href="#<?=strftime("%A", strtotime($child->Start_date));?>"><?=strftime("%A", strtotime($child->Start_date));?></a></li>
                        <?php endforeach;?>
                        
                        <!-- Events by day END -->

                        <li class="date"><span>Wednesday, 8 March</span></li>
                    </ul>
                    <!-- Events by day END -->

                    
                    <?php $days = $pages->find('template=calendar-post, start=0'); ?>
                                            
                    <?php foreach($days as $dnevi): ?>
                    <div id="<?php echo strftime("%A", strtotime($dnevi->Start_date)); ?>">

                        <!-- EVENT START -->
                        
                        <?php $events = $pages->find('template=calendar-post, Start_date|End_date>=today, sort=Start_date, start=0, limit=15'); ?>
                        <?php foreach($events as $single): ?>
                        
                        <div class="row movie-tabs">
                            <div class="col-md-2 col-sm-3">
                                <a href="<?=$single->url?>"><img alt="Movie title" src="<?php echo $config->urls->templates?>assets/images/movie-6.jpg"></a>
                            </div>


                            <div class="col-md-10 col-sm-9">
                                <span class="title">Action, Adventure, Fantasy</span>

                                <h3 class="no-underline"><?=$single->title?></h3>

                                <?=$sanitizer->truncate($single->body, 150);?>
                                
                                <p><a class="arrow-button" href="<?=$single->url?>">Beri več</a></p>
                                <div class="row">
                                    <div class="col-md-8 col-sm-9">
                                        <hr class="space-10">
                                        <span class="viewing-times"><i class="material-icons">access_time</i> Viewing times</span> <span class="time past">14:45</span> <span class="time">18:30</span>                                                        <span class="time">20:30</span> <span class="time">24:45</span>
                                    </div>
                                    <div class="col-md-4 col-sm-3 running-time">
                                        <hr class="space-10">
                                        105 mins <span class="certificate">15</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endforeach ;?>
                        <!-- EVENT END -->

                    </div>
<?php endforeach ;?>

                </div>

And original html:

Spoiler

                    <div class="tabs movies">
                        <ul>
                            <li><a href="#mon">Mon</a></li>
                            <li<a href="#tue">Tue</a></li>
                            <li><a href="#wed">Today</a></li>
                            <li><a href="#thu">Thu</a></li>
                            <li><a href="#fri">Fri</a></li>
                            <li><a href="#sat">Sat</a></li>
                            <li><a href="#sun">Sun</a></li>

                            <li class="date"><span>Wednesday, 8 March</span></li>
                        </ul>

                        

                        <div id="mon">
                            <div class="row movie-tabs">
                                <div class="col-md-2 col-sm-3">
                                    <a href="single-movie.html"><img alt="Movie title" src="images/movie-6.jpg"></a>
                                </div>


                                <div class="col-md-10 col-sm-9">
                                    <span class="title">Action, Adventure, Fantasy</span>

                                    <h3 class="no-underline">End of an era</h3>
                                    <p>European mercenaries searching for black powder become embroiled in the defense of End of an era of China against a horde of monstrous creatures.</p>
                                    <p><a class="arrow-button" href="news-single.html">Full synopsis</a>
                                    </p>
                                    <div class="row">
                                        <div class="col-md-8 col-sm-9">
                                            <hr class="space-10">
                                            <span class="viewing-times"><i class="material-icons">access_time</i> Viewing times</span> <span class="time past">14:45</span> <span class="time">18:30</span> <span class="time">20:30</span> <span class="time">24:45</span>
                                        </div>
                                        <div class="col-md-4 col-sm-3 running-time">
                                            <hr class="space-10">
                                            105 mins <span class="certificate">15</span>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>


                        <div id="tue">
                            <div class="row movie-tabs">
                                <div class="col-md-2 col-sm-3">
                                    <a href="single-movie.html"><img alt="Movie title" src="images/movie-7.jpg"></a>
                                </div>


                                <div class="col-md-10 col-sm-9">
                                    <span class="title">Drama</span>

                                    <h3 class="no-underline">As she sleeps</h3>


                                    <p>A chronicle of the childhood, adolescence and burgeoning adulthood of a young black man growing up in a rough neighborhood of Miami.</p>


                                    <p><a class="arrow-button" href="news-single.html">Full synopsis</a>
                                    </p>


                                    <div class="row">
                                        <div class="col-md-8 col-sm-9">
                                            <hr class="space-10">
                                            <span class="viewing-times"><i class="material-icons">access_time</i> Viewing times</span> <span class="time past">11:00</span> <span class="time past">14:30</span> <span class="time">20:00</span> <span class="time">21:15</span>
                                        </div>


                                        <div class="col-md-4 col-sm-3 running-time">
                                            <hr class="space-10">
                                            117 mins <span class="certificate">U</span>
                                        </div>
                                    </div>
                                </div>
                            </div>

etc .....
                    </div>

 

Any help appreciated (I'm not a coder) ?

Thank you

R

Posted

Somehow got it grouped by day but not quite working yet. It shows events somehow mixed up not sure why.

My code:

Spoiler

<?php
$events = $pages->find('template=calendar-post');
$grouped = [];
foreach ($events as $event) {
    $dayname = $datetime->date('%A', $event->Start_date);
    if (isset($grouped[$dayname])) {
        $grouped[$dayname][] = $event;
    } else {
        $grouped[$dayname] = [$event];
    }
}
?>  

 

             <div class="tabs movies">
                    <ul>
                    <!-- Events by day START -->
                    
                        <?php foreach ($grouped as $dayname => $eventsingle): ?>
                            <li>
                                <a href="#<?= $dayname ?>"><?= $dayname ?></a>
                            </li>
                        <?php endforeach;?>
                        
                        <!-- Events by day END -->

                        <li class="date"><span>Wednesday, 8 March</span>
                        </li>
                    </ul>
                    <!-- Events by day END -->

                    
                    <?php foreach ($grouped as $dayname => $eventsingle): ?>
                    <div id="<?= $dayname ?>">
                    

                        <!-- EVENT START -->
                        
                        <?php foreach ($eventsingle as $p): ?>
                        
                        <div class="row movie-tabs">
                            <div class="col-md-2 col-sm-3">
                                <a href="<?=$p->url?>"><img alt="Movie title" src="<?php echo $config->urls->templates?>assets/images/movie-6.jpg"></a>
                            </div>
                            <div class="col-md-10 col-sm-9">
                                <span class="title">Action, Adventure, Fantasy</span>

                                <h3 class="no-underline"><?=$p->title?></h3>

                                <?=$sanitizer->truncate($p->body, 150);?>
                                
                                <p><a class="arrow-button" href="<?=$p->url?>">Beri več</a></p>
                                <div class="row">
                                    <div class="col-md-8 col-sm-9">
                                        <hr class="space-10">
                                        <span class="viewing-times"><i class="material-icons">access_time</i> Viewing times</span> <span class="time past">14:45</span> <span class="time">18:30</span> <span class="time">20:30</span> <span class="time">24:45</span>
                                    </div>


                                    <div class="col-md-4 col-sm-3 running-time">
                                        <hr class="space-10">
                                        105 mins <span class="certificate">15</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endforeach ;?>
                        <!-- EVENT END --> 

                    </div>
                    <!-- Events by day END -->
                    <?php endforeach ;?>

                </div>

any help appreciated

thank you

R

Posted

Please use code formatting with the correct language. It’s the „<>“ button in the comment editor. Here it is:



<?php
$events = $pages->find('template=calendar-post');
$grouped = [];
foreach ($events as $event) {
    $dayname = $datetime->date('%A', $event->Start_date);
    if (isset($grouped[$dayname])) {
        $grouped[$dayname][] = $event;
    } else {
        $grouped[$dayname] = [$event];
    }
}
?>  

 

             <div class="tabs movies">
                    <ul>
                    <!-- Events by day START -->
                    
                        <?php foreach ($grouped as $dayname => $eventsingle): ?>
                            <li>
                                <a href="#<?= $dayname ?>"><?= $dayname ?></a>
                            </li>
                        <?php endforeach;?>
                        
                        <!-- Events by day END -->

                        <li class="date"><span>Wednesday, 8 March</span>
                        </li>
                    </ul>
                    <!-- Events by day END -->

                    
                    <?php foreach ($grouped as $dayname => $eventsingle): ?>
                    <div id="<?= $dayname ?>">
                    

                        <!-- EVENT START -->
                        
                        <?php foreach ($eventsingle as $p): ?>
                        
                        <div class="row movie-tabs">
                            <div class="col-md-2 col-sm-3">
                                <a href="<?=$p->url?>"><img alt="Movie title" src="<?php echo $config->urls->templates?>assets/images/movie-6.jpg"></a>
                            </div>
                            <div class="col-md-10 col-sm-9">
                                <span class="title">Action, Adventure, Fantasy</span>

                                <h3 class="no-underline"><?=$p->title?></h3>

                                <?=$sanitizer->truncate($p->body, 150);?>
                                
                                <p><a class="arrow-button" href="<?=$p->url?>">Beri več</a></p>
                                <div class="row">
                                    <div class="col-md-8 col-sm-9">
                                        <hr class="space-10">
                                        <span class="viewing-times"><i class="material-icons">access_time</i> Viewing times</span> <span class="time past">14:45</span> <span class="time">18:30</span> <span class="time">20:30</span> <span class="time">24:45</span>
                                    </div>


                                    <div class="col-md-4 col-sm-3 running-time">
                                        <hr class="space-10">
                                        105 mins <span class="certificate">15</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endforeach ;?>
                        <!-- EVENT END --> 

                    </div>
                    <!-- Events by day END -->
                    <?php endforeach ;?>

                </div>

 

What is $datetime? Also, what is $event->Start_date? That is to say, what types are they?

  • Like 1
Posted

hey, sorry for the formating, i did it so it doesn't look to much for the reader. 

"Start_date" is my datetime field that states the start of the event. 

$datetime was actualy some part of the code I found on the forum for showing the name of the day. I'm not a coder so maybe I'm completely wrong here. Not 100% sure what I'm doing... ? That is why I need some help with this. ?

thank you

R

 

Posted

I see, well, try this maybe?

$events = $pages->find('template=calendar-post');
$grouped = [];
foreach ($events as $event) {
    $dayname = date('D', $event->getUnformatted('Start_date'));
    $grouped[$dayname][] = $event;
}

 

  • Like 1
Posted

Yes, it's working great, only thing is that the days are now only in english. I have multilanguage site.

Tried "strftime("%A", strtotime($event->Start_date))" but ofc. it's not working. The days are in the right language but events are messed up as before.

Any ideas?

Thank you

R

Posted

strftime() is deprecated, you shouldn’t use it anymore. You can use IntlDateFormatter to format localised dates:

$weekdayFormatter = new \IntlDateFormatter('es_CO', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, null, null, 'eee');
$events = $pages->find('template=calendar-post');
$grouped = [];
foreach ($events as $event) {
    $dayname = $weekdayFormatter->format($event->getUnformatted('Start_date'));
    $grouped[$dayname][] = $event;
}

I wouldn’t really do this because you may get unexpected results and maybe even characters that are illegal in URL fragments (?). For example with de_DE you’ll get “Mo.” and “Lun.” with es_CO. I would probably keep the URL fragments English and have a second localised string. Also, the method above will only give you days that actually have events, so maybe pre-populate the $grouped array with all 7 days.

  • Like 1
Posted

I tried the above but not working as it should, the events are messed up again. 

22 minutes ago, Jan Romero said:

maybe pre-populate the $grouped array with all 7 days.

How would I do this? Sorry and thank you for helping, but would really like this one to work. ? 

Posted

Try this?

<?php
$events = $pages->find('template=calendar-post');

// populate dayname according to user’s language
$grouped = [
	'Mon' => (object)['events' => [], 'dayname' => 'Ponedeljek'],
	'Tue' => (object)['events' => [], 'dayname' => 'Torek'],
	'Wed' => (object)['events' => [], 'dayname' => 'Sreda'],
	'Thu' => (object)['events' => [], 'dayname' => 'Četrtek'],
	'Fri' => (object)['events' => [], 'dayname' => 'Petek'],
	'Sat' => (object)['events' => [], 'dayname' => 'Sobota'],
	'Sun' => (object)['events' => [], 'dayname' => 'Nedelja']
];
foreach ($events as $event) {
    $dayname = date('D', $event->getUnformatted('Start_date'));
    $grouped[$dayname]->events[] = $event;
}
?>  


<div class="tabs movies">
    <!-- tab buttons -->
    <ul>
    <?php foreach ($grouped as $anchor => $day): ?>
        <li>
            <a href="#<?=$anchor?>"><?=$day->dayname?> (<?=count($day->events)?>)</a>
        </li>
    <?php endforeach;?>
    </ul>
    <!-- /tab buttons -->

    
    <!-- tab contents -->
    <?php foreach ($grouped as $anchor => $day): ?>
    <div id="<?=$anchor?>">
        <?php foreach ($day->events as $p): ?>
        
        <div class="row movie-tabs">
            <div class="col-md-2 col-sm-3">
                <a href="<?=$p->url?>"><img alt="Movie title" src="<?php echo $config->urls->templates?>assets/images/movie-6.jpg"></a>
            </div>
            <div class="col-md-10 col-sm-9">
                <span class="title">Action, Adventure, Fantasy</span>

                <h3 class="no-underline"><?=$p->title?></h3>

                <?=$sanitizer->truncate($p->body, 150);?>
                
                <p><a class="arrow-button" href="<?=$p->url?>">Beri več</a></p>
                <div class="row">
                    <div class="col-md-8 col-sm-9">
                        <hr class="space-10">
                        <span class="viewing-times"><i class="material-icons">access_time</i> Viewing times</span> <span class="time past">14:45</span> <span class="time">18:30</span> <span class="time">20:30</span> <span class="time">24:45</span>
                    </div>

                    <div class="col-md-4 col-sm-3 running-time">
                        <hr class="space-10">
                        105 mins <span class="certificate">15</span>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach ;?>
    </div>
    <?php endforeach ;?>
    <!-- /tab contents -->
</div>

 

  • Like 2
  • Thanks 1
Posted

OMG, thank you very much it works great ?

also the count numbers! ?

Exactly what I needed. I need to save this for further use .. 

Thank you very much for help ? Much appreciated ?

R

  • Like 1
Posted
30 minutes ago, Roych said:

How did you know it is Slovene language

Eventually from the First Screenshot ? „Beri več“ 

  • Thanks 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...