Ah, you’re using ACF! Yes, you can define the outputted format directly in the ACF edit screen for that field under General > Return Format (see documentation). That also makes sense because it says that the value is saved as Ymd
(e.g. “YYYYMMDD”) in the database which is why grabbing the raw value using our built-in shortcode returns it in that format.
Also, this made me notice the typo in my above code where I wrote
$date = date_create_from_format('YYYYMMDD', $date, new DateTimeZone(wp_timezone_string()));
that needed to be
$date = date_create_from_format('Ymd', $date, new DateTimeZone(wp_timezone_string()));
Your solution is a great one. If you have the format you want configured in the ACF setting, you can use the ACF shortcode in our form tags ??
Since you’re using a custom format, the dynamic_text
or dynamic_hidden
form tags are more suitable for your case since HTML date inputs requires RFC 3339 specification, so only Y-m-d
(e.g. “YYYY-MM-DD”) is valid for that form tag.
Glad it’s sorted. Cheers!