Troubleshooting Common CMB2 Date and Time Field Issues
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
-
Current date/time on group fieldhttps://wordpress.org/support/topic/current-date-time-on-group-field/
-
Reset a cmb2 option after x amount of time?https://wordpress.org/support/topic/reset-a-cmb2-option-after-x-amount-of-time/
-
Issue after rendering a field using cmb2_get_fieldhttps://wordpress.org/support/topic/issue-after-rendering-a-field-using-cmb2_get_field/
-
Can’t empty text_date fieldhttps://wordpress.org/support/topic/cant-empty-text_date-field/
-
Date will not save – saves today instead.https://wordpress.org/support/topic/date-will-not-save-saves-today-instead/
-
Time picker problemhttps://wordpress.org/support/topic/time-picker-problem/
-
How to set Datepicker from current date…https://wordpress.org/support/topic/how-to-set-datepicker-from-current-date/
-
text_time return empty, database contains right valuehttps://wordpress.org/support/topic/text_time-return-empty-database-contains-right-value/
-
Using WordPress’s date/time format settingshttps://wordpress.org/support/topic/using-wordpresss-date-time-format-settings/
-
Default datetimes in repeatable fieldshttps://wordpress.org/support/topic/default-datetimes-in-repeatable-fields/
-
Date field year range settinghttps://wordpress.org/support/topic/date-field-year-range-setting/
-
Save sleceted value in hidden fieldhttps://wordpress.org/support/topic/save-sleceted-value-in-hidden-field/
-
text_date can’t save date before 1905https://wordpress.org/support/topic/text_date-cant-save-date-before-1905/
-
Repeatable Date Won't Savehttps://wordpress.org/support/topic/repeatable-date-wont-save/
-
Validation Fieldshttps://wordpress.org/support/topic/validation-fields/
-
Availability date [availability_date]https://wordpress.org/support/topic/availability-date-availability_date/
-
text:date picker – "non-repeat" function failinghttps://wordpress.org/support/topic/textdate-picker-non-repeat-function-failing/
-
Need Help: Weird Issue with Timestamps & the Datepickerhttps://wordpress.org/support/topic/need-help-weird-issue-with-timestamps-the-datepicker/
-
Date field doesn't display the value in adminhttps://wordpress.org/support/topic/date-field-doesnt-display-the-value-in-admin/
-
Date not savinghttps://wordpress.org/support/topic/date-not-saving-2/
-
An invalid form control with name=’…’ is not focusable.https://wordpress.org/support/topic/an-invalid-form-control-with-name-is-not-focusable-2/