继续"周四"当试图在strtotime PHP中传递变量时

时间:2014-03-23 13:14:51

标签: php loops strtotime

我正在努力解决Project Euler的第19个问题,但我遇到了一些麻烦。

这就是问题:

您将获得以下信息,但您可能更愿意为自己做一些研究。

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

在二十世纪的第一个月(1901年1月1日至2000年12月31日),有多少个星期日下降?

这是我的代码:

<?php
// In the beginning there are no Sundays counted yet
$number_of_sundays = 0; 

// Make the following actions for all years of the twentieth century
for ($year = 1900; $year <= 1999; $year++) { 
      // Make the following actions for all months from January to December
      for ($month = 1; $month <= 12; $month++) {
            // Get Unix Timestamp of the current month and year on the first day of the month
            $current_date = mktime(0, 0, 0, $month, 01, $year); 

            $get_day_of_week = date("D", $current_date);

            echo $year . ", " . $month . ": " . $get_day_of_week."<br/ >";
      }
}
?>

我甚至还没有试图计算周日,因为它一次又一次地与周四相呼应。

我在网上搜索,我发现它是因为我试图使用strtotime和变量。

所以问题是如何在strtotime或mktime中使用变量?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

不确定这里发生了什么。我尝试过你的脚本,它对我来说很完美。 [PHP 5.4.4-14 + deb7u8(cli)(建于2014年2月17日09:18:47)]

使用我做过的小调整来运行代码输出:

  

1900,1:Mon

     

1900,2:周四

     

1900年,3:星期四

     

1900,4:Sun

     

[更多行]