Archive page showing wrong month

Try adding the following above the first if statement to see what actual day is being useddisplayed:

echo get_the_date();

It might also help to add the following lines to help debug exactly what the query is doing:

global $wp_query;
var_dump($wp_query);

The code is correct – so it has to be something else affecting the display. Does it do this for every month or just specific ones?