Support

Account

Home Forums General Issues same Date field saves in different formats

Solved

same Date field saves in different formats

  • Hello,

    i recently have problems with a query when trying to compare dates saved with ACF. The dates for some of my posts get saved diffrently than other posts – using the same date fields. The output with get_field() displays just right – in get_post_custom() its shows up differently.

    See Post 34687 for example:

    The fields get populated with update_post_meta($id, ‘verleih_start’, date(“Y-m-d”, strtotime($verleih_start))); but even when changing the date in backend the format stays the same.

    The meta_query i use (work with posts which have the timestamps with hyphens):

           'meta_query' => array(
    					
    
    			array(
    				'relation' => 'AND',
    				array(
    					'key' => 'wpcf-transport_status',
    					'value' => '0',
    					'compare' => '!=',
    				)
    				,
    				array(
    					'key' => 'verleih_start',
    					'value' => date('Y-m-d', strtotime('+90 days')),
    					'compare' => '<=',
    					'type'    => 'DATE'    
    				),		
    				array(
    					 'key'     => 'verleih_start',
    					 'value'   => date('Y-m-d',strtotime("today")),
    					 'compare' => '>=',
    					 'type'    => 'DATE'                     
    				)
    				
    			),
    			
            ),

    Thanks.

  • ACF has altered the way is stores and returns values. This was done when the new date/time and time fields were added in order to make dates compatible across these field types.

    YYYYMMDD is the way that ACF used to store dates. YYYY-MM-DD is the way that is stores dates now. When loading the value for a field ACF checks to see if the format is the second one and if not it adds the dashes for compatibility, but it does not update the existing value unless you edit the old post where the date is stored and update it.

    What you’ll need to do is check the formatting and add the dashes yourself if you are getting values without using ACF.

  • Well, it has been the other way around: New posts have dates without hyphens, old ones have date with hyphens and work.

    meta_query seems to work now either way -> resolved

  • There are also some differences with date fields between ACF4 and 5. ACF4 let you set the storage format and ACF5 does not.

    I could have something reversed, I just remember that there was a change made when the new fields were added.

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

The topic ‘same Date field saves in different formats’ is closed to new replies.