Esempio n. 1
0
// 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.
//
// How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
function is_leap($year)
{
    return $year % 4 == 0 and ($year % 100 != 0 or $year % 400 == 0);
}
$month_names = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
$day_names = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');
$sundays_on_the_first = 0;
$day = 0;
for ($year = 1900; $year < 2001; $year++) {
    for ($month = 0; $month < 12; $month++) {
        // if ($year == 1901) {
        // http://www.timeanddate.com/calendar/?year=1901&country=9
        // echo "The 1st of " . $month_names[$month] . "/" . $year . " is a " . $day_names[$day] . "\n";
        // }
        $day += $days_in_month[$month];
        if ($month == 1 and is_leap($year)) {
            $day++;
        }
        $day %= 7;
        if ($day == 6 and $year > 1900) {
            $sundays_on_the_first++;
        }
    }
}
echo "{$sundays_on_the_first}\n";
Esempio n. 2
0
     $_POST['ext_tday'] = '29';
 }
 if ($leap == false and $_POST['ext_tmonth'] == '02' and ($_POST['ext_tday'] == '29' or $_POST['ext_tday'] == '30' or $_POST['ext_tday'] == '31')) {
     $_POST['ext_tday'] = '28';
 }
 if (($_POST['ext_tmonth'] == '04' or $_POST['ext_tmonth'] == '06' or $_POST['ext_tmonth'] == '09' or $_POST['ext_tmonth'] == '11') and $_POST['ext_tday'] == '31') {
     $_POST['ext_tday'] = '30';
 }
 if ($_POST['ext_tyear'] == '' or $_POST['ext_tmonth'] == '' or $_POST['ext_tday'] == '') {
     $properties->set_external_review_deadline(NULL);
 } else {
     $tmp_date = new DateTime($_POST['ext_tyear'] . '-' . $_POST['ext_tmonth'] . '-' . $_POST['ext_tday']);
     $properties->set_external_review_deadline($tmp_date->format('Y-m-d'));
     unset($tmp_date);
 }
 $leap = is_leap($_POST['int_tyear']);
 if ($leap == true and $_POST['int_tmonth'] == '02' and ($_POST['int_tday'] == '30' or $_POST['int_tday'] == '31')) {
     $_POST['int_tday'] = '29';
 }
 if ($leap == false and $_POST['int_tmonth'] == '02' and ($_POST['int_tday'] == '29' or $_POST['int_tday'] == '30' or $_POST['int_tday'] == '31')) {
     $_POST['int_tday'] = '28';
 }
 if (($_POST['int_tmonth'] == '04' or $_POST['int_tmonth'] == '06' or $_POST['int_tmonth'] == '09' or $_POST['int_tmonth'] == '11') and $_POST['int_tday'] == '31') {
     $_POST['int_tday'] = '30';
 }
 if ($_POST['int_tyear'] == '' or $_POST['int_tmonth'] == '' or $_POST['int_tday'] == '') {
     $properties->set_internal_review_deadline(NULL);
 } else {
     $tmp_date = new DateTime($_POST['int_tyear'] . '-' . $_POST['int_tmonth'] . '-' . $_POST['int_tday']);
     $properties->set_internal_review_deadline($tmp_date->format('Y-m-d'));
 }