Support

Account

Home Forums Backend Issues (wp-admin) Saving future date value using update_field

Solved

Saving future date value using update_field

  • Hey everyone,
    I’ve been racking my brain trying to update two fields to a post_type upon it being created. I have it saving values, they’re just not correct.

    I’m trying to save two different values to two different date picker fields using this code:

    add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ){
        
        $todaysdatefieldkey = 'field_579d04a5e494e';
    	$todaysdate = date( 'ymd' );
    	if ( function_exists( 'update_field' ) ){
    		update_field( $todaysdatefieldkey, $todaysdate, $order_id );
    		if ( isset( $_POST['acf'][$todaysdatefieldkey] ) )
    			unset($_POST['acf'][$todaysdatefieldkey] );
    	}
    	$nextweeksdatefieldkey = 'field_579d04b3e494f';
    	$date = new DateTime();
    	$date->modify("+7 day");
    	$nextweeksdate = $date->format("ymd");
    	if ( function_exists( 'update_field' ) ){
    		update_field( $nextweeksdatefieldkey, $nextweeksdate, $order_id );
    		if ( isset( $_POST['acf'][$nextweeksdatefieldkey] ) )
    			unset($_POST['acf'][$nextweeksdatefieldkey] );
    	}
        
    } , 10, 2);

    Both values come in the same, just displaying 02/01/1970

    Current + future dates seem to generate fine and I am saving some sort of value to the field via update_field, but not the right value. I’m tipping it’s something small that I’m doing wrong, but can’t work it out. Any ideas?

    Thanks!

  • Try formating the dates as ‘Ymd’ (with uppercase “Y”, which outputs 4 digits, as opposed to “y”, which outputs 2). This is the default format for date fields.

    Though, of course, if you have entered another format in your ACF setup for this field, then you should change accordingly. 😉

  • That did it. Thanks so much!

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

You must be logged in to reply to this topic.