I am using CMB2 and following code to select date & time:

$cmb->add_field( array(
        'name' => __( 'Test Date/Time Picker Combo (UNIX timestamp)', 'cmb2' ),
        'desc' => __( 'field description (optional)', 'cmb2' ),
        'id'   => $prefix . 'datetime_timestamp',
        'type' => 'text_datetime_timestamp',
    ) );

That code give me this in admin panel:

date and time admin

To retrieve this meta data I am using following code:

$text = get_post_meta( get_the_ID(), '_cmb2_event_date_prefix_datetime_timestamp', true )
echo esc_html( $text );

But this gives me a number as follow not the date and time I selected in admin panel.


Why is this ? How can I get the date and time I selected in that meta box.

  • 1
    It is a unix timestamp, an you need to convert into your needs. You can try date_i18n function.
    – Jevuska
    Mar 10, 2016 at 8:12

3 Answers 3


The date/time related field types for CMB2 store all their values as Unix timestamps in the database with the exception of text_datetime_timestamp_timezone which stores it's value as a serialized DateTime object.

  • text_date_timestamp Date Picker (UNIX timestamp)
  • text_datetime_timestamp Text Date/Time Picker Combo (UNIX timestamp)
  • text_datetime_timestamp_timezone Text Date/Time Picker/Time zone Combo (serialized DateTime object)

See: https://github.com/WebDevStudios/CMB2/wiki/Field-Types

What you see in the metabox is a conversion from the Unix timestamp to a human readable format which I believe you can adjust to your liking using the date_format key when calling $cmb->add_field().

In your case, all you need to do is pass your timestamp through PHP's date() function to format the result as you desire.


$text = get_post_meta( get_the_ID(), '_cmb2_event_date_prefix_datetime_timestamp', true )
echo date('Y-m-d H:i:s A', $text ); // results in 2016-03-07 08:40:00 AM

See the documentation on PHP's date() function for formatting instructions:


You're almost there. The number you see is a called Unix Timestamp.

You can easily convert it using date_i18n like this:

<?php echo date_i18n(get_option( 'date_format' ), $text); ?>

It seems that CMB2 is sabving dates in unix timestamp format, which IMHO, is a really good thing. This makes sorting and searching a breeze when you need to sort or search data according to dates.

It is really easy to convert unix timestamps into any needed format. As from PHP 5.3, you can use the DateTime class for these type of convertions

$text = get_post_meta( get_the_ID(), '_cmb2_event_date_prefix_datetime_timestamp', true );
$date = DateTime::createFromFormat( 'U', $text );
echo $date->format( 'd/m/Y H:i');

Just make sure about the output format, we in South Africa reads 03/07/2016 as the 3rd of July 2016, I know some countries read it as the March the 7th 2016. If the latter, then change your format to m/d/Y H:i


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.