This is really nice. My initial thought was "why just divide the time interval since the Unix Epoch by the synodic period of the moon". Turns out the moon's phase is a bit more complicated than that.
https://en.wikipedia.org/wiki/Lunar_phase#Calculating_phase