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

📄 calc.php

📁 apache windows下的一款好
💻 PHP
📖 第 1 页 / 共 4 页
字号:
<?php

// The constant telling us what day starts the week. Monday (1) is the
// international standard. Redefine this to 0 if you want weeks to
// begin on Sunday.
define('DATE_CALC_BEGIN_WEEKDAY', 1);

/**
 * Date_Calc is a calendar class used to calculate and
 * manipulate calendar dates and retrieve dates in a calendar
 * format. It does not rely on 32-bit system date stamps, so
 * you can display calendars and compare dates that date
 * pre 1970 and post 2038.
 *
 * This source file is subject to version 2.02 of the PHP license,
 * that is bundled with this package in the file LICENSE, and is
 * available at through the world-wide-web at
 * http://www.php.net/license/2_02.txt.
 * If you did not receive a copy of the PHP license and are unable to
 * obtain it through the world-wide-web, please send a note to
 * license@php.net so we can mail you a copy immediately.
 *
 * Copyright (c) 1999, 2000 ispi
 *
 * @access public
 *
 * @version 1.2.4
 * @author Monte Ohrt <monte@ispi.net>
 */

class Date_Calc {

    /**
     * Returns the current local date. NOTE: This function
     * retrieves the local date using strftime(), which may
     * or may not be 32-bit safe on your system.
     *
     * @param string the strftime() format to return the date
     *
     * @access public
     *
     * @return string the current date in specified format
     */

    function dateNow($format="%Y%m%d")
    {
        return(strftime($format,time()));

    } // end func dateNow

     /**
     * Returns true for valid date, false for invalid date.
     *
     * @param string year in format CCYY
     * @param string month in format MM
     * @param string day in format DD
     *
     * @access public
     *
     * @return boolean true/false
     */

    function isValidDate($day, $month, $year)
    {

        if(empty($year) || empty($month) || empty($day))
            return false;

        // must be digits only
        if(preg_match("/\D/",$year))
            return false;
        if(preg_match("/\D/",$month))
            return false;
        if(preg_match("/\D/",$day))
            return false;

        if($year < 0 || $year > 9999)
            return false;
        if($month < 1 || $month > 12)
            return false;
        if($day < 1 || $day > 31 || $day > Date_Calc::daysInMonth($month,$year))
            return false;

        return true;

    } // end func isValidDate

