0

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);
2
  • you shouldn't be storing dates in the database based on how you want to format/display them on the frontend, dates can always be reformatted on the fly, and WP already does this. You should store database friendly timestamps instead, that way you can query those values and then display them on the frontend in any timezone or format you want without forcing everybody to show them the same, or do an expensive bulk migration to change the database
    – Tom J Nowell
    Commented Dec 29, 2022 at 2:00
  • Thank you Tom, that is indeed what I want to achieve. I want to store the dates in the database in whatever default format makes sense (I don't know what it is, but I assume it is the default of the custom field, i.e. 'Y-m-d', or am I wrong to assume that? In which case please let me know what it should be). But the core of my question is once I have the date in the database in whatever format is appropriate to the database, I want to display it in a more human friendly format in the same way as Wordpress settings define.
    – nomade0
    Commented Dec 29, 2022 at 5:02

1 Answer 1

1

I don't see a function to do what you want directly, but it is not hard to retrieve the format that Wordpress has configured:

    $df = get_option('date_format');
    $tf = get_option('time_format');

You can then substitute $df wherever you had a hard-coded date format (or $tf for a time format). In your example, that would look like this:

    $timestamp = strtotime($content);
    $df = get_option('date_format');
    $hol_date = sprintf(
        '<time datetime="%1$s">%2$s</time>',
        date('c', $timestamp),
        date($df, $timestamp) );
4
  • Thank you Denise, that's useful, but not quite enough for me to figure it out as I don't speak PHP :/ I am not sure how to take that knowledge and make it into a piece of code that would convert on the fly the date format for display in that same Wordpress settings format...
    – nomade0
    Commented Dec 29, 2022 at 5:05
  • I edited my initial post with the code snippet I'm currently using to convert dates
    – nomade0
    Commented Dec 29, 2022 at 17:34
  • Ah, okay. I will edit mine as well :-) Commented Dec 30, 2022 at 8:03
  • Yay that works! Thank you very much Denise!
    – nomade0
    Commented Dec 30, 2022 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.