Hello ladies and gents,
I'm using F j, Y
(e.g. December 28, 2022) as default date format on my site (set in the admin settings). I have an event
custom post type with an event_start_date
custom field.
By default, event_start_date
outputs a date in format Y-m-d
(i.e. 2022-12-28), but I want it displayed in the same format as my default settings, i.e. F j, Y
.
I can change this default format, and know I can add some code to my function.php to convert it to another format. But what I would really like to do is convert is to whatever date format is set in the Wordpress admin settings.
That way my customer can change the date via Wordpress admin settings and it will change the format everywhere, without having to touch the code.
I haven't found anything on how to achieve that; is it possible? Thank you for your help.
PS: Furthermore, I need the date to be converted wherever it might show up: on a 'static' page, in a query loop, etc.
EDIT: Here is the current code I'm using to convert the date format on the fly. It works, but the date format is hardcoded in the script, which is not what I want ideally:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'human-friendly-date' ) !== false ) { // defines class name that will be used to identify elements with date to convert. Here 'human-friendly-date'
$timestamp = strtotime($content);
$hol_date = sprintf(
'<time datetime="%1$s">%2$s</time>',
date('c', $timestamp),
date('F j, Y', $timestamp) );
return $hol_date;
}
return $content;
}, 10, 3);