After getting year, you may check if category exists using
category_exists($cat_name);
if not found then create category using
wp_create_category( $cat_name, $parent );
after that you may create post using
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
You may also add category to a existing post using
wp_set_post_categories($post_id,$post_categories)
more information:
https://developer.wordpress.org/reference/functions/wp_insert_post/
https://codex.wordpress.org/Function_Reference/wp_create_category
https://developer.wordpress.org/reference/functions/category_exists/