首頁  >  文章  >  後端開發  >  php 時間日期工具類 星座/幹支/生肖

php 時間日期工具類 星座/幹支/生肖

巴扎黑
巴扎黑原創
2016-11-24 15:20:151225瀏覽

如果系統沒有設定時區,那麼得到的結果是UTC時間,相對中國用戶來說,就是相差了8個小時

<?php
class Date
{
    /**
     * 获取或者设置时区
     *
     * @param int $timezone 时区
     * @return string | bool
     */
    public static function timeZone($timezone = &#39;&#39;)
    {
        if ($timezone) {
            return function_exists(&#39;date_default_timezone_set&#39;) ? date_default_timezone_set($timezone) : putenv("TZ={$timezone}");
        } else {
            return function_exists(&#39;date_default_timezone_get&#39;) ? date_default_timezone_get() : date(&#39;e&#39;);
        }
    }
    /**
     * 检查年、月、日是有效组合。
     * @param integer $y year
     * @param integer $m month
     * @param integer $d day
     * @return boolean true if valid date, semantic check only.
     */
    public static function isValidDate($y, $m, $d)
    {
        return checkdate($m, $d, $y);
    }
    /**
     * 检查日期是否合法日期。
     * @param string $date 2012-1-12
     * @param string $separator
     * @return boolean true if valid date, semantic check only.
     */
    public static function checkDate($date, $separator = "-")
    {
        $dateArr = explode($separator, $date);
        return self::isValidDate($dateArr[0], $dateArr[1], $dateArr[2]);
    }
    /**
     * 检查是否有效的小时、分钟和秒.
     * @param integer $h hour
     * @param integer $m minute
     * @param integer $s second
     * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12.
     * @return boolean true if valid date, semantic check only.
     */
    public static function isValidTime($h, $m, $s, $hs24 = true)
    {
        if ($hs24 && ($h < 0 || $h > 23) || !$hs24 && ($h < 1 || $h > 12)) return false;
        if ($m > 59 || $m < 0) return false;
        if ($s > 59 || $s < 0) return false;
        return true;
    }
    /**
     * 检查时间是否合法时间
     * @param integer $time
     * @param string $separator
     * @return boolean true if valid date, semantic check only.
     * @since 1.0.5
     */
    public static function checkTime($time, $separator = ":")
    {
        $timeArr = explode($separator, $time);
        return self::isValidTime($timeArr[0], $timeArr[1], $timeArr[2]);
    }
    /**
     * 获得时间戳
     *
     * @param int $dateTime 默认为空,则以当前时间戳返回
     * @return int
     */
    public static function getTimeStamp($dateTime = null)
    {
        return $dateTime ? is_numeric($dateTime) ? $dateTime : strtotime($dateTime) : time();
    }
    /**
     * 格式化输出
     *
     * @param string $format 目标格式,默认为空则以Y-m-d H:i:s格式输出
     * @param int $dateTime Unix时间戳,默认为空则获取当前时间戳
     * @return string
     */
    public static function format($format = null, $dateTime = null)
    {
        return date($format ? $format : &#39;Y-m-d H:i:s&#39;, self::getTimeStamp($dateTime));
    }
    /**
     * 获取星期
     *
     * @param string $date 日期
     * @return int
     */
    public static function getWeekNum($date, $separator = "-")
    {
        $dateArr = explode($separator, $date);
        return date("w", mktime(0, 0, 0, $dateArr[1], $dateArr[2], $dateArr[0]));
    }
    /**
     * 获取星期
     *
     * @param int $week 星期,默认为当前时间获取
     * @return string
     */
    public static function getWeek($week = null)
    {
        $week = $week ? $week : self::format(&#39;w&#39;);
        $weekArr = array(&#39;星期天&#39;, &#39;星期一&#39;, &#39;星期二&#39;, &#39;星期三&#39;, &#39;星期四&#39;, &#39;星期五&#39;, &#39;星期六&#39;);
        return $weekArr[$week];
    }
    /**
     * 判断是否为闰年
     *
     * @param int $year 年份,默认为当前年份
     * @return bool
     */
    public static function isLeapYear($year = null)
    {
        $year = $year ? $year : self::format(&#39;Y&#39;);
        return ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0);
    }
    /**
     * 获取一年中有多少天
     * @param int $year 年份,默认为当前年份
     */
    public static function getDaysInYear($year = null)
    {
        $year = $year ? $year : self::format(&#39;Y&#39;);
        return self::isLeapYear($year) ? 366 : 365;
    }
    /**
     * 获取一天中的时段
     *
     * @param int $hour 小时,默认为当前小时
     * @return string
     */
    public static function getPeriodOfTime($hour = null)
    {
        $hour = $hour ? $hour : self::format(&#39;G&#39;);
        $period = null;
        if ($hour >= 0 && $hour < 6) {
            $period = &#39;凌晨&#39;;
        } elseif ($hour >= 6 && $hour < 8) {
            $period = &#39;早上&#39;;
        } elseif ($hour >= 8 && $hour < 11) {
            $period = &#39;上午&#39;;
        } elseif ($hour >= 11 && $hour < 13) {
            $period = &#39;中午&#39;;
        } elseif ($hour >= 13 && $hour < 15) {
            $period = &#39;响午&#39;;
        } elseif ($hour >= 15 && $hour < 18) {
            $period = &#39;下午&#39;;
        } elseif ($hour >= 18 && $hour < 20) {
            $period = &#39;傍晚&#39;;
        } elseif ($hour >= 20 && $hour < 22) {
            $period = &#39;晚上&#39;;
        } elseif ($hour >= 22 && $hour <= 23) {
            $period = &#39;深夜&#39;;
        }
        return $period;
    }
    public static function timeFromNow($dateline)
    {
        if (empty($dateline)) return false;
        $seconds = time() - $dateline;
        if ($seconds < 60) {
            return "1分钟前";
        } elseif ($seconds < 3600) {
            return floor($seconds / 60) . "分钟前";
        } elseif ($seconds < 24 * 3600) {
            return floor($seconds / 3600) . "小时前";
        } elseif ($seconds < 48 * 3600) {
            return date("昨天 H:i", $dateline) . "";
        } else {
            return date(&#39;Y-m-d&#39;, $dateline);
        }
    }
    /**
     * 日期数字转中文,适用于日、月、周
     * @param int $day 日期数字,默认为当前日期
     * @return string
     */
    public static function numberToChinese($number)
    {
        $chineseArr = array(&#39;一&#39;, &#39;二&#39;, &#39;三&#39;, &#39;四&#39;, &#39;五&#39;, &#39;六&#39;, &#39;七&#39;, &#39;八&#39;, &#39;九&#39;, &#39;十&#39;);
        $chineseStr = null;
        if ($number < 10) {
            $chineseStr = $chineseArr[$number - 1];
        } elseif ($number < 20) {
            $chineseStr = &#39;十&#39; . $chineseArr[$number - 11];
        } elseif ($number < 30) {
            $chineseStr = &#39;二十&#39; . $chineseArr[$number - 21];
        } else {
            $chineseStr = &#39;三十&#39; . $chineseArr[$number - 31];
        }
        return $chineseStr;
    }
    /**
     * 年份数字转中文
     *
     * @param int $year 年份数字,默认为当前年份
     * @return string
     */
    public static function yearToChinese($year = null, $flag = false)
    {
        $year = $year ? intval($year) : self::format(&#39;Y&#39;);
        $data = array(&#39;零&#39;, &#39;一&#39;, &#39;二&#39;, &#39;三&#39;, &#39;四&#39;, &#39;五&#39;, &#39;六&#39;, &#39;七&#39;, &#39;八&#39;, &#39;九&#39;);
        $chineseStr = null;
        for ($i = 0; $i < 4; $i++) {
            $chineseStr .= $data[substr($year, $i, 1)];
        }
        return $flag ? &#39;公元&#39; . $chineseStr : $chineseStr;
    }
    /**
     * 获取日期所属的星座、干支、生肖
     *
     * @param string $type 获取信息类型(SX:生肖、GZ:干支、XZ:星座)
     * @return string
     */
    public static function dateInfo($type, $date = null)
    {
        $year = self::format(&#39;Y&#39;, $date);
        $month = self::format(&#39;m&#39;, $date);
        $day = self::format(&#39;d&#39;, $date);
        $result = null;
        switch ($type) {
            case &#39;SX&#39;:
                $data = array(&#39;鼠&#39;, &#39;牛&#39;, &#39;虎&#39;, &#39;兔&#39;, &#39;龙&#39;, &#39;蛇&#39;, &#39;马&#39;, &#39;羊&#39;, &#39;猴&#39;, &#39;鸡&#39;, &#39;狗&#39;, &#39;猪&#39;);
                $result = $data[($year - 4) % 12];
                break;
            case &#39;GZ&#39;:
                $data = array(
                    array(&#39;甲&#39;, &#39;乙&#39;, &#39;丙&#39;, &#39;丁&#39;, &#39;戊&#39;, &#39;己&#39;, &#39;庚&#39;, &#39;辛&#39;, &#39;壬&#39;, &#39;癸&#39;),
                    array(&#39;子&#39;, &#39;丑&#39;, &#39;寅&#39;, &#39;卯&#39;, &#39;辰&#39;, &#39;巳&#39;, &#39;午&#39;, &#39;未&#39;, &#39;申&#39;, &#39;酉&#39;, &#39;戌&#39;, &#39;亥&#39;)
                );
                $num = $year - 1900 + 36;
                $result = $data[0][$num % 10] . $data[1][$num % 12];
                break;
            case &#39;XZ&#39;:
                $data = array(&#39;摩羯&#39;, &#39;宝瓶&#39;, &#39;双鱼&#39;, &#39;白羊&#39;, &#39;金牛&#39;, &#39;双子&#39;, &#39;巨蟹&#39;, &#39;狮子&#39;, &#39;处女&#39;, &#39;天秤&#39;, &#39;天蝎&#39;, &#39;射手&#39;);
                $zone = array(1222, 122, 222, 321, 421, 522, 622, 722, 822, 922, 1022, 1122, 1222);
                if ((100 * $month + $day) >= $zone[0] || (100 * $month + $day) < $zone[1]) {
                    $i = 0;
                } else {
                    for ($i = 1; $i < 12; $i++) {
                        if ((100 * $month + $day) >= $zone[$i] && (100 * $month + $day) < $zone[$i + 1]) break;
                    }
                }
                $result = $data[$i] . &#39;座&#39;;
                break;
        }
        return $result;
    }
    /**
     * 获取两个日期的差
     *
     * @param string $interval 日期差的间隔类型,(Y:年、M:月、W:星期、D:日期、H:时、N:分、S:秒)
     * @param int $startDateTime 开始日期
     * @param int $endDateTime 结束日期
     * @return int
     */
    public static function dateDiff($interval, $startDateTime, $endDateTime)
    {
        $diff = self::getTimeStamp($endDateTime) - self::getTimeStamp($startDateTime);
        switch ($interval) {
            case &#39;Y&#39;: //年
                $result = bcdiv($diff, 60 * 60 * 24 * 365);
                break;
            case &#39;M&#39;: //月
                $result = bcdiv($diff, 60 * 60 * 24 * 30);
                break;
            case &#39;W&#39;: //星期
                $result = bcdiv($diff, 60 * 60 * 24 * 7);
                break;
            case &#39;D&#39;: //日
                $result = bcdiv($diff, 60 * 60 * 24);
                break;
            case &#39;H&#39;: //时
                $result = bcdiv($diff, 60 * 60);
                break;
            case &#39;N&#39;: //分
                $result = bcdiv($diff, 60);
                break;
            case &#39;S&#39;: //秒
            default:
                $result = $diff;
                break;
        }
        return $result;
    }
    /**
     * 返回指定日期在一段时间间隔时间后的日期
     *
     * @param string $interval 时间间隔类型,(Y:年、Q:季度、M:月、W:星期、D:日期、H:时、N:分、S:秒)
     * @param int $value 时间间隔数值,数值为正数获取未来的时间,数值为负数获取过去的时间
     * @param string $dateTime 日期
     * @param string $format 返回的日期转换格式
     * @return string 返回追加后的日期
     */
    public static function dateAdd($interval, $value, $dateTime = null, $format = null)
    {
        $dateTime = $dateTime ? $dateTime : self::format();
        $date = getdate(self::getTimeStamp($dateTime));
        switch ($interval) {
            case &#39;Y&#39;: //年
                $date[&#39;year&#39;] += $value;
                break;
            case &#39;Q&#39;: //季度
                $date[&#39;mon&#39;] += ($value * 3);
                break;
            case &#39;M&#39;: //月
                $date[&#39;mon&#39;] += $value;
                break;
            case &#39;W&#39;: //星期
                $date[&#39;mday&#39;] += ($value * 7);
                break;
            case &#39;D&#39;: //日
                $date[&#39;mday&#39;] += $value;
                break;
            case &#39;H&#39;: //时
                $date[&#39;hours&#39;] += $value;
                break;
            case &#39;N&#39;: //分
                $date[&#39;minutes&#39;] += $value;
                break;
            case &#39;S&#39;: //秒
            default:
                $date[&#39;seconds&#39;] += $value;
                break;
        }
        return self::format($format, mktime($date[&#39;hours&#39;], $date[&#39;minutes&#39;], $date[&#39;seconds&#39;], $date[&#39;mon&#39;], $date[&#39;mday&#39;], $date[&#39;year&#39;]));
    }
    /**
     * 根据年份获取每个月的天数
     *
     * @param int $year 年份
     * @return array 月份天数数组
     */
    public static function getDaysByMonthsOfYear($year = null)
    {
        $year = $year ? $year : self::format(&#39;Y&#39;);
        $months = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        if (self::isLeapYear($year)) $months[1] = 29;
        return $months;
    }
    /**
     * 返回某年的某个月有多少天
     *
     * @param int $month 月份
     * @param int $year 年份
     * @return int 月份天数
     */
    public static function getDaysByMonth($month, $year)
    {
        $months = self::getDaysByMonthsOfYear($year);
        $value = $months[$month - 1];
        return !$value ? 0 : $value;
    }
    /**
     * 获取年份的第一天
     *
     * @param int $year 年份
     * @param int $format 返回的日期格式
     * @return string 返回的日期
     */
    public static function firstDayOfYear($year = null, $format = &#39;Y-m-d&#39;)
    {
        $year = $year ? $year : self::format(&#39;Y&#39;);
        return self::format($format, mktime(0, 0, 0, 1, 1, $year));
    }
    /**
     * 获取年份最后一天
     *
     * @param int $year 年份
     * @param int $format 返回的日期格式
     * @return string 返回的日期
     */
    public static function lastDayOfYear($year = null, $format = &#39;Y-m-d&#39;)
    {
        $year = $year ? $year : self::format(&#39;Y&#39;);
        return self::format($format, mktime(0, 0, 0, 1, 0, $year + 1));
    }
    /**
     * 获取月份的第一天
     *
     * @param int $month 月份
     * @param int $year 年份
     * @param int $format 返回的日期格式
     * @return string 返回的日期
     */
    public static function firstDayOfMonth($month = null, $year = null, $format = &#39;Y-m-d&#39;)
    {
        $year = $year ? $year : self::format(&#39;Y&#39;);
        $month = $month ? $month : self::format(&#39;m&#39;);
        return self::format($format, mktime(0, 0, 0, $month, 1, $year));
    }
    /**
     * 获取月份最后一天
     *
     * @param int $month 月份
     * @param int $year 年份
     * @param int $format 返回的日期格式
     * @return string 返回的日期
     */
    public static function lastDayOfMonth($month = null, $year = null, $format = &#39;Y-m-d&#39;)
    {
        $year = $year ? $year : self::format(&#39;Y&#39;);
        $month = $month ? $month : self::format(&#39;m&#39;);
        return self::format($format, mktime(0, 0, 0, $month + 1, 0, $year));
    }
    /**
     * 获取两个日期之间范围
     *
     * @param string $startDateTime
     * @param string $endDateTime
     * @param string $format
     * @return array 返回日期数组
     */
    public static function getDayRangeInBetweenDate($startDateTime, $endDateTime, $sort = false, $format = &#39;Y-m-d&#39;)
    {
        $startDateTime = self::getTimeStamp($startDateTime);
        $endDateTime = self::getTimeStamp($endDateTime);
        $num = ($endDateTime - $startDateTime) / 86400;
        $dateArr = array();
        for ($i = 0; $i <= $num; $i++) {
            $dateArr[] = self::format($format, $startDateTime + 86400 * $i);
        }
        return $sort ? array_reverse($dateArr) : $dateArr;
    }
}
if (!function_exists(&#39;bcdiv&#39;)) {
    function bcdiv($first, $second, $scale = 0)
    {
        $res = $first / $second;
        return round($res, $scale);
    }
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn