我正在尝试解决欧拉项目的第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/ >";
}
}
?>
我甚至还没有试着去数星期天,因为它一遍又一遍地重复Thu。
我在网上搜索,我发现这是因为我试图使用strtotime与一个变量。
那么问题是如何在strtotime或mktime中使用变量?
不知道这里发生了什么。我试过你的剧本,它对我来说是完美的。[PHP 5.4.4-14+deb7u8 (cli) (build: Feb 17 2014 09:18:47)]
运行你的代码与小调整我做了这样的输出:
1900, 1: Mon
1,1,2: Thu
1,1,3: Thu
1990,4: Sun
[more lines]