Support

Account

Home Forums General Issues Hide date field if empty

Solved

Hide date field if empty

  • Hello guys,

    Can someone give me a little help here?

    I have the following code to display date:

    <?php 
    // get raw date
    $date = get_field('date_of_birth', false, false);
    // make date object
    $date = new DateTime($date);
    ?>
    <?php echo $date->format('M j, Y'); ?> (<?php 
        $date = get_field('date_of_birth');
        $birthday = new DateTime($date);
        $interval = $birthday->diff(new DateTime);
        echo $interval->y.' years old';
    ?>)

    This display the following:

    Aug 26, 1990 (26 years old)

    How to hide if the date field is empty?

    Currently, if empty, is showing the today date:

    Jan 10, 2017 (0 years old)

    Can someone please help me? 🙂

    Thank you

  • 
    <?php 
    if (get_field('data_of_birth', false, false)) {
      // get raw date
      $date = get_field('date_of_birth', false, false);
      // make date object
      $date = new DateTime($date);
      echo $date->format('M j, Y'); ?> (<?php 
        $date = get_field('date_of_birth');
        $birthday = new DateTime($date);
        $interval = $birthday->diff(new DateTime);
        echo $interval->y.' years old';
      ?>)
    } // end if date
    
  • Hey John,

    Thank you very much for trying to help me.
    But when I replace the code, I get the following error:

    Parse error: syntax error, unexpected ‘endwhile’ (T_ENDWHILE) in /home/myusername/public_html/wp-content/themes/customtheme/single-model.php on line 62

    Any idea how to fix this? 🙂

  • There’s no while in what I posted. Generally, if you get an “Unexpected {anything}” error then the problem is a missing ; on a previous line of php, could also be other things as well. But the real error is on the lines before the line reported by PHP.

  • Hey John,

    Thank you again for your reply.
    Is too much to ask you if you can help me find the issue that you said? 🙂

    My code: http://paste2.org/paG6xhVL

    Note: You code in lines 38 to 50.

  • I see the problem now, sorry, it was my fault, forgot to put the end of the if statement inside of php

    
    <?php 
    if (get_field('data_of_birth', false, false)) {
      // get raw date
      $date = get_field('date_of_birth', false, false);
      // make date object
      $date = new DateTime($date);
      echo $date->format('M j, Y'); ?> (<?php 
        $date = get_field('date_of_birth');
        $birthday = new DateTime($date);
        $interval = $birthday->diff(new DateTime);
        echo $interval->y.' years old';
      ?>)<?php 
    } // end if date
    ?>
    
  • Thank you John 🙂

    I tried your new code and the error disappeared but…
    Not only the empty field appears hiding but also the ones with data.
    What could be? How to hide only the empty fields?

    P.S. Sorry mate, if this is giving you too much work.

  • To be honest, I don’t see anything. As far as I know it should only be skipping the fields with no value.
    Try adding this before the if statement to see exactly what is being returned for each value

    
    var_dump(get_field('data_of_birth', false, false));
    
  • Hey John,

    Here what appears in both posts (empty and not)

    bool(false)

    Weird 🙂

  • That means that the field for the post you’re getting the values for has no value set. Check the field name, that’s the only thing I can think of.

  • Hey John,

    I have found the issue in your code:

    if (get_field('data_of_birth', false, false)) {

    You put “data” instead of “date” 🙂

    Now is working perfectly. Thank you very much mate!! 😀

  • Ah. It’s funny how you read what you expect to be there 😛

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

You must be logged in to reply to this topic.