Sporadic absences count as days in Spain unless you can prove tax residency in another country, and the status applies to the whole year: Spain has no split-year residency. If two countries consider you resident at the same time, the tie is broken by the applicable double taxation treaty. The three tests in the law, with their articles, are set out in the guide are you a Spanish tax resident?.
Rather have this calculated for you?
kontora generates your tax forms box by box, tells you how much to set aside and reminds you before every deadline.