Zeller’s Congruence / Algorithm

Christian Zeller devised the Zeller’s congruence to calculate the day of the week for any date that uses the Gregorian calendar. It is also called by its other name – Zeller’s algorithm. It is one of the popular methods to calculate the day of the week for any particular date. The date in question can be in the past or future.

In Zeller’s algorithm, the year begins in March (with index of 3) making January (with index of 13) and February (with index of 14) to fall under the previous year. The software implementation of the formula for the Gregorian calendar is as follows:

w = (d + (m + 1)*26/10 + y + y/4 + 5) % 7

where:

w   is the day of the week with Saturday = 0, Sunday = 1 and so on
d   is the day of the month from 1 to 31
m   is the shifted month (March = 3, ... January = 13, February = 14)
y   is the 4-digit year which is (y - 1) if the month is January or February
%   is mod or modulo division (get the remainder only)

Note: For calindrical calculations, all divisions are done as integer division. That is, drop the decimal (or remainder) and take only the whole number.

Tøndering’s and Sakamoto’s Algorithm

Claus Tøndering’s algorithm is another method of computing the day of the week in the Gregorian calendar. It is a variant derived from the Gaussian algorithm for calculating the day of the week for January 1 of any given year.

In 1993, Tomohiko Sakamoto devised the following ANSI C function to implement Tøndering’s algorithm:


int dow(int y, int m, int d)
{
  static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
  y -= m < 3;
  return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

The above function by Sakamoto aims to minimize the code needed to convert a Gregorian date into a numerical day of the week. The function returns 0 for Sunday, 1 for Monday ... 6 for Saturday.

This website uses the above code adopted for PHP to compute the day of the week of any given date in the Gregorian calendar. Since there is no integer division in PHP, all divisions are casted as integer.