RSS

Daily Archives: January 18, 2014

Happy New Year!

I have seen couple of the times guys are struggling with question like below. If you are among the same please find this post as a help for you.

Q: On which of the two days of the week, Saturday or Sunday, does New Year’s Day fall more often? Explain your reasoning and write the corresponding PHP code to prove your answer.

I would share a reasoning for the above question. And I am sure you guys would be pretty good to convert it into a programme. If still you have any issue feel free to write me to help you even with code snippet also.

In the coming 400 years from 2001 to 2400, Saturday and Sunday both will occur equal 57 times on the New Year eve.

REASONING

ð  Every fourth year is a leap year. 2004, 2008, and 2012 are leap years.
ð  However, every hundredth year is not a leap year. 2100, 2200 and 2300 are not leap years.
ð  Every four hundred years, there’s a leap year after all. 2000 and 2400 are leap years.

For A Non Leap Year, i.e; 2001, 2002, 2003

No. of Days = 365

No. of Weeks = 365/7 = 52 Weeks + One Day

ð  The year starts with the day of the week will complete on the same day, and the next year will start with the next successive day of the week.

For example:

Ist Jan 2001 – Monday

So,

31st Jan 2001 – Monday

1st Jan 2002 – Tuesday…..and so far till leap year doesn’t come.

For A Leap Year, i.e; 2004, 2008, 2012

No. of Days = 366

No. of Weeks = 366/7 = 52 Weeks + Two Days

ð  The year starts with the day of the week will complete on the next day, and the next year will start with the next to next successive day of the week.

For example:

Ist Jan 2004 – Thursday

So,

31st Jan 2004 – Friday

1st Jan 2005 – Saturday…..and so far

As per the logic described above please find the below PHP program which will output the following:

Total No. of Sunday’s count on New Year: 58
Total No. of Saturday’s count on New Year: 56

I have written the code for the same by using the below two functions, you are free to use the same concept or something new

  1. checkLeap ~ To identify the particular year is a Leap year or not.
  2. newYearDay  ~ To find the first day of the year as per the Leap year and Non Leap year condition.

I hope the above explanation will work for you to generate a programme for the scenario!

 
Leave a comment

Posted by on January 18, 2014 in PHP

 

Tags: , , , , , , , ,