Every astronomical fact of interest about the moon and its appearance from Earth can be calculated to nearly arbitrary accuracy. The results depend on the observer's position on Earth as well as the date and time of day, to a fraction of a second.
See the offerings at Moonrise and Moonset Calculator and various astronomy buff websites for more information.
Or better, buy a copy of Jean Meeus' book "Astronomical Formula for Calculators". The moon phase calculation (more accurately, the illuminated fraction of the moon's disk) is presented and explained in Chapter 31 (4th Ed.).
Many of the Meeus algorithms were coded in C and are available in the source for the "moontool" program, which displays moon phases using a 64x64 bitmap. See the bitmap in action with the Windows version of "moontool".
/*
* $Id: moon.xbm,v 1.1.1.1 1993/08/17 09:42:33 alm Exp $
*/
/* converted from Sun Icon format - originally distributed with moontool */
#define moon_width 64
#define moon_height 64
static char moon_bits[] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x10,0xf8,
0xff,0xff,0xff,0xff,0x07,0x82,0xc4,0xe0,0xff,0xff,0xff,0xff,0x01,0x10,0x04,
0x80,0xff,0xff,0xff,0xff,0x10,0x47,0x50,0x0d,0xff,0xff,0xff,0x7f,0xc0,0x21,
0x02,0x15,0xfe,0xff,0xff,0x1f,0x20,0x36,0x44,0x31,0xf8,0xff,0xff,0x0f,0x69,
0x8b,0xd8,0x9a,0xf0,0xff,0xff,0x07,0xec,0x1a,0xa0,0xe3,0xe0,0xff,0xff,0x27,
0xa0,0x85,0x04,0x8f,0xe2,0xff,0xff,0x03,0x55,0x0b,0xf5,0x28,0xc5,0xff,0xff,
0x41,0xa2,0x20,0x02,0x67,0x81,0xff,0xff,0x90,0x89,0x84,0x20,0x2d,0x10,0xff,
0xff,0x04,0xb1,0x80,0x04,0x0a,0x04,0xff,0x7f,0x40,0x5a,0x52,0x10,0x40,0x01,
0xfe,0x7f,0x14,0x81,0x48,0x04,0x99,0x48,0xfe,0x3f,0xa0,0x62,0x52,0x52,0x06,
0x08,0xfc,0x3f,0x2a,0x02,0x89,0x00,0x22,0x42,0xfc,0x3f,0x16,0xa4,0x2d,0x42,
0x82,0x04,0xfc,0x1f,0x81,0x44,0x06,0xdd,0x0d,0x20,0xf8,0x1f,0x14,0x82,0x30,
0x20,0x10,0x04,0xf8,0x9f,0x00,0x9f,0x0a,0x33,0x65,0x91,0xf8,0x1f,0xc9,0x28,
0x00,0x0c,0x80,0x00,0xf8,0x1f,0x50,0x64,0x12,0x54,0x04,0x01,0xf9,0x1f,0xc9,
0xa0,0x00,0x08,0x10,0x03,0xf8,0x9f,0x84,0x19,0x04,0x28,0x81,0x00,0xf8,0x1f,
0x08,0xee,0x10,0x11,0x40,0x92,0xf8,0x1f,0xb5,0x50,0x01,0x62,0x38,0x00,0xf8,
0x1f,0x56,0x08,0x02,0x48,0x87,0x00,0xf8,0x1f,0xa9,0x09,0x41,0x00,0x00,0x80,
0xf8,0x1f,0x56,0x40,0x00,0x00,0x00,0x00,0xf8,0x9f,0x50,0x12,0x00,0x00,0x00,
0x40,0xf8,0x1f,0xaa,0x20,0x11,0x00,0x40,0x0e,0xf8,0x1f,0x0c,0x05,0x00,0x20,
0x90,0x11,0xf8,0x3f,0x90,0x02,0x00,0x00,0x40,0x10,0xfc,0x3f,0xc1,0x52,0x02,
0x04,0x20,0x94,0xfc,0x3f,0x20,0x8c,0x00,0x00,0x21,0x0a,0xfc,0x7f,0x92,0x08,
0xf2,0x21,0xa0,0x04,0xfe,0x7f,0x90,0x16,0x0c,0x02,0xc0,0x03,0xfe,0xff,0x48,
0xa1,0x06,0x08,0x00,0x00,0xff,0xff,0x10,0x5a,0x01,0x30,0x00,0x08,0xff,0xff,
0xb1,0xce,0x40,0x28,0x80,0x80,0xff,0xff,0xc3,0x93,0xe0,0x41,0x00,0xc0,0xff,
0xff,0x47,0xc1,0x70,0x2c,0x20,0xe0,0xff,0xff,0x07,0x8a,0x80,0x28,0x00,0xe2,
0xff,0xff,0x0f,0x0a,0x49,0x06,0x12,0xf0,0xff,0xff,0x1f,0x00,0x73,0x31,0x40,
0xf8,0xff,0xff,0x7f,0x90,0x9e,0x87,0x00,0xfe,0xff,0xff,0xff,0x10,0x70,0x48,
0x00,0xff,0xff,0xff,0xff,0x81,0x40,0x02,0x84,0xff,0xff,0xff,0xff,0x07,0x04,
0x90,0xe0,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0xf8,0xff,0xff,0xff,0xff,0xff,
0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff};