Custom Fields
Re-Envisioned
Support MB Admin Columns Admin Column - Date Format

  • Creator
    Topic
  • #31272
    Resolved uptwo
    Participant

    Hey,
    currently I save the format of my datapicker as 'Y-m-d' (bc of filtering) and jsoptions as 'dd.mm.yy'.

    However, the problem is that in the admin columns 'Y-m-d' is used. I know that is because it is stored that way in the database but in my opinion is very impractical.
    Would there be the possibility to implement something like:

    admin_columns' => [
                        'dateFormat' => 'dd.mm.yy',
    ],

    Or is there a way I haven't seen yet to take the jsoptions as admin column format?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #31274
    Long Nguyen
    Moderator

    Hi,

    Please refer to this tutorial to change the display another date format https://docs.metabox.io/using-two-date-formats/

    Let me know how it goes.

    #31292
    uptwo
    Participant

    Hey Long, thanks for your fast reply!

    But that doesn't change my initial problem!

    My Date Value as saved as Y-m-d in the Database.

    The display for the date is saved as d.m.Y.

    In the Admin Columns the Date is shown as Y-m-d (Database Format).

    But i want to display the date in the admin columns as the display date (d.m.Y)! Without changing the database formatting.

    I guess this isn't possible?

    #31293
    uptwo
    Participant

    See additional screenshots:

    Admin Columns Format:
    https://ibb.co/Xtjg8x8

    Display Format:
    https://ibb.co/0mVKNwN

    Wanted Result (without changing the database format):
    https://ibb.co/f4Cg3Mj

    #31306
    Long Nguyen
    Moderator

    Hi,

    The admin column uses the helper function rwmb_the_value to show the field value. You can use this filter hook to change the date format when showing on the backend. For example:

    add_filter( 'rwmb_the_value', function( $output, $field, $args, $object_id ) {
        $value = rwmb_meta( 'date_picker', $args, $object_id );
        if( is_admin() ) {
            $output = date( 'd F, Y', strtotime( $value ) );
        }
    
        return $output;
    }, 11, 4 );

    Admin column: https://share.getcloudapp.com/kpu6kg7g
    Database: https://share.getcloudapp.com/BluDg9Y7

    Read more on the documentation https://docs.metabox.io/filters/#rwmb_the_value

    #31309
    uptwo
    Participant

    Absolutely wonderful! Thank you very much for your awesome support, again and again!

    #31327
    uptwo
    Participant

    Another Problem has occurred, now every admin column is taken from this specific field 😀

    See screenshot: https://ibb.co/wyWwYpX

    #31333
    Long Nguyen
    Moderator

    Hi,

    If you have more admin columns for other fields, you can add the condition to show for a specific field as well.

    add_filter( 'rwmb_the_value', function( $output, $field, $args, $object_id ) {
        $value = rwmb_meta( 'date_picker', $args, $object_id );
        if( is_admin() && $field['id'] == 'date_picker') {
            $output = date( 'd F, Y', strtotime( $value ) );
        }
    
        return $output;
    }, 11, 4 );

    Get the field settings from the variable $fields
    https://docs.metabox.io/field-settings/#general

    #31335
    uptwo
    Participant

    And again, thank you very much. Makes sense! You and the whole meta box team are wonderful

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.