Support

Account

Home Forums ACF PRO Comparing current date with ACF date

Solved

Comparing current date with ACF date

  • Hello,
    I’m trying to compare two date (the current date with an ACF datepicker one) to display specific content.
    What I’m trying to achieve is to display a phrase if the ACF date is lower than the current time (in few words if a service is expired). This is my code:

    $dataScadenza = get_field('data_di_scadenza');
    $currentDateTime = date('d.m.y');
    
    if($dataScadenza <= $currentDateTime) {
    
    echo '<a href="'.get_field('link_esito').'">';
    echo '<button class="vedi_esiti">VEDI ESITI</button>';
    echo '</a>';
    
    } else {
    
    echo '<span class="ds_label">data scadenza</span>';
    echo '<div class="ds_date">'.get_field('data_di_scadenza').'</div>';
    
    }

    where $dataScadenza is the ACF date and $currentDateTime is current date. Both variables have the same output (d.m.y).

    What happens is that the comparison is applied only on day number and not on the entire date.

    Anyone can help to solve the problem?

    Thank you!

  • In order to compare dates the way your are doing it the date needs to have year first, then month, then day. ACF stores the date in the DB in the format “Ymd”

    
    // supply 3rd argument of false to get unformatted date
    $dataScadenza = get_field('data_di_scadenza', false, false);
    // use the same date format for the current date
    $currentDateTime = date('Ymd');
    
    if($dataScadenza <= $currentDateTime) {
      // ......
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Comparing current date with ACF date’ is closed to new replies.