Support

Account

Home Forums General Issues Date format when looping authors?

Solved

Date format when looping authors?

  • I have a datepicker field as custom field for my authors called “antallningsdatum”.
    The code below shows dates as Ymd

    <?php
    $args  = array(
    'meta_key' => 'antallningsdatum',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'
    );
    $author_query = new WP_User_Query( $args );
    $authors = $author_query->get_results();
    $user_id = get_the_author_meta('ID');
    foreach( $authors as $author ):
    
    echo '<li class="anstallningsdatum">'. get_the_author_meta( 'antallningsdatum', $author->ID ).'</li>';
    
    ;?>
    <?php endforeach;?>

    I want to display the date as d-m-Y instead, how to achieve this?

  • Hi @dillehal

    One way to achieve this would be to set d-m-Y as the return format. This option is available on the field options from the field group edit screen. Then use get_field(...) or the_field(...) function to display this on your frontend.

    Alternatively, you could use $date = DateTime::createFromFormat(…) then $date->format(‘d-m-Y’); to format the date. Check out this link for more information: http://php.net/manual/en/datetime.createfromformat.php

  • Hmm, not sure how to change my code in order to use those examples.. Could you alter this code?

    <?php
    $args  = array(
    'meta_key' => 'antallningsdatum',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'
    );
    $author_query = new WP_User_Query( $args );
    $authors = $author_query->get_results();
    $user_id = get_the_author_meta('ID');
    foreach( $authors as $author ):
    
    echo '<li class="anstallningsdatum">'. get_the_author_meta( 'antallningsdatum', $author->ID ).'</li>';
    
    ;?>
    <?php endforeach;?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Date format when looping authors?’ is closed to new replies.