    function isLeapYear($year="")
    {

        if(empty($year))
            $year = Date_Calc::dateNow("%Y");

        if(strlen($year) != 4)
            return false;

        if(preg_match("/\D/",$year))
            return false;

        return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);

    } // end func isLeapYear

    /**
     * Determines if given date is a future date from now.
     *
     * @param string year in format CCYY
     * @param string month in format MM
     * @param string day in format DD
     *
     * @access public
     *
     * @return boolean true/false
     */

    function isFutureDate($day,$month,$year)
    {
        $this_year = Date_Calc::dateNow("%Y");
        $this_month = Date_Calc::dateNow("%m");
        $this_day = Date_Calc::dateNow("%d");


        if($year > $this_year)
            return true;
        elseif($year == $this_year)
            if($month > $this_month)
                return true;
            elseif($month == $this_month)
                if($day > $this_day)
                    return true;

        return false;

    } // end func isFutureDate

    /**
     * Determines if given date is a past date from now.
     *
     * @param string year in format CCYY
     * @param string month in format MM
     * @param string day in format DD
     *
     * @access public
     *
     * @return boolean true/false
     */

    function isPastDate($day,$month,$year)
    {
        $this_year = Date_Calc::dateNow("%Y");
        $this_month = Date_Calc::dateNow("%m");
        $this_day = Date_Calc::dateNow("%d");


        if($year < $this_year)
            return true;
        elseif($year == $this_year)
            if($month < $this_month)
                return true;
            elseif($month == $this_month)
                if($day < $this_day)
                    return true;

        return false;

    } // end func isPastDate

    /**
     * Returns day of week for given date, 0=Sunday
     *
     * @param string year in format CCYY, default is current local year
     * @param string month in format MM, default is current local month
     * @param string day in format DD, default is current local day
     *
     * @access public
     *
     * @return int $weekday_number
     */

    function dayOfWeek($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");

        if($month > 2)
            $month -= 2;
        else
        {
            $month += 10;
            $year--;
        }

        $day =     ( floor((13 * $month - 1) / 5) +
                $day + ($year % 100) +
                floor(($year % 100) / 4) +
                floor(($year / 100) / 4) - 2 *
                floor($year / 100) + 77);

        $weekday_number = (($day - 7 * floor($day / 7)));

        return $weekday_number;

    } // end func dayOfWeek

    /**
     * Returns week of the year, first Sunday is first day of first week
     *
     * @param string year in format CCYY
     * @param string month in format MM
     * @param string day in format DD
     *
     * @access public
     *
     * @return integer $week_number
     */

    function weekOfYear($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");

        $week_year = $year - 1501;
        $week_day = $week_year * 365 + floor($week_year / 4) - 29872 + 1
                - floor($week_year / 100) + floor(($week_year - 300) / 400);

        $week_number =
                floor((Date_Calc::julianDate($day,$month,$year) + floor(($week_day + 4) % 7)) / 7);

        return $week_number;

    } // end func weekOfYear

    /**
     * Returns number of days since 31 December of year before given date.
     *
     * @param string year in format CCYY, default is current local year
     * @param string month in format MM, default is current local month
     * @param string day in format DD, default is current local day
     *
     * @access public
     *
     * @return int $julian
     */

    function julianDate($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");

        $days = array(0,31,59,90,120,151,181,212,243,273,304,334);

        $julian = ($days[$month - 1] + $day);

        if($month > 2 && Date_Calc::isLeapYear($year))
            $julian++;

        return($julian);

    } // end func julianDate

    /**
     * Returns quarter of the year for 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 int $year_quarter
     */

    function quarterOfYear($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");

        $year_quarter = (intval(($month - 1) / 3 + 1));

        return $year_quarter;

    } // end func quarterOfYear

    /**
     * Returns date of begin of next month of 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 string format for returned date
     *
     * @access public
     *
     * @return string date in given format
     */

    function beginOfNextMonth($day="",$month="",$year="",$format="%Y%m%d")
    {
        if(empty($year))
            $year = Date_Calc::dateNow("%Y");
        if(empty($month))
            $month = Date_Calc::dateNow("%m");
        if(empty($day))
            $day = Date_Calc::dateNow("%d");

        if($month < 12)
        {
            $month++;
            $day=1;
        }
        else
        {
            $year++;
            $month=1;
            $day=1;
        }

        return Date_Calc::dateFormat($day,$month,$year,$format);

    } // end func beginOfNextMonth

    /**
     * Returns date of the last day of next month of 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 string format for returned date
     *
     * @access public
     *
     * @return string date in given format
     */

    function endOfNextMonth($day="",$month="",$year="",$format="%Y%m%d")
    {
        if(empty($year))
            $year = Date_Calc::dateNow("%Y");
        if(empty($month))
            $month = Date_Calc::dateNow("%m");
        if(empty($day))
            $day = Date_Calc::dateNow("%d");


        if($month < 12)
        {
            $month++;
        }
        else
        {
            $year++;
            $month=1;
        }

        $day = Date_Calc::daysInMonth($month,$year);

        return Date_Calc::dateFormat($day,$month,$year,$format);

    } // end func endOfNextMonth

    /**
     * Returns date of the first day of previous month of 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 string format for returned date
     *
     * @access public
     *
     * @return string date in given format
     */

    function beginOfPrevMonth($day="",$month="",$year="",$format="%Y%m%d")
    {

⌨️ 快捷键说明

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