Date field visualisation problem

Support General Date field visualisation problem

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1414
    mendiomendio
    Participant

    I want to use Date field with dd-mm-yyyy format in WP admin (spanish form), but I want to store the date in yyyy-mm-dd format to allow correct ordering and doing queries with dates.

    Right now I use: 'dateFormat' => 'dd-mm-yyyy' and this is how it show and save the date. It would usefull to use something like 'dateParse' => 'yyyy-mm-dd'. to distinguish between the saved date and how it show in the admin. Is this possible or there is a workaround?

    Thanks!

    #1418
    mendiomendio
    Participant

    Just to clarify, In other words: Is it possible to show on WP admin date field a different date format (e.g. dd-mm-yyyy) than the one saved on the database (e.g. yyyy-mm-dd) ?

    #1435
    Anh TranAnh Tran
    Keymaster

    Hi mendio,

    Yes, you can do that using the rwmb_{$field_id}_value and rwmb_{$field_id}_meta filters (see this documentation) to change the displayed value and saved value. Here is the sample code:

    add_filter( 'rwmb_ID_value', function( $value )
    {
        return date( 'Y-m-d', strtotime( $value ) );
    } );
    
    add_filter( 'rwmb_ID_meta', function( $value )
    {
        return date( 'd-m-Y', strtotime( $value ) );
    } );

    Don't forget to change ID to your field ID.

    #1442
    mendiomendio
    Participant

    Thank you Anh Tran, it's worked like a charm.
    I didn't know about those filters

    I only change your code just to avoid problems with dates when there is no date value (always return 1970-01-01)

    add_filter( 'rwmb_My_date_value', function( $value )
    {
        if ($value != '') {
    		return date( 'Y-m-d', strtotime( $value ) );
    	};
    } );
    
    add_filter( 'rwmb_My_date_meta', function( $value )
    {
    	if ($value != '') {
    		return date( 'd-m-Y', strtotime( $value ) );	
    	};
        
    } );

    Let me know if this new code is ok or there is a better way to deal with.
    Thanks again!

    #1443
    Anh TranAnh Tran
    Keymaster

    I think that works, too. I will add this question to the Documentation as this is maybe useful for others as well.

    #1445
    mendiomendio
    Participant

    Great! Thanks for the support

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Date field visualisation problem’ is closed to new replies.