We can in fact never guarantee such a continuous extension on some $U\cap X$, unless $X$ has interior or is not dense, in which case we trivially can as you have observed.
Proof.
Suppose $X$ is dense and has no interior, so that $\mathbb R\backslash X$ is dense.
For ease of exposition, we will suppose that $X\subseteq\mathbb R\backslash \mathbb D$, where $\mathbb D=\left\{\frac{k}{2^n}\mid k\in\mathbb Z,n\in \mathbb N\right\}$ are the dyadics, and explain how to modify the construction to the general case at the end.
Let $I_n^k=\left(\frac{k}{2^n},\frac{k+1}{2^n}\right)$, and for each $n$ define $f_n\colon X\to \mathbb R$ so that
$$f_n(x)=
\begin{cases}
3^{-n} & x\in I_n^k\cap X, k \text { odd}\\
0 & x\in I_n^k\cap X, k \text { even}.
\end{cases}
$$
Let $f(x)=\sum_{n=1}^\infty f_n(x)$, and note that $f$ is continuous, since on each $I_n^k$, $f$ varies by at most $\sum_{m=n+1}^{\infty} 3^{-m} = \frac{1}{2}\left(3^{-n}\right)$, and each $x\in X$ is contained in the interior of some $I_n^k$ for each $n$.
On the other hand, $f$ cannot be uniformly continuous in any open subset of $X$, since every such set includes $(I_n^{2k}\cup I_n^{2k+1})\cap X$ for some $n$ and $k$, and then $$\left|f(x)-f(y)\right|\geq \left|3^{-n}-\frac{1}{2}\left(3^{-n}\right)\right| = \frac{1}{2}\left(3^{-n}\right)$$ for each $x\in I_n^{2k}\cap X$ and $y\in I_n^{2k+1}\cap X$, despite that such $x$ and $y$ may be arbitrarily close to each other, by density of $X$.
Since $f$ is not uniformly continuous on any open subset of $X$, $f$ cannot be extended to a continuous function on any open set in $\mathbb R$, since every continuous function on an open set in $\mathbb R$ is locally uniformly continuous.
Finally, note that we can mimic this construction for any dense subset $X$ with no interior, by simply choosing intervals $I_n^k$ to be nested in the same way as above, with diameters converging uniformly to $0$ with each generation, and with endpoints not in $X$.