ACF - Fatal error

Hi,

Not sure if you can help with this. I’m trying to have a custom field for posts, to have the date field I’m using the ACF that comes with X, I’m trying to reference that fields as below:

<?php

$event_date = new DateTime(get_field('event_date'));
      echo $event_date->format('M')
?>

When I’m not referencing the get_field('event_date') it just work fine to show the current date as per PHP function DateTime(); when I do reference it I’m getting the following error.

:  Uncaught Exception: DateTime::__construct(): Failed to parse time string (31/12/2018) at position 0 (3): Unexpected character in /Applications/MAMP/htdocs/cs-dev/wp-content/themes/fictional-university-theme/front-page.php:31
Stack trace:
/Applications/MAMP/htdocs/cs-dev/wp-content/themes/fictional-university-theme/front-page.php(31): DateTime->__construct('31/12/2018')
/Applications/MAMP/htdocs/cs-dev/wp-includes/template-loader.php(74): include('/Applications/M...')
 /Applications/MAMP/htdocs/cs-dev/wp-blog-header.php(19): require_once('/Applications/M...')
 /Applications/MAMP/htdocs/cs-dev/index.php(17): require('/Applications/M...')
 {main}
      thrown in 

How can I just make it work the get_field(); to show them on FE?

Thanks in advance!

Hello @abdurrehman,

Thanks for writing in!

With ACF PRO, it is only the_field() function.
Please check out how you can display the fields from this article: https://theme.co/apex/forum/t/integrated-plugins-acf-pro/49

Hope this helps.

It does have the get_field(); check out here

It’s not just working when I try to change the date format because with only the_field(); it shows the complete day.

Any suggestions?

Thanks

Hi @abdurrehman,

The question is, what date format is being stored and retrieved by get_field();? And that format is probably not understood by DateTime().

Based on the error, it doesn’t accept the format 31/12/2018, please check this https://stackoverflow.com/questions/24466794/fatal-error-uncaught-exception-exception-with-message-datetime-construct.

The format that DateTime() is accepting is mm/dd/yy or mm-dd-yy (month first). Even by manually debugging it, your date format isn’t working (eg. print_r( date_create('31/12/2018') ); ). So pleased edit your custom field for date and change its format, then you’ll have to manually change the already saved dates.

Thanks!

It worked! Awesome @Rad thanks a lot!

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.