Week day number falling on a month day

Week day number falling on a month day

Created:14 Feb 2020 01:51:58 , in  Maths

Suppose, that you were born on Monday, January 1, and want to know how many Mondays happened to fall on the same day of any month of the year over, lets say, the last 100 years. It is a silly problem, but also one which points to some problems and inaccuracies ensuing from switching between Gregorian calendar dates to Julian day counts and back.

Mixing time and PHP

Anyway, here is a solution to the problem. As already hinted, it is given in PHP. Devising, it felt a bit like stretching the time axis (perhaps, I spend too much time watching vids on Linear Algebra these days again), first to take a closer look at years in the range, then to give a glimpse to months.

The solution to the problem relies heavily on two functions, that is, gregoriantojd and jddayofweek. The former converts Gregorian calendar dates to Julian Day Count, the latter returns the day of the week based on a count. Getting off the track for a second again, I'm still looking for a better, I mean shorter, name for the piece of code below.

 * Class SWWWWeekDaysOnMonthDayOverTime - Number of week days falling on a month day over time
 * Author: S.Wojnowski
 * Licence: GNU v3

 class SWWWWeekDaysOnMonthDayOverTime{

  private $monthDay = NULL;
  private $startYear = NULL;
  private $endYear = NULL;
  private $day = NULL;
  private $matches = [];

  public function __construct($conf){
    $this -> validateInput($conf);
  public function get(){
    for($year = $this -> startYear; $year < $this -> endYear + 1; $year++ ){
      for($month = 1; $month < 13;$month++){
       // get Julian day count
       $julianDay = gregoriantojd($month,$this -> monthDay,$year);
       // get the day of the week
       if(jddayofweek($julianDay,0) === $this -> day){
         $this -> matches[] = [
           'day' => $this -> monthDay,
           'month' => $month,
           'year' => $year
    return $this -> matches;

  // some validation 
  private function validateInput($conf){
    if(!isset($conf['monthDay']) or !isset($conf['startYear']) or !isset($conf['endYear']) or !isset($conf['day'])){
      throw new Exception('Day of month, start and end of year and day must be given');

    if($conf['monthDay'] < 0 or $conf['monthDay'] > 31 ){
      throw new RangeException('Day of the month must be between 0 and 31.');
    } else {
      $this -> monthDay = $conf['monthDay'];   

    if($conf['startYear'] < 1500 or $conf['startYear'] > 2030 ){
      throw new RangeException('Start year must be between 1500 and 2300.');
    } else {
      $this -> startYear = $conf['startYear'];   
    if($conf['endYear'] < 1500 or $conf['endYear'] > 2200 ){
      throw new RangeException('End year must be between 1500 and 2300.');
    } else {
      $this -> endYear = $conf['endYear'];   

    if($conf['startYear'] > $conf['endYear']  ){
      throw new Exception('Start year must be less or equal to the end year.');

    if($conf['day'] < 0 or $conf['day'] > 6 ){
      throw new RangeException('Day must be between 1500 and 2300.');
    } else {
      $this -> day = $conf['day'];   

Count of Sundays on 1st

Here is how to find number of Sundays, which happened to fall on the first day of any month over 100 years of the XX century.

    $conf = [
     'monthDay' => 1, // first day of each month 
     'startYear' => 1901, // start year
     'endYear' => 2000, // end year considered
     'day' => 0 // Sunday - day of the week to search for

    $sundayCount = new SWWWWeekDaysOnMonthDayOverTime
    $sundayCount -> get();
    => 171

It looks like about a seventh of all months started with a Sunday in the XX century. I should have envisaged that, rather than spending time on converting between what a caesar and a pope devised hand in hand with some Greek Maths wizards centuries ago.


The problem given in this article is easier to solve today than it was in the days when it was invented. The latter happened to be a good few years ago. Unlike some other earlier languages, PHP comes with a good set of functions for managing dates, calendar changes etc., which, surprise, surprise, can be unreliable at times. See PHP manual comments sections for more details on this.

This post was updated on 14 Feb 2020 02:01:38

Tags:  php 

Author, Copyright and citation


Sylwester Wojnowski

Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.


©Copyright, 2021 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.


Cite this article as:

Wojnowski, Sylwester. "Week day number falling on a month day." From sWWW - Code For The Web . https://wojnowski.net.pl//main/index/week-day-number-falling-on-a-month-day

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]

I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!