Measuring "phase" difference between two square waves

You are communication at 9600 baud, that means you can send 960 characters per second, or one character every 1mS. As your input frequency is 40KHz the one cycle is 25uS. So in effect you are not looking at your input signal fast enough to measure the time.

So write the code to measure the time first and then display it.

It dosn't help that you are not measuring the time correctly anyway. You want to set t1 when pin 12 becomes high not when it is high, the same goes for pin 13. This is known as a state change and their is an example of how to do this in the Arduino's IDE.