Support » Plugin: CMB2 » Using WordPress’s date/time format settings

  • Resolved Morgan Kay

    (@gwendydd)


    I have a plugin that relies on CMB2.

    That plugin does a lot of stuff with dates, and I have found that users are really picky about date/time formats. My plugin grabs the date/time formats from WordPress’s general settings, and uses those formats everywhere.

    However, I cannot get those settings to work within a metabox with a text_datetime_timestamp field. If I add a ‘date_format’ parameter to that field, the field data will not save. Additionally, if the date format is set to ‘MM j, yy’, the format is displayed as ‘MayMay 05, 1717’ instead of ‘May 05, 2017’.

    In other words, if I use this code:

    $shift_meta->add_field( array(
        'name' => __( 'Scheduled Start Date/Time', 'employee-scheduler' ),
        'id'   => $prefix . 'shift_start',
        'type' => 'text_datetime_timestamp',
        'date_format' => 'MM j, yy'
     ) );

    the date will not save, and the format is displayed incorrectly.

    Other people have reported these issues:
    https://github.com/CMB2/CMB2/issues/765
    https://github.com/CMB2/CMB2/issues/646

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Are you basing your format from JavaScript or from PHP?

    Screen Shot

    I got it to match the format you’re looking for with 'date_format' => 'M j, Y' as the documentation for the field type states it takes PHP’s formatting.

    http://php.net/manual/en/function.date.php#refsect1-function.date-parameters

    Thanks for getting back to me!

    I must have been using JS time formats (it was several months ago when I was working on this).

    Things get really funky if I use European date formats. I currently have this as my metabox field:

    $shift_meta->add_field( array(
        'name' => __( 'Scheduled Start Date/Time', 'employee-scheduler' ),
        'id'   => $prefix . 'shift_start',
        'type' => 'text_datetime_timestamp',
        'date_format' => get_option( 'date_format' ),
        'time_format' => get_option( 'time_format' )
    ) );

    Under Settings–>General, I have d/m/Y and H:i. d/m/Y is one of WordPress’s default date options, and is a fairly common date format in Europe. When I save the post, the date is blank. I’m pretty sure I know why – my guess is that CMB2 validates the field by running strtotime, and doesn’t save the field if it doesn’t get a valid timestamp. However, PHP doesn’t recognize d/m/Y as a valid time format – if the date separator is a /, PHP assumes it is American month/day/year, and if the date separator is a -, PHP assumes it is a European day/month/year. So even though WordPress presents that as a valid date format, PHP gets confused and rejects it.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Yah, unfortunately JS formats will not work. CMB2 does the work of transforming most of the php formats to the JS equivalents, but I have no doubt it doesn’t handle all variants, as there are so many. As for your issue, it sounds like the best bet would be to do some translation of the get_option( 'date_format' ) values before you set them as the parameter for the CMB2 field.

    Thanks, Justin!

    I have put in a filter to check for European date formats and put them in a format that PHP recognizes, but I’m running into a new issue.

    Putting in the date format F j, Y results in this: https://s.nimbus.everhelper.me/share/1209522/r9lfpqlbfo6c30u68q18 Where it should say October 4, 2017, it says F 4 2017.

    That happens whether I get that format from the settings, or put it in directly as 'date_format' => 'F j, Y',

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not completely sure why, though I suspect it’s a case of the timestamp conversion results in an empty string, changing the format to use the F is not saving any meta data.

    Correct – changing the format to ‘F’ means that the field gets filled with ‘F 4 2017’ instead of ‘October 4 2017’, and then the data doesn’t save because it isn’t a valid time format.

    This seems to me like a bug in CMB2 because ‘F’ is a valid time format. Any idea why this is happening?

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Part of an issue in general, not necessarily specific to CMB2, is that PHP and JavaScript follow different formatting standards, and the date/time based fields utilize jQuery UI. Nothing changed on my end since last night, but I haven’t tried to dive back into it again yet either.

    Right. I suspect that somewhere CMB2 isn’t correctly translating between PHP and jQuery UI time formats.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Ok, I have updated CMB2 to translate PHP date formats to JS ones in more contexts. In my testing, your format (F j, Y) will work in the next release. https://github.com/CMB2/CMB2/commit/7212e878dd74e393ab70fd5b0ebff302c02d1a41

    You’re awesome! Thank you!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Using WordPress’s date/time format settings’ is closed to new replies.