Support

Account

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