Skip Navigation

[Resolved] Output ACF date in Views

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 4 voices.

Last updated by israelP 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1117302
ACFdate.png

I use ACF alongside views and here's my problem :
I have a date picker in the back-end to store a date with ACF. The value is stored as yymmdd.

When I use views to show a list a CPT with the upcoming date, the output is for example : 20180928.
Here's the shortcode generated in views :
[wpv-post-field name='gestion_groupe_0_date_debut']

Is there a way to output it in a d F Y way?

If I embeded the field in a php template I can use the echo date_i18n function and define format...

Thanks

#1117570

"20180928" means that date is stored as a Timestamp, just like Types Toolset's Date field as well.

However, our shortcode for our own date field can convert this to a human readable time, while when the data comes directly from the database (I see you use the post fields shortcode to display this 3rd party field) then the data is not converted, because we do not know it is a date field.

So you can, for example, get that field's value in a custom shortcode, and convert it to whatever you like, then return this and register the ShortCode in Toolset > Settings > Front End Content > Third-party shortcode arguments

Then you can use your Custom ShortCode in the View.

I have a similar example here, which will show you how to convert the actual field value:

$acf_field_value = get_post_meta( $post_id, 'your_acf_field', true );//get posts' Date field value (will retrun a timestamp)
$converted_acf_field_value = gmdate("m-d-Y", $acf_field_value);//convert timestamp to m-d-y for output

The functions used are documented further here:
https://developer.wordpress.org/reference/functions/get_post_meta/
hidden link

Later, you can return that in a ShortCode:
https://codex.wordpress.org/Shortcode_API

That ShortCode can be used to display this Date Field as a humanly readable date, anywhere you have access to that data and an existing post, of course.

#1119290
Paramètres ‹ Toolset — WordPress.png

Thanks Beda, but I must misread something or miss something but it doesn't work.

If created a shortcode :

function date_convert_func(){
$acf_field_value = get_sub_field('date_debut');
$converted_acf_field_value = gmdate("j F Y", $acf_field_value);//convert timestamp to m-d-y for output
}

add_shortcode('date_convert', 'date_convert_func');

Then I registred it in tootset (see image)

At last, I tried to output it in views :
[wpv-post-field id="[date_convert]"]

I think my problem might be that the field is actuallya sub_field for a repeater field... I saw a ticket about that (https://toolset.com/forums/topic/display-acf-repeater-fields-via-shortcode-issue/) but not sure hot to use that info...

#1119726

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - It looks like my colleague Beda has missed something here.

The value you are getting is a date string - not a timestamp.

Would you mind to share problem URL and access details with information where you want to display this date and tell me in which format you want to display it?

I have set the next reply to private which means only you and I have access to it.

#1121471

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've added following code to your current theme's functions.php file:

// Shortcode to create our ACF Pro content
function func_convrt_acf_date($atts){ 
 
 $acf_date = $atts['date'];
 
 $dateformatstring = "j F Y";
 $unixtimestamp =  strtotime($acf_date);
 return date_i18n($dateformatstring, $unixtimestamp);
 
} 
add_shortcode('convrt_acf_date', 'func_convrt_acf_date');

And within view, I'm calling the above shortcode as given under:

<td>
  [convrt_acf_date date="[wpv-post-field name='gestion_groupe_0_date_limite']"]
</td>

<td>  [convrt_acf_date date="[wpv-post-field name='gestion_groupe_0_date_debut']"] </td>

I can see not it displays formatted date. Could you please confirm it work for you:
=> hidden link

#1121684

Thanks a lot Minesh, it works ! I duplicated the code on my real site and it works (and it's working also the dev site 🙂 )

#1123941
Nos cours de perfectionnement3.png

Hi,

Instead of starting a new thread, I will update this one as the solution doesn't solve entirely my problem :
The shortcode provided tranform the unix timestamp in a readable format. But, if there's no date, it output the date of the present day (as shown in image)....

#1279543

Perfecto. My issue was wolved.
Thank you so much.