Home › Forums › Feature Requests › Date Time Picker should optionally save timestamp › Reply To: Date Time Picker should optionally save timestamp
In adddition to hatsumatsu: Writing the timestamps should probably consider the current timezone settings of your WP installation. I was struggling with that for several hours now, checking the timestamps back and forth. Here’s my approach:
/**
* Convert values of named ACF 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 ) {
$tz = new DateTimeZone(get_option('timezone_string'));
$date = DateTime::createFromFormat('Y-m-d H:i:s', $value, $tz);
$value = $date->getTimestamp();
}
return $value;
}
add_filter( 'acf/update_value/name=your_field_name', 'acf_save_as_timestamp', 10, 3 );
Or in case you want all date_time_pickers saving timestamps:
add_filter( 'acf/load_value/type=date_time_picker', 'acf_load_as_timestamp', 10, 3 );
Just in case someone needs that
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.