Support

Account

Home Forums ACF PRO Date Picker comparing two dates

Solved

Date Picker comparing two dates

  • Hello

    I have a custom Date Picker field.
    I want to compare the custom field with the current date.
    example:

    $date_now = date('d/m/Y');
    $date = get_field('scadenza');
    
    if ($date < $date_now) {
      $class_data = 'old-post';
    	}
    	else {
    		$class_data = 'new-post';
    	}

    How can I fix it?
    thank you

  • Hi Simov,
    Find my code. Not sur it’s the cleanest way but it works.
    I use ‘specials’ CPT and i want display them only is the date is valid.

    
    <?php 
    $datedeb = get_field('debut-promo', false, false);
    $datefin = get_field('fin-promo', false, false);
    $today = strtotime(date('Ymd'));
    $promoend = strtotime($datefin);
    $datedeb = new DateTime($datedeb);
    $datefin = new DateTime($datefin);
    ?>			
         <?php if ($promoend >=$today): ?>
              // your code here
         <?php endif; ?>	
    

    Hope that will help you.
    cheers,
    Pierre-Yves

  • Hi @simow,


    @guinnessboy
    solution should work just fine.

    Also you may conisdering filtering your posts by date.

    $today = date(“Y-m-d”);
    echo $today;

    $args = array(
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => -1,
    ‘meta_key’ => ‘event_date’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘ASC’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘event_date’,
    ‘value’ => $today,
    ‘type’ => ‘DATE’,
    ‘compare’ => ‘>’,
    )
    )
    );
    Hope this helps.

  • Thanks
    problem solved!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Date Picker comparing two dates’ is closed to new replies.