How do you write a differential operator as a matrix?

It will depend somewhat on the functions you want the operator to work on, but the general scheme is the same.

First, you need to pick some basis for the functions your differential operator is going to work on. If we look to the vector space of quadratics over $\mathbb{R}$, we could have the basis $\{1,x,x^2\}$

Now a linear map is completely determined by what it does to the basis elements. If we call the differential operator $D$, then of course $D(1)=0,\quad D(x)=1, \quad D(x^2) = 2x$.

So we can encode this into a matrix:

$$D= \left( \begin{array}{ccc} 0 & 1 & 0 \\ 0 & 0 & 2 \\ 0 & 0 & 0 \end{array} \right)$$

The columns of this matrix are the images of the basis vectors after applying $D$.

Second-order derivatives are just the derivative of the derivative, i.e., applying the operator $D$ twice. So the matrix $D^2$ will represent the linear map of a function to its second derivative.

In this answer: https://math.stackexchange.com/a/943212/87896 there is another example, and applying it to solve an integral.