Support

Account

Home Forums Feature Requests Date Time Picker should optionally save timestamp

Solving

Date Time Picker should optionally save timestamp

  • The Date Time Picker field added in 5.3.9 is a welcome addition. Previously I had been using acf-field-date-time-picker, which this field sherlocks.

    One feature of acf-field-date-time-picker which I have a lot of existing functionality built around is the ability to save the value as a timestamp. I would continue with the acf-field-date-time-picker plugin, but when installed alongside 5.3.9 you end up with BOTH the stock ACF Date Time Picker field and the contrib plugin’s field. I can work something out with strtotime(), but if this field could optionally save the value as a timestamp all of my existing code could transition seamlessly.

    My existing codebase aside I generally prefer to store timestamps, and I’m fairly sure many others would as well.

  • This reply has been marked as private.
  • +1 for an option to save as timestamp, seems way more flexible to me.

    (Can’t view James’ reply since it’s marked private)

    I have a site with complex timestamp-based queries using the Date and Time Picker Addon, but after the latest ACF update this is broken in combination with the newly introduced date and time picker in ACF core. So I’m basically left with either coming up with a hook to convert the data into timestamp everytim a date is saved (seems rather hacky to me) or rewriting all of my queries? o_o

    An option to save data as timestamp would be greatly appreciated!

  • Hi @hatsumatsu

    I believe the plugin author has created a compatibility with the old third party plugin on version 3.9.2, so you can always force the saved value as timestamp by using the update_field() or update_post_meta() functions. You can do it when saving the post/objects by using the acf/save_post hook.

    I hope this helps 🙂

  • 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 );
    
  • Thank you, @hatsumatsu, great solution. This works for me also perfectly!

  • It’s been a while! But a tip: if the dates have been saved by a date_picker field and not a date_time_picker field in Ymd format (for example 20200916), you’ll need to convert the values in the database to a machine-legible date string (Y-m-d) before integrating this solution. Otherwise, the dates will all be displayed as 1970 dates, and potentially saved with incorrect values when re-edited.

  • 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

  • @hatsumatsu ‘s solution works great, except I can’t seem to be able to make it work with a date field assigned to a custom taxonomy. I’m using it for a date picker like this:

    /**
     * Convert values of ACF core date pickers from Ymd 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_wp_date( $value, $post_id, $field  ) {
        if( $value ) {
            $value = strtotime( $value );
        }
    
        return $value;    
    }
    
    add_filter( 'acf/update_value/type=date_picker', 'acf_save_as_wp_date', 10, 3 );
    
    /**
     * Convert values of ACF core date pickers from timestamp to Ymd
     * @param  string $value   unmodified value
     * @param  int    $post_id post ID
     * @param  object $field   field object
     * @return string          modified value
     */
    function acf_load_as_acf_date( $value, $post_id, $field  ) {
        if( $value ) {
            $value = date( 'Ymd', $value );
        }
    
        return $value;    
    }
    
    add_filter( 'acf/load_value/type=date_picker', 'acf_load_as_acf_date', 10, 3 );

    Tried adding the name of taxonomy, like $value = strtotime( $value, 'event_dates' ); and $value = date( 'Ymd', $value, 'event_dates' ); as well, but I don’t know if that’s at all correct way of doing it.

  • Let’s all just agree that having the option to just save the value as a timestamp would be very handy.

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Date Time Picker should optionally save timestamp’ is closed to new replies.