Support

Account

Home Forums General Issues Set month and year from a date field

Helping

Set month and year from a date field

  • Hello,

    Iam looking to set a field (event_month) to the month and year based on another ACF called (event_date) do I do this on save?

    Is something along the lines of this correct? https://gist.github.com/rveitch/9018669face1686e74aaa68026856f36

    Cheers

    Jon

  • So ive managed to get it working using the following. Any suggestions?

    function set_event_month($post_id, $post, $update) {
        
        $event_date = get_field('event_date_start', $post_id, FALSE ,FALSE); // Event Start Date
        $event_date = new DateTime($event_date); 
        $event_date = $event_date->format('F Y'); // Format date to month and year
        update_field('event_month_year', $event_date); // Update field
        }
    add_action('save_post', 'set_event_month', 10, 3);
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Set month and year from a date field’ is closed to new replies.