Your assertion is false as there are various elementary counterexamples already pointed out in the other answers.
The general result (due to von Neumann) is the following one. A pair of selfadjoint operators $A$ and $B$ strongly commute, which means that their spectral measures commute, if and only if there is a third selfadjoint operator $C$ such that $A=f(C)$ and $B=g(C)$ for a pair of (Borel-measurable) functions $f,g : \mathbb{R} \to \mathbb{R}$.
In general $f$ and $g$ are not invertible, there are trivial examples. $A=X^2$ and $B=X^3- kX$ where $C=X$ is the standard position operator in $L^2(R, dx)$ and $k$ a dimensional coefficient. Here you cannot write $A$ as function of $B$ or vice versa. Also in finite dimension, consider $\mathbb{C}^3$ and the angular momentum operator $C=L_z$ and next take $A=L_z^2$ and $B= L_z^3-\hbar^2 L_z$, there is no way to write one as a function of the other.
For a proof in the general case, see the book by Riesz and Nagy on functional analysis or the book by Varadarajan on the Geometric structure of Quantum Theory.
In finite dimension strongly commutativity is equivalent to standard commutativity of the operators.