📄 calc.php
字号:
/** * Formats the date in the given format, much like * strfmt(). This function is used to alleviate the * problem with 32-bit numbers for dates pre 1970 * or post 2038, as strfmt() has on most systems. * Most of the formatting options are compatible. * * formatting options: * * %a abbreviated weekday name (Sun, Mon, Tue) * %A full weekday name (Sunday, Monday, Tuesday) * %b abbreviated month name (Jan, Feb, Mar) * %B full month name (January, February, March) * %d day of month (range 00 to 31) * %e day of month, single digit (range 0 to 31) * %E number of days since unspecified epoch (integer) * (%E is useful for passing a date in a URL as * an integer value. Then simply use * daysToDate() to convert back to a date.) * %j day of year (range 001 to 366) * %m month as decimal number (range 1 to 12) * %n newline character (\n) * %t tab character (\t) * %w weekday as decimal (0 = Sunday) * %U week number of current year, first sunday as first week * %y year as decimal (range 00 to 99) * %Y year as decimal including century (range 0000 to 9999) * %% literal '%' * * @param string day in format DD * @param string month in format MM * @param string year in format CCYY * @param string format for returned date * * @access public * * @return string date in given format */ function dateFormat($day,$month,$year,$format) { if (!go_date_Calc::isValidDate($day,$month,$year)) { $year = go_date_Calc::dateNow('%Y'); $month = go_date_Calc::dateNow('%m'); $day = go_date_Calc::dateNow('%d'); } $output = ''; for($strpos = 0; $strpos < strlen($format); $strpos++) { $char = substr($format,$strpos,1); if ($char == '%') { $nextchar = substr($format,$strpos + 1,1); switch($nextchar) { case 'a': $output .= go_date_Calc::getWeekdayAbbrname($day,$month,$year); break; case 'A': $output .= go_date_Calc::getWeekdayFullname($day,$month,$year); break; case 'b': $output .= go_date_Calc::getMonthAbbrname($month); break; case 'B': $output .= go_date_Calc::getMonthFullname($month); break; case 'd': $output .= sprintf('%02d',$day); break; case 'e': $output .= $day; break; case 'E': $output .= go_date_Calc::dateToDays($day,$month,$year); break; case 'j': $output .= go_date_Calc::julianDate($day,$month,$year); break; case 'm': $output .= sprintf('%02d',$month); break; case 'n': $output .= "\n"; break; case "t": $output .= "\t"; break; case 'w': $output .= go_date_Calc::dayOfWeek($day,$month,$year); break; case 'U': $output .= go_date_Calc::weekOfYear($day,$month,$year); break; case 'y': $output .= substr($year,2,2); break; case 'Y': $output .= $year; break; case '%': $output .= '%'; break; default: $output .= $char.$nextchar; } $strpos++; } else { $output .= $char; } } return $output; } // end func dateFormat /** * Returns the current local year in format CCYY * * @access public * * @return string year in format CCYY */ function getYear() { return go_date_Calc::dateNow('%Y'); } // end func getYear /** * Returns the current local month in format MM * * @access public * * @return string month in format MM */ function getMonth() { return go_date_Calc::dateNow('%m'); } // end func getMonth /** * Returns the current local day in format DD * * @access public * * @return string day in format DD */ function getDay() { return go_date_Calc::dateNow('%d'); } // end func getDay /** * Returns the full month name for the given month * * @param string month in format MM * * @access public * * @return string full month name */ function getMonthFullname($month) { $month = (int)$month; if (empty($month)) { $month = (int) go_date_Calc::dateNow('%m'); } $month_names = go_date_Calc::getMonthNames(); return $month_names[$month]; // getMonthNames returns months with correct indexes //return $month_names[($month - 1)]; } // end func getMonthFullname /** * Returns the abbreviated month name for the given month * * @param string month in format MM * @param int optional length of abbreviation, default is 3 * * @access public * * @return string abbreviated month name * @see go_date_Calc::getMonthFullname */ function getMonthAbbrname($month,$length=3) { $month = (int)$month; if (empty($month)) { $month = go_date_Calc::dateNow('%m'); } return substr(go_date_Calc::getMonthFullname($month), 0, $length); } // end func getMonthAbbrname /** * Returns the full weekday name for the given date * * @param string day in format DD, default is current local day * @param string month in format MM, default is current local month * @param string year in format CCYY, default is current local year * * @access public * * @return string full month name */ function getWeekdayFullname($day='',$month='',$year='') { if (empty($year)) { $year = go_date_Calc::dateNow('%Y'); } if (empty($month)) { $month = go_date_Calc::dateNow('%m'); } if (empty($day)) { $day = go_date_Calc::dateNow('%d'); } $weekday_names = go_date_Calc::getWeekDays(); $weekday = go_date_Calc::dayOfWeek($day,$month,$year); return $weekday_names[$weekday]; } // end func getWeekdayFullname /** * Returns the abbreviated weekday name for the given date * * @param string day in format DD, default is current local day * @param string month in format MM, default is current local month * @param string year in format CCYY, default is current local year * @param int optional length of abbreviation, default is 3 * * @access public * * @return string full month name * @see go_date_Calc::getWeekdayFullname */ function getWeekdayAbbrname($day='',$month='',$year='',$length=3) { if (empty($year)) { $year = go_date_Calc::dateNow('%Y'); } if (empty($month)) { $month = go_date_Calc::dateNow('%m'); } if (empty($day)) { $day = go_date_Calc::dateNow('%d'); } return substr(go_date_Calc::getWeekdayFullname($day,$month,$year),0,$length); } // end func getWeekdayFullname /** * Returns the numeric month from the month name or an abreviation * * Both August and Aug would return 8. * Month name is case insensitive. * * @param string month name * @return integer month number */ function getMonthFromFullName($month) { $month = strtolower($month); $months = go_date_Calc::getMonthNames(); while(list($id, $name) = each($months)) { if (ereg($month, strtolower($name))) { return($id); } } return(0); } // end func getMonthFromFullName /** * Returns an array of month names * * Used to take advantage of the setlocale function to return * language specific month names. * XXX cache values to some global array to avoid preformace * hits when called more than once. * * @returns array An array of month names */ function getMonthNames() { for($i=1;$i<13;$i++) { $months[$i] = strftime('%B', mktime(0, 0, 0, $i, 1, 2001)); } return($months); } // end func getMonthNames /** * Returns an array of week days * * Used to take advantage of the setlocale function to * return language specific week days * XXX cache values to some global array to avoid preformace * hits when called more than once. * * @returns array An array of week day names */ function getWeekDays() { for($i=0;$i<7;$i++) { $weekdays[$i] = strftime('%A', mktime(0, 0, 0, 1, $i, 2001)); } return($weekdays); } // end func getWeekDays /** * Converts from Gregorian Year-Month-Day to * ISO YearNumber-WeekNumber-WeekDay * * Uses ISO 8601 definitions. * Algorithm from Rick McCarty, 1999 at * http://personal.ecu.edu/mccartyr/ISOwdALG.txt * * @param string day in format DD * @param string month in format MM * @param string year in format CCYY * @return string * @access public */ // Transcribed to PHP by Jesus M. Castagnetto (blame him if it is fubared ;-) function gregorianToISO($day,$month,$year) { $mnth = array (0,31,59,90,120,151,181,212,243,273,304,334); $y_isleap = go_date_Calc::isLeapYear($year); $y_1_isleap = go_date_Calc::isLeapYear($year - 1); $day_of_year_number = $day + $mnth[$month - 1]; if ($y_isleap && $month > 2) { $day_of_year_number++; } // find Jan 1 weekday (monday = 1, sunday = 7) $yy = ($year - 1) % 100; $c = ($year - 1) - $yy; $g = $yy + intval($yy/4); $jan1_weekday = 1 + intval((((($c / 100) % 4) * 5) + $g) % 7); // weekday for year-month-day $h = $day_of_year_number + ($jan1_weekday - 1); $weekday = 1 + intval(($h - 1) % 7); // find if Y M D falls in YearNumber Y-1, WeekNumber 52 or if ($day_of_year_number <= (8 - $jan1_weekday) && $jan1_weekday > 4){ $yearnumber = $year - 1; if ($jan1_weekday == 5 || ($jan1_weekday == 6 && $y_1_isleap)) { $weeknumber = 53; } else { $weeknumber = 52; } } else { $yearnumber = $year; } // find if Y M D falls in YearNumber Y+1, WeekNumber 1 if ($yearnumber == $year) { if ($y_isleap) { $i = 366; } else { $i = 365; } if (($i - $day_of_year_number) < (4 - $weekday)) { $yearnumber++; $weeknumber = 1; } } // find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 if ($yearnumber == $year) { $j = $day_of_year_number + (7 - $weekday) + ($jan1_weekday - 1); $weeknumber = intval($j / 7); if ($jan1_weekday > 4) { $weeknumber--; } } // put it all together if ($weeknumber < 10) $weeknumber = '0'.$weeknumber; return "{$yearnumber}-{$weeknumber}-{$weekday}"; } /** * Determines julian date of the given season * Adapted from previous work in Java by James Mark Hamilton, mhamilton@qwest.net * * @author Robert Butler <rob@maxwellcreek.org> * * @param string is VERNALEQUINOX, SUMMERSOLSTICE, AUTUMNALEQUINOX, or WINTERSOLSTICE. * @param string year in format CCYY, must be a calendar year between -1000BC and 3000AD. * * @access public * * @return float $juliandate */ function dateSeason ($season, $year = '') { if ($year == '') { $year = go_date_Calc::dateNow('%Y'); } if (($year >= -1000) && ($year <= 1000)) { $y = $year / 1000.0; if ($season == 'VERNALEQUINOX') { $juliandate = (((((((-0.00071 * $y) - 0.00111) * $y) + 0.06134) * $y) + 365242.1374) * $y) + 1721139.29189; } else if ($season == 'SUMMERSOLSTICE') { $juliandate = ((((((( 0.00025 * $y) + 0.00907) * $y) - 0.05323) * $y) + 365241.72562) * $y) + 1721233.25401; } else if ($season == 'AUTUMNALEQUINOX') { $juliandate = ((((((( 0.00074 * $y) - 0.00297) * $y) - 0.11677) * $y) + 365242.49558) * $y) + 1721325.70455; } else if ($season == 'WINTERSOLSTICE') { $juliandate = (((((((-0.00006 * $y) - 0.00933) * $y) - 0.00769) * $y) + 365242.88257) * $y) + 1721414.39987; } } elseif (($year > 1000) && ($year <= 3000)) { $y = ($year - 2000) / 1000; if ($season == 'VERNALEQUINOX') { $juliandate = (((((((-0.00057 * $y) - 0.00411) * $y) + 0.05169) * $y) + 365242.37404) * $y) + 2451623.80984; } else if ($season == 'SUMMERSOLSTICE') { $juliandate = (((((((-0.0003 * $y) + 0.00888) * $y) + 0.00325) * $y) + 365241.62603) * $y) + 2451716.56767; } else if ($season == 'AUTUMNALEQUINOX') { $juliandate = ((((((( 0.00078 * $y) + 0.00337) * $y) - 0.11575) * $y) + 365242.01767) * $y) + 2451810.21715; } else if ($season == 'WINTERSOLSTICE') { $juliandate = ((((((( 0.00032 * $y) - 0.00823) * $y) - 0.06223) * $y) + 365242.74049) * $y) + 2451900.05952; } } return ($juliandate); } // end func dateSeason} // end class go_date_Calc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -