Support

Account

Home Forums Front-end Issues Date output

Solving

Date output

  • How do i return a date like this 2013-08-25T09:00-12:00

    when i have it outputting someplace else like this dd.mm.yy or this D, dd M yy

    i need it for schema info to show. can you please tell me how to output different formats then the ones being used.

    the full code example please.

    thanks you guys are great.

    • Elliot

    • July 9, 2013 at 10:00 am

    Hi @madsynn

    You can format the date value into any format you with thanks to the great PHP date / time functions.

    I have written up some code here:
    http://www.advancedcustomfields.com/resources/field-types/date-picker/

    And have extended it a little bit for you below:

    All you need to do is change the format_out and you can do that by finding the format string here:
    http://www.php.net/manual/en/function.date.php

    
    <?php 
    
    /*
    *  Create PHP DateTime object from Date Piker Value
    *  this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
    */
    
    $format_in = 'Ymd'; // the format your value is saved in (set in the field options)
    $format_out = 'd-m-Y'; // the format you want to end up with
    
    $date = DateTime::createFromFormat($format_in, get_field('date_picker'));
    
    echo $date->format( $format_out );
    
    ?>
    
  • I’m having this same issue, trying to get the date to automatically display formatted like “Mon July, 29”.

    If I use the code above, it breaks the page and nothing will load. Any thoughts on this? I love this plugin by the way!

    Here is the code that I’m using. The “onsale” field is the Date Picker and I would like to reformat how it displays on the page. Thanks!

    <?php
    $onsale_date = strtotime(get_field(‘onsale’));
    $now = time();

    if ($onsale_date > $now) {
    ?>
    On Sale

    <?php
    echo get_field( ‘onsale’ );
    }
    else { echo get_field( ‘price’ );
    }
    ?>

    • Elliot

    • July 17, 2013 at 2:55 pm

    Hi @calebingersoll

    The above code will break if you have an incorrect value, or if your server PHP version does not have the datetime functionality (I think its v5.3 and above)

    A for the code you have posted, have you debugged it? Have you tested the variable $onsale_date?

  • Thanks Elliot,

    I upgraded my php to 5.3 and no change. The code I posted works great, it’s just when I try to reformat the display date, it breaks. Maybe I’m missing something on how I’m implementing it.

    Where in my above code would I add this code to reformat? Also, did I customize this correctly to fit my use?

    $date = DateTime::createFromFormat(‘Ymd’, get_field(‘onsale’));
    echo $date->format(‘D, m y’);

    Thanks again!

    • Elliot

    • July 18, 2013 at 12:43 pm

    Hi @calebingersoll

    Have you debugged your code?

    
    <?php 
    
    echo '<pre>';
    	print_r($date);
    echo '</pre>';
    die;
    
     ?>
    
  • Sorry to revive an old thread but I pulled your code from the example above;

    <? if (get_field('date-start')) : ?>
      The next opportunities to undertake this workshop are: <br/>
      <?
      $format_in = 'yymmdd'; // the format your value is saved in (set in the field options)
      $format_out = 'D d MM'; // the format you want to end up with
    
      $date = DateTime::createFromFormat($format_in, get_field('date-start'));
      echo get_field('date-start');
      //echo $date->format( $format_out );
      echo '$date: <pre>';
    	print_r($date);
      echo '</pre>';
    
      endif;
    ?>
    

    Outputs;

    The next opportunities to undertake this workshop are:
    20150725$daten: 

    ie the date-start field is valid.
    if I uncomment this line //echo $date->format( $format_out );

    I get a fatal error.

    PHP 5.5

    • TiborP

    • January 5, 2015 at 9:45 pm

    I found out that when using ACF Pro (5.1.5) does already convert the date field’s content to a string, so you will get a fatal error (Call to a member function format() on a non-object) when using

    
    $date = DateTime::createFromFormat($format_in, get_field('date-start'));
    

    What does work is

    
    $date = new DateTime( get_field('date-start') );
    
  • Thanks Tibor… looks like this solution is necessary with the regular version of ACF as well. Hopefully the documentation will update soon.

    • cchiera

    • March 30, 2015 at 8:27 am

    While none of that complex code above seemed to work, simply using this did for me:

    <?php $date = get_field('my-date');
    $date2 = date("F j, Y", strtotime($date)); ?>
    
    <?php echo $date2; ?>
  • Where can I place this code? (Sorry, I don’t have experience with this.)

    I ‘m using WordPress 4.1.1 theme: Twenty Fifteen.

    Who can help me?

    • Kailo

    • November 25, 2015 at 12:11 am

    This one works for me. Thank you!

  • @cchiera nice!

  • Hi,

    I am new to WordPress and PHP and got stuck while using ACF.

    I want to use a custom date field for my event section.

    And on the front-end I only want to show the 3 letter of the month and nothing else but it won’t work. Please find below what I am writing in PHP.

    <?php
    $eventDate = new DateTime(get_field(‘event_date’));
    echo $eventDate->format(‘M’)
    ?>

    the error I am getting is…

    Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (26/09/2019) at position 0 (2): Unexpected character in /app/public/wp-content/themes/fictional-university-theme/front-page.php:30

  • Hi nishantsapra,

    This is a very old post and seems to be solved even though it looks pending. Please start a new thread so that you can get the help you need!

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.