-
Notifications
You must be signed in to change notification settings - Fork 566
Description
Describe the bug
On entering some dates the text_date_timestamp field type will display an incorrect date after the field has been saved.
Steps to reproduce
- Create a
text_date_timestampfield as normal, I am using it on a custom post type, but this doesn't seem to matter. - Using the default date format as shown in the CMB documentation enter the date 07/23/2022. It has occurred with other dates too but this is just one I've chosen for testing.
- Save the post. If your post type is using the Gutenberg editor you will need to refresh the page, due to the save being done without reloading.
- The date field should now show an incorrect date, I see 02/17/4400
Possible Solution
I believe I have tracked the issue down to a change (commit 37c8763 I believe) to the CMB2_Utils::make_valid_time_stamp() function. Passing a valid timestamp through strtotime() such as 1658534400 (which is the timestamp for 07/23/2022) does not always return false and so returns an unexpected timestamp such as 76687491533 which is 02/17/4400.
Replicated on a fresh WordPress install, using a fresh download of the git repo. Also replicated by taking an unrelated project using an older version of CMB2 (2.7.0) and adding the changes implemented to CMB2_Utils::make_valid_time_stamp(). Doing so introduced the problem described above, and was fixed by reverting the code.
I have rolled back to the code to from before the commit and that has fix the issue for my client's site. I believe the commit was required to help with dates from the API etc so that may not be the best overall solution.
CMB2 Field Registration Code:
Replicated in multiple projects. Using the registration code taken from the documentation for a text_date_timestamp field has reproduced this error during testing. For example:
//... Standard registration for text_date_timestamp
$cmb->add_field( array(
'name' => 'Test Date Picker (UNIX timestamp)',
'id' => 'wiki_test_textdate_timestamp',
'type' => 'text_date_timestamp',
// 'timezone_meta_key' => 'wiki_test_timezone',
// 'date_format' => 'l jS \of F Y',
) );The code provided for the text_date_timestamp in the example-functions.php file can also be used.
Your Environment
Replicated on multiple environments (Apache + Nginx), using PHP versions 7.4 and 8.0.
Additional Notes
The problem does not appear to be too disruptive as the timestamp is saved to the database correctly, it is just displayed in the admin field incorrectly. However if the page is saved while the incorrect date is shown in the field it appears to replace the date with the incorrect date causing disruption to the site.