0

I'm developing a small online-courses website for a friend and got stuck with ACF repeater field!

Here's my code to display the course dates table:

<?php 
$rows = get_field('dates');
$i = 1;

if($rows) { ?>
    <table class="specs">
        <thead>
            <tr>
                <th colspan="5">Course Dates</th>
            </tr>
        </thead>
        </tbody>
            <?php                               
            foreach($rows as $row) { 
                echo '<tr>';
                echo '<td>' . $i . '</td>';
                echo '<td>' . $row['start_date'] ;

                // Check, if "end_date" field is not empty
                if( $row['end_date']) {
                    echo ' - ' . $row['end_date'] . '</td>';
                } else {
                    echo '</td>';
                }

                echo '<td>10am – 5pm</td>';
                echo '<td>£' . $row['course_price'] .'</td>';
                echo '<td><a href="#" class="button orange">Book Now</a></td>';
                echo '</tr>';
                $i++;
            }; ?>
        </tbody>
    </table> 

<?php }

This code outputs a nice table:

OUTPUT

The only thing I can't figure out is how to check ['start_date'] sub_field against current date and display courses in the loop with valid dates only.

So, basically, I need to auto delete or just exclude from the loop a whole table row if the [start_date] sub_field is not valid any more!

Is there any workaround for this?

1
  • What have you tried? What didn't work? (Both questions are prerequisites for asking questions on this site). Have you searched their docu? Have you tried their forums?
    – kaiser
    Aug 30, 2013 at 10:16

2 Answers 2

0

Possibly better than strtotime, as you already know the date format would be date_create_from_format(). Something like:

$course_start = date_create_from_format('dmY',$row['start_date']);
if ($course_start > date('dmY')){
        echo '<tr>';
        echo '<td>' . $i . '</td>';
        echo '<td>' . $row['start_date'] ;

        // Check, if "end_date" field is not empty
        if( $row['end_date']) {
            echo ' - ' . $row['end_date'] . '</td>';
        } else {
            echo '</td>';
        }

        echo '<td>10am – 5pm</td>';
        echo '<td>£' . $row['course_price'] .'</td>';
        echo '<td><a href="#" class="button orange">Book Now</a></td>';
        echo '</tr>';
        $i++;
    }
1
  • Thank you for your suggestion! Had to modify your piece of code to make it work. Here is my code: $course_start = DateTime::createFromFormat('d/m/Y',$row['start_date'])->format('Y-m-d'); if ($course_start > date('Y-m-d')){ } ` Sep 13, 2013 at 22:55
0

I assume "not valid" means it's in the past?! Then you would be searching for a condition like this:

 if( strtotime($row['start_date']) > time() ) { ....

in your context:

    <?php                               
    foreach($rows as $row) { 
        if( strtotime($row['start_date']) > time() ) {
            echo '<tr>';
            echo '<td>' . $i . '</td>';
            echo '<td>' . $row['start_date'] ;

            // Check, if "end_date" field is not empty
            if( $row['end_date']) {
                echo ' - ' . $row['end_date'] . '</td>';
            } else {
                echo '</td>';
            }

            echo '<td>10am – 5pm</td>';
            echo '<td>£' . $row['course_price'] .'</td>';
            echo '<td><a href="#" class="button orange">Book Now</a></td>';
            echo '</tr>';
            $i++;
        }
    }; ?>

General advice: Be carefull with strtotime() though. It can be picky what kind of dates formats work and deliver unexpected results. In most of the cases it's better to save dates as a proper format in the database (i.e. unix timestamp or sql date fields).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.