Conditional statement for dates

Im trying to create a conditional statement comparing two dates. I have a ACF custom date field as one date, and then comparing against the current date.

Below is what i have and iv echoed both $date and $currentdate and they both come out in the same format, so im not sure why it wont work. They both output in the format like: 20170105 which i beleive they need to be to compare.

?php
    $currentdate = current_time('Ymd');
    $date = get_field('course_start_date', false, false);
    $date = new DateTime($date);


    if ($currentdate  $date) {
    echo ' - Started.';  
    } else {
      echo ' - Not started.';
    }
    ?

Topic conditional-tags comparison php Wordpress

Category Web


instead of $currentdate = current_time('Ymd');

you can use $currentdate = new DateTime();

so the Final Solution should be as like given below

<?php
    $currentdate = new DateTime();
    $date = get_field('course_start_date', false, false);
    $date = new DateTime($date);


    if ($currentdate > $date) {
    echo ' - Started.';  
    } else {
      echo ' - Not started.';
    }
    ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.