Home › Forums › General Issues › Date Picker (and if date specified has passed) › Reply To: Date Picker (and if date specified has passed)
Okay, thanks for my friend Matt we managed to work this out.
Basically, my PHP skills failed me a little as $date_passed
was defined in an if statement, thus it’s not always defined and you are getting PHP notices when it is not defined.
So, he suggested I either define $date_passed
to something (null) or use isset or empty to check whether it has been defined.
So adapting my code to below, worked:
$date_passed = null;
if ( $end_date_passed_check->format('Ymd') < date('Ymd') ) {
$date_passed = 'date-passed';
}
I hope this post proves to be useful in applying specific classes to events that have passed using the ACF DatePicker.
Full code as follows:
<?php get_header(); ?>
<?php if ( ! have_posts() ) : ?>
<?php endif; ?>
<div class="container_12">
<?php
while ( have_posts() ) : the_post();
$end_date_passed_check = DateTime::createFromFormat('Ymd', get_field('event_end_date'));
$date_passed = null;
if ( $end_date_passed_check->format('Ymd') < date('Ymd') ) {
$date_passed = 'date-passed';
}
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="grid_3">
<div class="each-programme-content <?php echo $date_passed; ?>">
<a class="each-programme-content-info" href="<?php the_permalink(); ?>">
<?php if ( get_field('type_of_event') ) : ?>
<span class="type-of-event"><?php the_field('type_of_event'); ?></span>
<?php endif; ?>
<?php if ( get_field('event_start_date') && get_field('event_end_date') ) : ?>
<?php $start_date = DateTime::createFromFormat('Ymd', get_field('event_start_date'));
$end_date = DateTime::createFromFormat('Ymd', get_field('event_end_date')); ?>
<h4 class="date"><?php echo $start_date->format('d'); ?>—<?php echo $end_date->format('d F'); ?></h4>
<?php elseif ( get_field('event_start_date') ) : ?>
<?php $start_date = DateTime::createFromFormat('Ymd', get_field('event_start_date')); ?>
<h4 class="date"><?php echo $start_date->format('d F'); ?></h4>
<?php endif; ?>
<h4><?php the_title(); ?></h4>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="clear"></div>
<?php get_footer(); ?>
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.