This is an implementation of the algorithm from Meeus' Astronomical Algorithms for computing the dates of the phases of the Moon. View the source code of this page
for the code, the code is public domain. The main algorithm is based on "lunation cycles", and since no one knows what those are, a helper function getCycleEstimate(year,month)
is available. The value returned from that truly is an estimate, so the user should compute phases for cycles both before and after the cycle returned. In one of the examples
in the book, Meeus uses the middle of the month as an estimate, and it might appear this is a sufficient method of getting all of the phases in a month, but it is not. Months
are longer than lunation cycles, and if you always use the middle of the month as a starting estimate, you will eventually skip a lunation cycle. This page serves as an example
of how to compute phases for an entire year, but the main algorithm is the getPhaseDate() function.