Home › Forums › Feature Requests › Date Time Picker should optionally save timestamp › Reply To: Date Time Picker should optionally save timestamp
Okay, so it’s about taking the filter / actions approach. This way converting the field value when saving a post is not enough, you also have to convert the value back when the field is loaded by ACF otherwise the date picker UI doesn’t work:
This works for me:
/**
* Convert values of ACF core date time pickers from Y-m-d H:i:s to timestamp
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_save_as_timestamp( $value, $post_id, $field ) {
if( $value ) {
$value = strtotime( $value );
}
return $value;
}
add_filter( 'acf/update_value/type=date_time_picker', 'acf_save_as_timestamp', 10, 3 );
/**
* Convert values of ACF core date time pickers from timestamp to Y-m-d H:i:s
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_load_as_timestamp( $value, $post_id, $field ) {
if( $value ) {
$value = date( 'Y-m-d H:i:s', $value );
}
return $value;
}
add_filter( 'acf/load_value/type=date_time_picker', 'acf_load_as_timestamp', 10, 3 );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.