Home › Forums › General Issues › Loop through fields to find todays date
Hi,
I have created a opening hours option for a client and I want to display on the front of the site of the store is open based on todays date. It is sort of working, but not quite. The query is only comparing the first field entry. I somehow need to store all entries into an array and get see if that day is in there, if so, continue to show the results of just that date. Here is my current code. Thanks in advance 🙂
<?php
// Check for opening hour entries
if( have_rows('opening_hour_settings', 'option') ):?>
<div class="header-opentimes">
<?php while( have_rows('opening_hour_settings', 'option') ): the_row();
// Get the Day
$storeopen = get_sub_field('open_message');
$storeclose = get_sub_field('closed_message');
while( have_rows('opening_hours') ): the_row();
$today = date('l');
$day = get_sub_field('day');
// See if it is current day, if so display todays open times
//Test to see if current day is same as the $day returned by query
echo $today; echo $day;
if ( $today == $day) :?>
<h5><?php echo $storeopen;?></h5>
<ul class="opentimes">
<?php
//Loop through all opening times for that day
while( have_rows('times') ): the_row();
$open = get_sub_field('open_time');
$close = get_sub_field('close_time');
$times = '<li>' . $open . '-' . $close . '</li>';
echo $times;
endwhile;
echo '</ul>';
//break loop to just display first result
break;
// If store not open, message closed displayed
else:?>
<h5><?php echo $storeclose ?></h5>
<?php //Break loop otherwise it will duplicate result based on number of 'day' fields
break;
endif;
endwhile;
endwhile;
?>
</div>
<?php
endif;?>
For anyone who runs into this with same issue, this solution worked for me based on Johns response here… link
<?php
$found = false;
$value = date('l');
if( have_rows('opening_hour_settings', 'option') ) {
while( have_rows('opening_hour_settings', 'option')) {
the_row();
$storeopen = get_sub_field('open_message');
$storeclose = get_sub_field('closed_message');
while( have_rows('opening_hours')) { the_row();
if ($value == get_sub_field('day')) {
$found = true;
echo '<h5>' . $storeopen .'</h5>
<ul class="opentimes">';
//Loop through all opening times for that day
while( have_rows('times') ): the_row();
$open = get_sub_field('open_time');
$close = get_sub_field('close_time');
$times = '<li>' . $open . '-' . $close . '</li>';
echo $times;
endwhile;
echo '</ul>';
//break loop to just display first result
break;
}
}
}
// end while have rows
} // end if have rows
if (!$found) {
echo '<h5>' . $storeclose . '</h5>';
}
?>
The topic ‘Loop through fields to find todays date’ 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.