Support

Account

Home Forums Front-end Issues Incorrect return format from date and time

Helping

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!

  • I reached out to support and I got a quick answer! The solution is here:

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

You must be logged in to reply to this topic.