Support

Account

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 );