Support

Account

Home Forums General Issues How to customize the “Date” field

Solved

How to customize the “Date” field

  • Hello !

    I need to make an agenda for a website in WordPress. I use ACF for the date and I would like to separate the day, month and year with a span or a div to change the style of the date.

    For the moment I have :

    <?php the_field('jour_de_levenement'); ?>

    The date appears but I can’t select the day to customize it. So I tried something like that but it didn’t seem to work :

    <?php $date = DateTime::createFromFormat('dFY', get_field('jour_de_levenement')); ?>
    <span class="day"><?php echo $date->format('d'); ?></span>
    <span class="month"><?php echo $date->format('F'); ?></span>
    <span class="year"><?php echo $date->format('Y'); ?></span>

    It gives me a fatal error.

    Here an image of what I want to do : http://imgur.com/JpDB2Gv

    Thanks a lot !

  • DateTime::createFromFormat requires the second parameter to be a timestamp and ACF returns an already formatted date that depends on the return/display format you’ve set for the field. You can’t use this function.

    
    // returns unformatted date
    $date = get_field('jour_de_levenement', false, false);
    // convert it to a time
    $time = strtotime($date);
    
    <span class="day"><?php echo date('d', $time); ?></span>
    <span class="month"><?php echo date('F', $time); ?></span>
    <span class="year"><?php echo date('Y', $time); ?></span>
    
  • It works ! Thanks a lot !

  • I tried adding this to query a custom post type and it gives an odd timestamp 01 January 1970:

    <?php
    $date = get_field('fromdate', false, false);
    // convert it to a time
    $time = strtotime($date);
    
    if ( get_query_var('paged') ) $paged = get_query_var('paged');  
    if ( get_query_var('page') ) $paged = get_query_var('page');
    
    $query = new WP_Query( array( 'post_type' => 'events', 'posts_per_page' => 20,  'order' => 'ASC' ) ); 
    
    if ( $query->have_posts() ) :
            
            while ( $query->have_posts() ) : $query->the_post(); ?> 
    
    <div class='events-title'>
    <span class="day"><?php echo date('d', $time); ?></span>
    <span class="month"><?php echo date('F', $time); ?></span>
    <span class="year"><?php echo date('Y', $time); ?></span>
    </div>
    
       <?php endwhile; wp_reset_postdata();
    
    else : ?>
            
            <p>There are no events at the moment</p>
    
    <?php endif;?>

    The dateformat is set up to return Ymd.

    Jon

  • Not sure why but I deleted the two datepicker fields, recreated them and now have a working date.

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

The topic ‘How to customize the “Date” field’ is closed to new replies.