WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
It is a unix timestamp, an you need to convert into your needs. You can try date_i18n function. – Jevuska Mar 10 at 8:12
up vote 5 down vote accepted

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:

share|improve this answer
+1 for the extra info given – Pieter Goosen Mar 10 at 8:25
Thank you so much for the explanation.. – Foolish Coder Mar 10 at 8:27
No problem... :) – userabuser Mar 10 at 8:28

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); ?>
share|improve this answer
+1 for the "WordPress way" – userabuser Mar 10 at 8:27

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

share|improve this answer
+1 for the alternative method given :) – userabuser Mar 10 at 8:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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