Home › Forums › Front-end Issues › Incorrect return format from date and time
Hi
I’m using two custom fields from ACF, one date picker and one time picker. I’m also using a block theme and Full Site Editing. When displaying the date and time on the front-end I get the incorrect format. Date is YYYYMMDD and time for example 19:00:00 instead of the settings I use in ACF.
To get ACF to work with Full Site Editing I used the following PHP snippet for date and a similar for time:
register_meta(
'post',
'start_date',
array(
'object_subtype' => 'event',
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'sanitize_callback' => 'wp_strip_all_tags'
)
);
I also got some help with trying to correct the date format using this PHP snippet instead:
register_meta(
'post',
'start_date',
array(
'object_subtype' => 'event',
'show_in_rest' => array(
'schema' => array(
'format_callback' => function($meta_value, $meta_key, $object_type, $object_subtype, $field_args) {
return date('F j, Y', strtotime($meta_value));
},
),
),
'single' => true,
'type' => 'string',
'sanitize_callback' => 'wp_strip_all_tags'
)
);
On the template itself for displaying the date and time fields on the front-end I used the new block binding, biding it to a paragraph block like so:
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"start_date"}}}}} -->
<p></p>
<!-- /wp:paragraph -->
I should probably mention that I don’t know PHP. For the snippets I used guides on how to achieve custom fields with the new block binding feature.
Is there a way to get the correct date and time format on the front-end? Thank you in advance!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.