⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calc.php

📁 一个基于web的开源项目管理工具
💻 PHP
📖 第 1 页 / 共 4 页
字号:
    } // 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(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 year in format CCYY     * @param string month in format MM     * @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 = Date_Calc::dayOfWeek($DOW1day,$month,$year);        $wdate = ($occurance - 1) * 7 + 1 +                (7 + $dayOfWeek - $DOW1) % 7;        if( $wdate > Date_Calc::daysInMonth($month,$year)) {            return -1;        } else {            return(Date_Calc::dateFormat($wdate,$month,$year,$format));        }    } // end func NWeekdayOfMonth    /**     *  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 year in format CCYY     * @param string month in format MM     * @param string day in format DD     * @param string format for returned date     *     * @access public     *     * @return string date in given format     */    function dateFormat($day,$month,$year,$format)    {        if(!Date_Calc::isValidDate($day,$month,$year))        {            $year = Date_Calc::dateNow("%Y");            $month = Date_Calc::dateNow("%m");            $day = 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 .= Date_Calc::getWeekdayAbbrname($day,$month,$year);                        break;                    case "A":                        $output .= Date_Calc::getWeekdayFullname($day,$month,$year);                        break;                    case "b":                        $output .= Date_Calc::getMonthAbbrname($month);                        break;                    case "B":                        $output .= Date_Calc::getMonthFullname($month);                        break;                    case "d":                        $output .= sprintf("%02d",$day);                        break;                    case "e":                        $output .= $day;                        break;                    case "E":                        $output .= Date_Calc::dateToDays($day,$month,$year);                        break;                    case "j":                        $output .= 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 .= Date_Calc::dayOfWeek($day,$month,$year);                        break;                    case "U":                        $output .= 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 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 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 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) Date_Calc::dateNow("%m");        $month_names = 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 Date_Calc::getMonthFullname     */    function getMonthAbbrname($month,$length=3)    {        $month = (int)$month;        if(empty($month))            $month = Date_Calc::dateNow("%m");        return substr(Date_Calc::getMonthFullname($month), 0, $length);    } // end func getMonthAbbrname    /**     * Returns the full weekday name for the given date     *     * @param string year in format CCYY, default current local year     * @param string month in format MM, default current local month     * @param string day in format DD, default current local day     *     * @access public     *     * @return string full month name     */    function getWeekdayFullname($day="",$month="",$year="")    {        if(empty($year))            $year = Date_Calc::dateNow("%Y");        if(empty($month))            $month = Date_Calc::dateNow("%m");        if(empty($day))            $day = Date_Calc::dateNow("%d");        $weekday_names = Date_Calc::getWeekDays();        $weekday = Date_Calc::dayOfWeek($day,$month,$year);        return $weekday_names[$weekday];    } // end func getWeekdayFullname    /**     * Returns the abbreviated weekday name for the given date     *     * @param string year in format CCYY, default current local year     * @param string month in format MM, default current local month     * @param string day in format DD, default current local day     * @param int optional length of abbreviation, default is 3     *     * @access public     *     * @return string full month name     * @see Date_Calc::getWeekdayFullname     */    function getWeekdayAbbrname($day="",$month="",$year="",$length=3)    {        if(empty($year))            $year = Date_Calc::dateNow("%Y");        if(empty($month))            $month = Date_Calc::dateNow("%m");        if(empty($day))            $day = Date_Calc::dateNow("%d");        return substr(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 = Date_Calc::getMonthNames();        while(list($id, $name) = each($months)){            if(ereg($month, strtolower($name))){                return($id);            }        }        return(0);    }    /**    * Retunrs 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);    }    /**    * 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 class Date_calendar?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -