Home › Forums › General Issues › Display day in loop with Datepicker
Hello,
I use this code to display a carousel of events sorted by month / year on the basis of an ACF “DatePicker” field, it works very well.
I would just like to solve 2 problems:
1. Show my French months in no English
2. View the recorded day for the event
Below my code.
Thanks for your help
<?php
$group_posts = array();
if( $posts ) {
global $post;
foreach( $posts as $post ) {
$date = get_field('date_de_levenement', $post->ID, false);
$date = new DateTime($date);
$year = $date->format('Y');
$month = $date->format('F');
$group_posts[$year][$month][] = array($post, $date);
}
} ?>
<?php
foreach ($group_posts as $yearKey => $years) {
foreach ($years as $monthKey => $months) { ?>
<?php echo '<h4 class="blanc">' . $monthKey . ' ' . $yearKey . '</h4>';
foreach ($months as $postKey => $posts) {
setup_postdata($post); ?>
<a href='<?php echo get_the_permalink($post->ID) ?>'>
<?php ///// HERE DISPLAY DAY OF THE EVENT ////// ?>
<span class="blanc"><?php echo get_the_title(); ?></span>
</a>
<?php
} ?>
<?php }
} wp_reset_postdata();
?>
Hi Eric,
Your first issue : write month in French it’s not an ACF issue but a PHP one because you use ‘DateTime()‘ an ‘format‘ and this only works in English. If you want to get the month in French you can :
For the first solution, the exemple :
Traduce_Month = array (
"January" => 'Janvier',
"February" => 'Fevrier',
...
)
echo '<h4 class="blanc">' .Traduce_Month[$monthKey]. ' ' . $yearKey . '</h4>';
For the second one you can find all the documentation needed in PHP codex by searching ‘strftime‘ and ‘DateTime::createFromFormat‘ (to convert you date).
For your second issue (still a PHP issue), to write the event date you just have to get the $date value you saved in your array ‘$group_posts[$year][$month][]‘ and echo it by using ‘$date->format‘ as you already done.
Hope it’s help
The topic ‘Display day in loop with Datepicker’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.