Support

Account

Home Forums Front-end Issues ACF Date Picker (date output)

Solving

ACF Date Picker (date output)

  • Hi,

    Iā€™m using the ACF Date Picker field, but i have a problem with the date output language.
    Front-end is in english, but WordPress which is configured in wp-config.php is in french (it must be). What you can do to get the date displayed on the website in english?

    Thanks
    Tsering

  • Hi,

    when you’re editing your custom date field, you can see “Display format” on the bottom. This will format the display of the date.

    http://www.advancedcustomfields.com/resources/field-types/date-picker/

    If you’re setting the format in wp backend, use JS date format, but you can also format the date later in PHP – use the code from example

    $date = DateTime::createFromFormat('Ymd', get_field('date_picker'));
    echo $date->format('d-m-Y');
  • Thanks for the reply, but I didn’t mean the date format. I mean the language, that is, instead of “Juin” (in french), “June” (in english). Is it possible?

  • Aha, sure, just use the WordPress function for formatting dates in different languages.

    $dateformatstring = "l d F, Y";
    $unixtimestamp = strtotime(get_field('date_picker'));
    echo date_i18n($dateformatstring, $unixtimestamp);

    This is dependent on your blog language setting. More info here: http://codex.wordpress.org/Function_Reference/date_i18n

    If you can’t set blog language to french, you can also do it with PHP only.

    setlocale(LC_ALL, 'fr_FR');
    echo strftime("%A %e %B %Y");

    Another example with day of week and month (french):

    setlocale(LC_ALL, 'fr_FR');
    echo strftime("%A le %d %B, %Y");
    //with day of the week = mercredi le 18 septembre, 2013
  • Thanks,

    How to implement it using the ACF?
    `<?php the_sub_field(‘data_picker’); ?>

    I’m sorry, but it’s a little bit too difficult for me…

  • Ok, so let’s say ‘data_picker’ is your date field name in ACF. Set the locale before you go into the loop:

    <?php setlocale(LC_ALL, 'fr_FR'); ?>

    Then inside the loop do these steps:

    // FIRST, SET THE WAY YOU WANT TO FORMAT DATE
    // DETAILS HERE - https://php.net/strftime
    $dateformatstring = "%A le %d %B, %Y";
    
    // THEN, CONVERT THE FIELD FROM ACF TO UNIX TIMESTAMP
    $unixtimestamp = strtotime(get_field('data_picker'));
    
    // NOW ECHO THE FRENCH DATE USING PHP'S strftime
    echo strftime($dateformatstring, $unixtimestamp);
  • And if you have your blog set to french, you can just do these steps in the loop:

    // FIRST, SET THE WAY YOU WANT TO FORMAT DATE
    // DETAILS HERE - http://php.net/manual/en/function.date.php
    $dateformatstring = "l d F, Y";
    
    // THEN, CONVERT THE FIELD FROM ACF TO UNIX TIMESTAMP
    $unixtimestamp = strtotime(get_field('data_picker'));
    
    // NOW ECHO THE FRENCH DATE USING A WORDPRESS FUNCTION
    echo date_i18n($dateformatstring, $unixtimestamp);
  • Thank you, but it doesn’t work.

  • Try this with the first (PHP) solution (replace the setlocale line):

    setlocale (LC_ALL, 'fr-FR.utf8','fra');

    There’s a bunch of locale codes … the top one works, but here’s a big discussion on this topic: Zut alors! Mon dieu! šŸ™‚

  • I don’t know what I’m doing wrong, but it still doesn’t work. Below my original code:

    <?php query_posts( array( 'post_type' => 'custom_post_name', 'post_status' => 'publish', 'posts_per_page' => 1 ) ); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php if(have_rows('repeater_field_name')): ?>
    	<?php while( have_rows('repeater_field_name') ): the_row(); ?>
    
    			<?php the_sub_field('date_picker'); ?>
    
    	<?php endwhile; ?>
    	<?php endif; ?>
    
    <?php endwhile; endif; ?>
  • And what is the current output of your code? What do you see in the browser?

  • I see the name of the month in french, and I would in english šŸ™

  • Ha šŸ™‚ I thought you were trying to get it in french the whole time…

    No big deal, just change the “setlocale” line of code to this:

    setlocale(LC_ALL, array('en_GB.UTF8','en_GB','english'));

    This sets up everything for UK english. If you want USA english:

    setlocale(LC_ALL, array('en_US.UTF8','en_US','english'));

    You can specify the values either in an array (like i did here) or as a sequence of string (as shown above with the french example).

  • This is my final code, but it also does not work:

    <?php setlocale(LC_ALL, array('en_US.UTF8','en_US','english')); ?>
    
    <?php query_posts( array( 'post_type' => 'custom_post_name', 'post_status' => 'publish', 'posts_per_page' => 1 ) ); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php if(have_rows('repeater_field_name')): ?>
    	<?php while( have_rows('repeater_field_name') ): the_row(); ?>
    
    			// ORIGINAL <?php the_sub_field('date_picker'); ?>
                
    						<?php
    						$dateformatstring = "M";
    						$unixtimestamp = strtotime(get_sub_field('date_picker'));
    						echo date_i18n($dateformatstring, $unixtimestamp);
    						?>            
                     
                
    
    	<?php endwhile; ?>
    	<?php endif; ?>
    
    <?php endwhile; endif; ?>
  • You’re not using the correct piece of code. If you want to use date_i18n, you have to set the language of your WordPress to English. This is a WP specific function.

    To use the setlocale method, use this code inside the loop (the setlocale on top is OK):

    // FIRST, SET THE WAY YOU WANT TO FORMAT DATE
    // DETAILS HERE - https://php.net/strftime
    $dateformatstring = "%A le %d %B, %Y";
    
    // THEN, CONVERT THE FIELD FROM ACF TO UNIX TIMESTAMP
    $unixtimestamp = strtotime(get_field('data_picker'));
    
    // NOW ECHO THE ENGLISH DATE USING PHP'S strftime
    echo strftime($dateformatstring, $unixtimestamp);
  • Thakns.

    Updated code here:

    <?php setlocale(LC_ALL, array('en_US.UTF8','en_US','english')); ?>
    
    <?php query_posts( array( 'post_type' => 'custom_post_name', 'post_status' => 'publish', 'posts_per_page' => 1 ) ); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php if(have_rows('repeater_field_name')): ?>
    	<?php while( have_rows('repeater_field_name') ): the_row(); ?>
                
    						<?php
    						$dateformatstring = "%A %d %B, %Y";
    						$unixtimestamp = strtotime(get_sub_field('date_picker'));
    						echo strftime($dateformatstring, $unixtimestamp);
    						?>            
                     
                
    
    	<?php endwhile; ?>
    	<?php endif; ?>
    
    <?php endwhile; endif; ?>

    But it shows me everywhere “Thursday 01 January, 1970 “, instead of getting from the ACF Date Picker Fields.

  • Well, you probably changed the default display of the date in ACF field settings. The php function strtotime cannot work with french dates.

    Go to ACF field settings for date_picker and set it to YYYYMMDD

  • Ok, I understand.

    enorog, thank you for your support.

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

The topic ‘ACF Date Picker (date output)’ is closed to new replies.