Home Forums Front-end Issues Is my date in future ?


Is my date in future ?

  • Hello there!
    In a custom post I specify a date thanks to ACF.

    I need to compare that field to the current date. If that field is in future I want to show “Soon”.

    That is my current code :

                  $today = date("Y-m-d");
                  echo $today;
                  $date_fr = get_field('date_de_sortie_fr', false, false);
                  $date_fr = new DateTime($date_fr);
                  <span><?php echo $date_fr->format('Y-m-d'); ?></span>

    I think that my today date is not in a good format to do the comparison. Do you confirm ?
    Also, how to compare that dates ? Can you help me please ?

  • Try this, using DateTime() rather than date():

    $today = new DateTime();
    $date_fr = DateTime::createFromFormat('Ymd', get_field('date_de_sortie_fr'));
      if($date_fr > $today) echo "Soon: ";
      echo $date_fr->format('Y-m-d'); 

    NOTE This requires that you store the date field in Ymd format.

  • This should work:

    $endOfDay = strtotime("tomorrow") - 1;
    $unixtimestamp = strtotime(get_field('date_de_sortie_fr'));
    $date_fr = date("Y-m-d", $unixtimestamp);
    echo  $unixtimestamp > $endOfDay ? "soon" : $date_fr;

    EDITED: Syntax Error

  • Thank you!!
    The first solution solved my issue.
    Just for information the second didn’t…

  • Oh, did you see the missing ; at the end of $date_fr = date("Y-m-d", $unixtimestamp)? :D… sorry. If you´re working with Date Picker field, this should also works fine :).

  • I didn’t, but i’ll try to confirm your code 😉.

    Thank you again!

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

The topic ‘Is my date in future ?’ is closed to new replies.