-2

I want to chage date format to d-m-y means day-month-year format . I have added

'date_format' => __( 'd-m-Y', 'cmb2' )

But not working. now it's month-day-year formated . Actually i want to change date format like month-day-year to day-month-year.

mata field

Here is code:

add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );
function cmb_sample_metaboxes( array $meta_boxes ) {

    $prefix = '_single_';

    $meta_boxes[] = array(
        'id'         => 'coupons_metabox',
        'title'      => 'Coupon details',
        'pages'      => array( 'coupons', ), // Post type
        'context'    => 'normal',
        'priority'   => 'high',
        'show_names' => true, // Show field names on the left
        'fields' => array(
            array( // Text Input
                'name' => 'The date the coupon expires', // <label>
                'desc'  => 'The expiration date of the coupon. Leave if empty if the coupon doesn\'t have an expiration date.', // description
                'id'    => $prefix . 'date', // field id and name
                'type'  => 'text_date_timestamp', // type of field,
                ),
        )
    );

    // Add other metaboxes as needed

    return $meta_boxes;
   }

add_action( 'init', 'cmb_initialize_cmb_meta_boxes', 9999 );
/**
 * Initialize the metabox class.
 */
function cmb_initialize_cmb_meta_boxes() {

    if ( ! class_exists( 'cmb_Meta_Box' ) )
        require_once 'init.php';

}
4
  • The right place to ask this question is the cmb forums Commented Mar 14, 2015 at 7:44
  • 1
    Please be very clear what you want. This update renders my answer useless. Please be sure you add all info before you post a question so that no one wastes their time answering irrelevant info Commented Mar 14, 2015 at 14:53
  • Sorry for that . Actually i want to change date format like month-day-year to day-month-year.
    – Saiful
    Commented Mar 14, 2015 at 15:01
  • That is info that should be in your question, and not in a comment where nobody actually sees it Commented Mar 14, 2015 at 15:21

1 Answer 1

1

EDIT

You should be using date_create_from_format or its alias DateTime::createFromFormat() to convert on format to another. You can try the following

$date = DateTime::createFromFormat('m-d-Y', '03-09-2015');
echo $date->format('d-m-Y');

This will convert 03-09-2015 to 09-03-2015

ORIGINAL ANSWER

This is more a PHP question than Wordpress question. You can simply use str_replace to change the / in your date to -. You are not changing the format, so no need for date_format or any other date related functions

You can try something like

$date = '03/09/2015';
$modified_date = str_replace( '/', '-', $date );
echo $modified_date;

This will output 03-09-2015

2
  • See my update. Please read How to Ask and visit help center and take a tour to learn how the SE sites work, and how to ask a good question and also see what is on topic and what is not. Commented Mar 14, 2015 at 16:24
  • Sorry. How to add this function in wordpress custom meta field ?
    – Saiful
    Commented Mar 15, 2015 at 13:05

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