Back to Community

Troubleshooting CMB2 Time and Date Field Issues: Default Values, Formatting, and Saving

14 threads Sep 7, 2025 PluginCmb2

Content

Working with time and date fields in CMB2 can sometimes be tricky. Users often encounter issues with default values not appearing, formats not applying, or data not saving in the desired format. This guide compiles common problems and their solutions based on community discussions.

Common Issue 1: Default Value Not Working

Problem: A user creating a custom field type found that the 'default' parameter for a text_time field was not being applied.

Why it happens: The 'default' parameter may not be respected in all contexts, especially when the field is part of a more complex custom field implementation or when a value already exists in the database.

Solution: For a more reliable way to set a default, consider using the default_cb parameter with a callback function. For example, 'default_cb' => 'your_callback_function'. This callback can return a default value dynamically, ensuring it is set even if other methods fail.

Common Issue 2: Time Format Not Applying

Problem: Users report that the 'time_format' => 'h:i:s A' parameter does not change the format of the saved or displayed time.

Why it happens: This appears to be a known issue where the format parameter is not fully implemented for the time picker UI in some versions of CMB2.

Solution: As a workaround, you can format the time when retrieving it from the database. Use PHP's date() and strtotime() functions to convert the stored value into your desired format.

$mytime = date( 'h:i:s A', strtotime( get_post_meta( get_the_ID(), 'my_meta_key', true ) ) );

Common Issue 3: Needing to Save as a Timestamp

Problem: Users want to save a time or date as a Unix timestamp for easier querying and sorting but need to maintain a user-friendly input interface.

Why it happens: The standard text_time and text_date fields save values in a human-readable format, which is not ideal for performing meta queries in WordPress.

Solution: Use a timestamp-based field type instead. CMB2 provides several fields that save Unix timestamps automatically, offering the best of both worlds: a user-friendly UI and a sortable integer value in the database.

  • For a date only: Use text_date_timestamp.
  • For a date and time: Use text_datetime_timestamp.

These fields will save a UTC timestamp, which you can then convert to any format on the front end of your site.

Common Issue 4: Cannot Delete Field Content

Problem: For text_time fields, particularly within repeatable groups, users cannot delete the value. After manually clearing the input and moving focus away, the time picker automatically repopulates the field.

Why it happens: This is likely due to the jQuery time picker plugin's behavior, which is designed to always have a value.

Solution: This may require a custom JavaScript solution to override the default behavior of the time picker and allow for empty values. Inspecting the CMB2 source and the jQuery UI timepicker documentation would be the best course of action for developers looking to implement this.

Conclusion

Many common issues with CMB2 time and date fields can be resolved by using the appropriate field type for your data storage needs and applying formatting on output. For more persistent bugs, checking the official CMB2 GitHub repository for ongoing discussions and upcoming fixes is recommended.

Related Support Threads Support