Back to Community

Troubleshooting Common CMB2 Date and Time Field Issues

21 threads Sep 9, 2025 PluginCmb2

Content

CMB2 is a powerful tool for creating custom metaboxes in WordPress, but its date and time fields can sometimes be a source of frustration. Based on community reports, here are the most common problems and their solutions.

1. Date/Time Not Saving Correctly

This is a widespread issue with several potential causes.

Problem: Dates before 1905 or with specific formats fail to save.

Why it happens: The underlying JavaScript datepicker library and PHP date parsing functions have limitations with very old dates and certain format combinations.

Solution: Avoid using the 'text_date' type for dates before ~1905. Instead, use 'text_date_timestamp' or 'text_datetime_timestamp', which store the value as a UNIX timestamp and are more robust. Furthermore, be cautious with date formats like 'd/m/Y' that can cause parsing conflicts; using a format with a 4-digit year and hyphens or dots (e.g., 'Y-m-d') is often more reliable.

Problem: A repeatable date/time field reverts to the current date on save.

Why it happens: This is a known bug that has been reported in the CMB2 GitHub repository, particularly affecting repeatable groups with timestamp fields.

Solution: Keep CMB2 updated to the latest version, as fixes are periodically released. If the issue persists, a common workaround is to use a non-repeatable group or a different field type until a permanent fix is implemented.

2. Date/Time Picker Interface Problems

Problem: The date or time picker UI doesn't appear or function.

Why it happens: This typically occurs when a field is rendered outside of a standard CMB2 metabox context (e.g., using cmb2_get_field to render it in a custom form). The necessary JavaScript and CSS files for the picker may not be enqueued.

Solution: If you must render a field manually, ensure you also enqueue the CMB2 scripts and styles. A more robust approach is to use the standard CMB2 metabox setup and ensure the 'cmb_styles' and 'hookup' box parameters are not set to false unless you are manually handling the enqueuing.

Problem: The time picker is locked to 5-minute intervals.

Why it happens: This is the default behavior of the time picker UI.

Solution: For more granular control, like recording a duration in minutes, a 'text_time' field may not be the best tool. Consider using a standard 'text' or 'text_small' field with custom validation to accept only numbers, or a custom select dropdown with the exact minute values you need.

3. Date/Time Value Display and Retrieval Issues

Problem: A saved time value returns empty or 00:00 on the front-end, even though the database value is correct.

Why it happens: This is often a formatting mismatch. The 'text_time' field saves data in a standard format (e.g., H:i:s). If you retrieve it with get_post_meta() but expect a different format, it can lead to display errors.

Solution: Be consistent with your time format. Use the same format for retrieval that you defined when creating the field ('time_format' => 'H:i').

Problem: The date picker shows a corrupted year (e.g., 4400) in the admin when editing, but the front-end displays correctly.

Why it happens: This is a rare bug where the timestamp is being misinterpreted by the JavaScript datepicker upon reloading the post edit screen.

Solution: This appears to be an edge case bug. Ensure you are running the latest version of CMB2 and WordPress. If the problem continues, try switching the field type between text_date_timestamp and text_datetime_timestamp to see if one is more stable than the other in your environment.

4. Setting Defaults and Automating Values

Problem: Needing to set a default date/time (e.g., current time) automatically.

Solution: Use the 'default' parameter. For a dynamic default like "now," you can pass a PHP timestamp: 'default' => time(). For a date a month in the future in a repeatable group, calculate the timestamp for each new row using a hook like cmb2_default_filter rather than a static default, which will be the same for every row.

By understanding these common pitfalls, you can effectively troubleshoot and build more reliable forms with CMB2's date and time fields. For further reading, the CMB2 GitHub wiki is an excellent resource for advanced field parameters and hooks.

Related Support Threads Support