function digits(num, len)
{
	var str = num.toString();
	while (str.length < len)
		str = "0" + str;
	return str;
}

// emulates the php "date" functionality
// incomplete - the following are missing, mostly timezone-related:
//   'e': Timezone name, eg "Australia/Sydney"
//   'I': Whether or not the timezone is in daylight savings
//   'N', 'o': Undocumented, I dunno what they are
//   'T': Timezone name abbreviation, eg "EST"
//   'W': ISO 8601 week number (a pain to calculate)
var monthnames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var shortmonthnames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var monthlengths = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var monthlengthtotals = new Array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
var daynames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
var shortdaynames = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
function phpdate(format, date)
{
	if (!date)
		date = new Date();
	var str = '';
	for (var i = 0; i < format.length; i++)
	{
		var c = format.charAt(i);
		switch(c)
		{
		case 'a':
			str += date.getHours() < 12 ? 'am' : 'pm';
			break;
		case 'A':
			str += date.getHours() < 12 ? 'AM' : 'PM';
			break;
		case 'B':
			str += digits(Math.round((date.getTime() + 3600000) / 86400) % 1000, 3);
			break;
		case 'c':
			str += digits(date.getFullYear(), 4) + "-" + digits(date.getMonth() + 1, 2) + "-" + digits(date.getDate(), 2) + "T" + digits(date.getHours(), 2) + ":" + digits(date.getMinutes(), 2) + ":" + digits(date.getSeconds(), 2);
			var tz = -date.getTimezoneOffset();
			if (tz < 0)
			{
				tz = -tz;
				str += "-";
			}
			else
				str += "+";
			str += digits(Math.floor(tz / 60), 2) + ":" + digits(tz % 60, 2);
			break;
		case 'd':
			str += digits(date.getDate(), 2);
			break;
		case 'D':
			str += shortdaynames[date.getDay()];
			break;
		case 'F':
			str += monthnames[date.getMonth()];
			break;
		case 'g':
			var h = date.getHours();
			if (h == 0 || h == 12)
				str += "12";
			else
				str += h % 12;
			break;
		case 'G':
			str += date.getHours();
			break;
		case 'h':
			var h = date.getHours();
			if (h == 0 || h == 12)
				str += "12";
			else
				str += digits(h % 12, 2);
			break;
		case 'H':
			str += digits(date.getHours(), 2);
			break;
		case 'i':
			str += digits(date.getMinutes(), 2);
			break;
		case 'j':
			str += date.getDate();
			break;
		case 'l':
			str += daynames[date.getDay()];
			break;
		case 'L':
			var y = date.getFullYear();
			if (y % 400 == 0)
				str += '1';
			else if (y % 100 == 0)
				str += '0';
			else if (y % 4 == 0)
				str += '1';
			else
				str += '0';
			break;
		case 'm':
			str += digits(date.getMonth() + 1, 2);
			break;
		case 'M':
			str += shortmonthnames[date.getMonth()];
			break;
		case 'n':
			str += date.getMonth() + 1;
			break;
		case 'O':
			var tz = -date.getTimezoneOffset();
			if (tz < 0)
			{
				tz = -tz;
				str += "-";
			}
			else
				str += "+";
			str += digits(Math.floor(tz / 60), 2) + digits(tz % 60, 2);
			break;
		case 'r':
			format = format.substring(0, i - 1) + "D, d M Y H:i:s O" + format.substring(i + 1);
			i--;
			break;
		case 's':
			str += digits(date.getSeconds(), 2);
			break;
		case 'S':
			var d = date.getDate();
			if (d % 100 >= 10 && d % 100 < 20)
				str += 'th';
			else if (d % 10 == 1)
				str += 'st';
			else if (d % 10 == 2)
				str += 'nd';
			else if (d % 10 == 3)
				str += 'rd';
			else
				str += 'th';
			break;
		case 't':
			if (date.getMonth() == 1)
			{
				var y = date.getFullYear();
				if (y % 400 == 0)
					str += '29';
				else if (y % 100 == 0)
					str += '28';
				else if (y % 4 == 0)
					str += '29';
				else
					str += '28';
			}
			else
				str += monthlengths[date.getMonth()];
			break;
		case 'U':
			str += Math.round(date.getTime() / 1000);
			break;
		case 'w':
			str += date.getDay();
			break;
		case 'Y':
			str += digits(date.getFullYear(), 4);
			break;
		case 'y':
			str += digits(date.getFullYear() % 100, 2);
			break;
		case 'z':
			var d = date.getDate() + monthlengthtotals[date.getMonth()] - 1;
			if (date.getMonth() >= 2)
			{
				if (y % 400 == 0)
					d++;
				else if (y % 100 == 0)
					;
				else if (y % 4 == 0)
					d++;
			}
			str += d;
			break;
		case 'Z':
			str += -date.getTimezoneOffset() * 60;
			break;
		case '\\':
			str += format.charAt(++i);
			break;
		default:
			str += c;
		}
	}
	return str;
}
