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:

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.
1457368800
Why is this ? How can I get the date and time I selected in that meta box.
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.
Example:
$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:
- http://php.net/manual/en/function.date.php