How to change the date and time in REST API for comments?

/**
 * Add a Formatted Date to the WordPress REST API JSON Post Object
 *
 */
add_action('rest_api_init', function() {
   register_rest_field(
      array('comment'),
      'formatted_date',
      array(
         'get_callback' = function() {
            return get_the_date();
         },
         'update_callback' = null,
         'schema' = null,
      )
   );
});

This is not working for me. I am constantly getting the following in my JSON:

formatted_date:false,

How can I show it in a format like: 28 November 2021, 15:00?

Topic wp-api date rest-api date-time api Wordpress

Category Web


In general I would be careful modifying existing endpoints, but you could try using the object passed into the callback, e.g.:

'get_callback' => function( $object ) {
    return date_i18n( __( 'j. F Y, H:i', 'wpse' ), strtotime( $object['date'] ) );
 },

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.