0

I came to a see a interesting post that i am trying to write a code for that

"Hello,On which of the two days of the week, Saturday or Sunday, does New Year’s Day fall often"

Reference :http://answers.yahoo.com/question/index?qid=20080731110435AAlyxd4

i have found solution

Calendar repeats itself every 400 years, but because 2100 etc are not leap years, the distribution is not even.It turns out that Sunday and Tuesday and Friday have the most occurrences of Jan 1 - 58 every 400 years.

   Monday - 56
   Tuesday - 58
   Wednesday - 57
   Thursday - 57
   Friday - 58
   Saturday – 56
   Sunday - 58

But I am not getting, how I can replicate this in PHP?

For this who think that i have not try ans try to downloadvote my question. I have already test the code.

function DayOfNewYearsDay($beginYear,$endYear)
{
for($i=$beginYear;$i<$endYear;$i++)
{
$day = date("l", mktime(1, 1, 1, 1, 1, $i));
if(isset ($days[$day]))
$days[$day]++;
else
$days[$day]=1;
}
return $days;
}

echo  print_r(DayOfNewYearsDay(2000, 2400));

But i am not getting 58 Sunday as answer

Any Idea ?

jyoti
  • 350
  • 1
  • 5
  • 15
  • Well, a good start would be to link us to the post so we know what's going on... – imulsion Jun 15 '13 at 08:54
  • its a yahoo Question.http://answers.yahoo.com/question/index?qid=20080731110435AAlyxd4 – jyoti Jun 15 '13 at 08:56
  • Someone try here code also. but it did;t work http://www.abdohoo.com/discussion/15/what-the-two-days-of-the-week-saturday-or-sunday-does-new-year-day-fall-more-often-prove-via-php/p1#_ – jyoti Jun 15 '13 at 08:56
  • My main motive here is to replicate this in PHP. :( – jyoti Jun 15 '13 at 08:57
  • What did you to get it? – Shiplu Mokaddim Jun 15 '13 at 08:58
  • Please use formating standarts, such as padding of code inside {} bracket, or your code is veryhardtoread. – kajacx Jun 15 '13 at 09:18
  • Your problem is here: http://stackoverflow.com/questions/5319710/accessing-dates-in-php-beyond-2038 – kajacx Jun 15 '13 at 09:40
  • I get that php 5.2 has new class "DateTime" and how we will use than that for here code. i haven't use this class yet now :( – jyoti Jun 15 '13 at 09:45
  • What answer did you get? I tried your code, it returns 58 for Sunday. – Arjan Jun 15 '13 at 09:48
  • Even i try echo print_r(DayOfNewYearsDay(1901 , 2038));, it is print only Array ( [Thursday] => 21 [Wednesday] => 20 [Friday] => 19 [Sunday] => 19 [Monday] => 20 [Tuesday] => 19 [Saturday] => 19 ) :( – jyoti Jun 15 '13 at 10:02
  • Array ( [Thursday] => 316 [Wednesday] => 15 [Friday] => 15 [Sunday] => 14 [Monday] => 14 [Tuesday] => 13 [Saturday] => 13 ) .It return this when i have use echo print_r(DayOfNewYearsDay(1600, 2000) – jyoti Jun 15 '13 at 10:03
  • BTW, if you are checking new years days from 2000-01-01 up to and including 2400-10-01, you actually check 401 years. – Arjan Jun 15 '13 at 22:32

2 Answers2

1

I dont want to spoon feed you. So giving a simple algorithm

  1. Create an array A
  2. For number range 1600 to 2000
    1. Create a datetime object of date "Number-01-01".
    2. Get the week day of datetime
    3. increment A[weekday] by 1
  3. Just print the array.
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • ,I have already write the alogo.But i am expecting answer as Sunday 58. Since which is corrct answer. Thts why i have posted question here – jyoti Jun 15 '13 at 09:16
  • That result will come if you run the program in year range `1600`-`2000`. And its stated in my answer already. – Shiplu Mokaddim Jun 15 '13 at 09:22
  • Array ( [Thursday] => 316 [Wednesday] => 15 [Friday] => 15 [Sunday] => 14 [Monday] => 14 [Tuesday] => 13 [Saturday] => 13 ) .It return this when i have use echo print_r(DayOfNewYearsDay(1600, 2000) – jyoti Jun 15 '13 at 09:30
  • calendar is bugged and works only from 1901 to 2038, see https://bugs.php.net/bug.php?id=40346 – kajacx Jun 15 '13 at 09:33
  • Even i try echo print_r(DayOfNewYearsDay(1901 , 2038));, it is print only Array ( [Thursday] => 21 [Wednesday] => 20 [Friday] => 19 [Sunday] => 19 [Monday] => 20 [Tuesday] => 19 [Saturday] => 19 ) :( – jyoti Jun 15 '13 at 09:38
  • @kajacx That is only true for 32-bit versions of PHP. You can use any 4-digit year if you are using a 64-bit version (I'm not sure if it also requires a 64-bit OS). – Arjan Jun 15 '13 at 09:59
  • @jyoti You should use DateTime. I used DateTime and [it works](https://eval.in/33628). – Shiplu Mokaddim Jun 15 '13 at 10:56
1

Use the DateTime object to get around the php limitation in mktime:

function DayOfNewYearsDay ($beginYear, $endYear)
{
    for ($i = $beginYear; $i < $endYear; $i++) {
        $date = new DateTime;
        $date->setDate($i, 1, 1);
        $day = $date->format('l');
        if (isset ($days[$day]))
            $days[$day]++;
        else
            $days[$day] = 1;
    }

    return $days;
}

http://codepad.org/iGou6dk4

Mike B
  • 31,886
  • 13
  • 87
  • 111