📄 calc.php
字号:
$weeks = 0; } $first_week_days %= 7; return (ceil((go_date_Calc::daysInMonth($month, $year) - $first_week_days) / 7) + $weeks); } // end func weeksInMonth /** * Find the day of the week for the first of the month of given date. * * @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 int number of weekday for the first day, 0=Sunday */ function firstOfMonthWeekday($month='',$year='') { if (empty($year)) { $year = go_date_Calc::dateNow('%Y'); } if (empty($month)) { $month = go_date_Calc::dateNow('%m'); } return(go_date_Calc::dayOfWeek('01',$month,$year)); } // end func firstOfMonthWeekday /** * Return date of first day of month of given date. * * @param string month in format MM, default is current local month * @param string year in format CCYY, default is current local year * @param string format for returned date * * @access public * * @return string date in given format */ function beginOfMonth($month='',$year='',$format='%Y%m%d') { if (empty($year)) { $year = go_date_Calc::dateNow('%Y'); } if (empty($month)) { $month = go_date_Calc::dateNow('%m'); } return(go_date_Calc::dateFormat('01',$month,$year,$format)); } // end of func beginOfMonth /** * Find the month day of the beginning of week for given date, * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.) * * @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 string format for returned date * * @access public * * @return string date in given format */ function beginOfWeek($day='',$month='',$year='',$format='%Y%m%d') { 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'); } $this_weekday = go_date_Calc::dayOfWeek($day,$month,$year); $interval = (7 - DATE_CALC_BEGIN_WEEKDAY + $this_weekday) % 7; return(go_date_Calc::daysToDate(go_date_Calc::dateToDays($day,$month,$year) - $interval,$format)); } // end of func beginOfWeek /** * Find the month day of the end of week for given date, * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday * of following month.) * * @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 string format for returned date * * @access public * * @return string date in given format */ function endOfWeek($day='',$month='',$year='',$format='%Y%m%d') { 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'); } $this_weekday = go_date_Calc::dayOfWeek($day,$month,$year); $interval = (6 + DATE_CALC_BEGIN_WEEKDAY - $this_weekday) % 7; return(go_date_Calc::daysToDate(go_date_Calc::dateToDays($day,$month,$year) + $interval,$format)); } // end func endOfWeek /** * Find the month day of the beginning of week after given date, * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.) * * @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 string format for returned date * * @access public * * @return string date in given format */ function beginOfNextWeek($day='',$month='',$year='',$format='%Y%m%d') { 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"); } $date = go_date_Calc::daysToDate( go_date_Calc::dateToDays($day+7,$month,$year),"%Y%m%d" ); $next_week_year = substr($date,0,4); $next_week_month = substr($date,4,2); $next_week_day = substr($date,6,2); return go_date_Calc::beginOfWeek( $next_week_day,$next_week_month,$next_week_year, $format ); $date = go_date_Calc::daysToDate(go_date_Calc::dateToDays($day+7,$month,$year),"%Y%m%d"); } // end func beginOfNextWeek /** * Find the month day of the beginning of week before given date, * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.) * * @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 string format for returned date * * @access public * * @return string date in given format */ function beginOfPrevWeek($day='',$month='',$year='',$format='%Y%m%d') { 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"); } $date = go_date_Calc::daysToDate( go_date_Calc::dateToDays($day-7,$month,$year),"%Y%m%d" ); $prev_week_year = substr($date,0,4); $prev_week_month = substr($date,4,2); $prev_week_day = substr($date,6,2); return go_date_Calc::beginOfWeek($prev_week_day,$prev_week_month,$prev_week_year,$format); $date = go_date_Calc::daysToDate(go_date_Calc::dateToDays($day-7,$month,$year),"%Y%m%d"); } // end func beginOfPrevWeek /** * Return an array with days in week * * @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 string format for returned date * * @access public * * @return array $week[$weekday] */ function getCalendarWeek($day='',$month='',$year='',$format='%Y%m%d') { 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'); } $week_array = array(); // date for the column of week $curr_day = go_date_Calc::beginOfWeek($day,$month,$year,'%E'); for($counter=0; $counter <= 6; $counter++) { $week_array[$counter] = go_date_Calc::daysToDate($curr_day,$format); $curr_day++; } return $week_array; } // end func getCalendarWeek /** * Return a set of arrays to construct a calendar month for * the given date. * * @param string month in format MM, default is current local month * @param string year in format CCYY, default is current local year * @param string format for returned date * * @access public * * @return array $month[$row][$col] */ function getCalendarMonth($month='',$year='',$format='%Y%m%d') { if (empty($year)) { $year = go_date_Calc::dateNow('%Y'); } if (empty($month)) { $month = go_date_Calc::dateNow('%m'); } $month_array = array(); // date for the first row, first column of calendar month if (DATE_CALC_BEGIN_WEEKDAY == 1) { if (go_date_Calc::firstOfMonthWeekday($month,$year) == 0) { $curr_day = go_date_Calc::dateToDays('01',$month,$year) - 6; } else { $curr_day = go_date_Calc::dateToDays('01',$month,$year) - go_date_Calc::firstOfMonthWeekday($month,$year) + 1; } } else { $curr_day = (go_date_Calc::dateToDays('01',$month,$year) - go_date_Calc::firstOfMonthWeekday($month,$year)); } // number of days in this month $daysInMonth = go_date_Calc::daysInMonth($month,$year); $weeksInMonth = go_date_Calc::weeksInMonth($month,$year); for($row_counter=0; $row_counter < $weeksInMonth; $row_counter++) { for($column_counter=0; $column_counter <= 6; $column_counter++) { $month_array[$row_counter][$column_counter] = go_date_Calc::daysToDate($curr_day,$format); $curr_day++; } } return $month_array; } // end func getCalendarMonth /** * Return a set of arrays to construct a calendar year for * the given date. * * @param string year in format CCYY, default current local year * @param string format for returned date * * @access public * * @return array $year[$month][$row][$col] */ function getCalendarYear($year='',$format='%Y%m%d') { if (empty($year)) { $year = go_date_Calc::dateNow('%Y'); } $year_array = array(); for($curr_month=0; $curr_month <=11; $curr_month++) { $year_array[$curr_month] = go_date_Calc::getCalendarMonth(sprintf('%02d',$curr_month+1),$year,$format); } return $year_array; } // end func getCalendarYear /** * Converts a date to number of days since a * distant unspecified epoch. * * @param string day in format DD * @param string month in format MM * @param string year in format CCYY * * @access public * * @return integer number of days */ function dateToDays($day,$month,$year) { $century = (int) substr($year,0,2); $year = (int) substr($year,2,2); if ($month > 2) { $month -= 3; } else { $month += 9; if ($year) { $year--; } else { $year = 99; $century --; } } return (floor(( 146097 * $century) / 4 ) + floor(( 1461 * $year) / 4 ) + floor(( 153 * $month + 2) / 5 ) + $day + 1721119); } // end func dateToDays /** * Converts number of days to a distant unspecified epoch. * * @param int number of days * @param string format for returned date * * @access public * * @return string date in specified format */ function daysToDate($days,$format='%Y%m%d') { $days -= 1721119; $century = floor(( 4 * $days - 1) / 146097); $days = floor(4 * $days - 1 - 146097 * $century); $day = floor($days / 4); $year = floor(( 4 * $day + 3) / 1461); $day = floor(4 * $day + 3 - 1461 * $year); $day = floor(($day + 4) / 4); $month = floor(( 5 * $day - 3) / 153); $day = floor(5 * $day - 3 - 153 * $month); $day = floor(($day + 5) / 5); if ($month < 10) { $month +=3; } else { $month -=9; if ($year++ == 99) { $year = 0; $century++; } } $century = sprintf('%02d',$century); $year = sprintf('%02d',$year); return(go_date_Calc::dateFormat($day,$month,$century.$year,$format)); } // end func daysToDate /** * Calculates the date of the Nth weekday of the month, * such as the second Saturday of January 2000. * * @param string occurance: 1=first, 2=second, 3=third, etc. * @param string dayOfWeek: 0=Sunday, 1=Monday, etc. * @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 NWeekdayOfMonth($occurance,$dayOfWeek,$month,$year,$format='%Y%m%d') { $year = sprintf('%04d',$year); $month = sprintf('%02d',$month); $DOW1day = sprintf('%02d',(($occurance - 1) * 7 + 1)); $DOW1 = go_date_Calc::dayOfWeek($DOW1day,$month,$year); $wdate = ($occurance - 1) * 7 + 1 + (7 + $dayOfWeek - $DOW1) % 7; if ( $wdate > go_date_Calc::daysInMonth($month,$year)) { return -1; } else { return(go_date_Calc::dateFormat($wdate,$month,$year,$format)); } } // end func NWeekdayOfMonth
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -