php 时间日期工具类 星座/干支/生肖

如果系统没有设置时区,那么获得的结果是UTC时间,相对中国用户来说,就是相差了8个小时

Java代码   收藏代码
  1. <?php  
  2.   
  3. class Date  
  4. {  
  5.     /** 
  6.      * 获取或者设置时区 
  7.      * 
  8.      * @param int $timezone 时区 
  9.      * @return string | bool 
  10.      */  
  11.     public static function timeZone($timezone = '')  
  12.     {  
  13.         if ($timezone) {  
  14.             return function_exists('date_default_timezone_set') ? date_default_timezone_set($timezone) : putenv("TZ={$timezone}");  
  15.         } else {  
  16.             return function_exists('date_default_timezone_get') ? date_default_timezone_get() : date('e');  
  17.         }  
  18.     }  
  19.   
  20.     /** 
  21.      * 检查年、月、日是有效组合。 
  22.      * @param integer $y year 
  23.      * @param integer $m month 
  24.      * @param integer $d day 
  25.      * @return boolean true if valid date, semantic check only. 
  26.      */  
  27.     public static function isValidDate($y, $m, $d)  
  28.     {  
  29.         return checkdate($m, $d, $y);  
  30.     }  
  31.   
  32.     /** 
  33.      * 检查日期是否合法日期。 
  34.      * @param string $date 2012-1-12 
  35.      * @param string $separator 
  36.      * @return boolean true if valid date, semantic check only. 
  37.      */  
  38.     public static function checkDate($date, $separator = "-")  
  39.     {  
  40.         $dateArr = explode($separator, $date);  
  41.         return self::isValidDate($dateArr[0], $dateArr[1], $dateArr[2]);  
  42.     }  
  43.   
  44.     /** 
  45.      * 检查是否有效的小时、分钟和秒. 
  46.      * @param integer $h hour 
  47.      * @param integer $m minute 
  48.      * @param integer $s second 
  49.      * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12. 
  50.      * @return boolean true if valid date, semantic check only. 
  51.      */  
  52.     public static function isValidTime($h, $m, $s, $hs24 = true)  
  53.     {  
  54.         if ($hs24 && ($h < 0 || $h > 23) || !$hs24 && ($h < 1 || $h > 12)) return false;  
  55.         if ($m > 59 || $m < 0return false;  
  56.         if ($s > 59 || $s < 0return false;  
  57.         return true;  
  58.     }  
  59.   
  60.     /** 
  61.      * 检查时间是否合法时间 
  62.      * @param integer $time 
  63.      * @param string $separator 
  64.      * @return boolean true if valid date, semantic check only. 
  65.      * @since 1.0.5 
  66.      */  
  67.     public static function checkTime($time, $separator = ":")  
  68.     {  
  69.         $timeArr = explode($separator, $time);  
  70.         return self::isValidTime($timeArr[0], $timeArr[1], $timeArr[2]);  
  71.     }  
  72.   
  73.     /** 
  74.      * 获得时间戳 
  75.      * 
  76.      * @param int $dateTime 默认为空,则以当前时间戳返回 
  77.      * @return int 
  78.      */  
  79.     public static function getTimeStamp($dateTime = null)  
  80.     {  
  81.         return $dateTime ? is_numeric($dateTime) ? $dateTime : strtotime($dateTime) : time();  
  82.     }  
  83.   
  84.     /** 
  85.      * 格式化输出 
  86.      * 
  87.      * @param string $format 目标格式,默认为空则以Y-m-d H:i:s格式输出 
  88.      * @param int $dateTime Unix时间戳,默认为空则获取当前时间戳 
  89.      * @return string 
  90.      */  
  91.     public static function format($format = null, $dateTime = null)  
  92.     {  
  93.         return date($format ? $format : 'Y-m-d H:i:s', self::getTimeStamp($dateTime));  
  94.     }  
  95.   
  96.     /** 
  97.      * 获取星期 
  98.      * 
  99.      * @param string $date 日期 
  100.      * @return int 
  101.      */  
  102.     public static function getWeekNum($date, $separator = "-")  
  103.     {  
  104.         $dateArr = explode($separator, $date);  
  105.         return date("w", mktime(000, $dateArr[1], $dateArr[2], $dateArr[0]));  
  106.     }  
  107.   
  108.     /** 
  109.      * 获取星期 
  110.      * 
  111.      * @param int $week 星期,默认为当前时间获取 
  112.      * @return string 
  113.      */  
  114.     public static function getWeek($week = null)  
  115.     {  
  116.         $week = $week ? $week : self::format('w');  
  117.         $weekArr = array('星期天''星期一''星期二''星期三''星期四''星期五''星期六');  
  118.         return $weekArr[$week];  
  119.     }  
  120.   
  121.     /** 
  122.      * 判断是否为闰年 
  123.      * 
  124.      * @param int $year 年份,默认为当前年份 
  125.      * @return bool 
  126.      */  
  127.     public static function isLeapYear($year = null)  
  128.     {  
  129.         $year = $year ? $year : self::format('Y');  
  130.         return ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0);  
  131.     }  
  132.   
  133.     /** 
  134.      * 获取一年中有多少天 
  135.      * @param int $year 年份,默认为当前年份 
  136.      */  
  137.     public static function getDaysInYear($year = null)  
  138.     {  
  139.         $year = $year ? $year : self::format('Y');  
  140.         return self::isLeapYear($year) ? 366 : 365;  
  141.     }  
  142.   
  143.     /** 
  144.      * 获取一天中的时段 
  145.      * 
  146.      * @param int $hour 小时,默认为当前小时 
  147.      * @return string 
  148.      */  
  149.     public static function getPeriodOfTime($hour = null)  
  150.     {  
  151.         $hour = $hour ? $hour : self::format('G');  
  152.         $period = null;  
  153.         if ($hour >= 0 && $hour < 6) {  
  154.             $period = '凌晨';  
  155.         } elseif ($hour >= 6 && $hour < 8) {  
  156.             $period = '早上';  
  157.         } elseif ($hour >= 8 && $hour < 11) {  
  158.             $period = '上午';  
  159.         } elseif ($hour >= 11 && $hour < 13) {  
  160.             $period = '中午';  
  161.         } elseif ($hour >= 13 && $hour < 15) {  
  162.             $period = '响午';  
  163.         } elseif ($hour >= 15 && $hour < 18) {  
  164.             $period = '下午';  
  165.         } elseif ($hour >= 18 && $hour < 20) {  
  166.             $period = '傍晚';  
  167.         } elseif ($hour >= 20 && $hour < 22) {  
  168.             $period = '晚上';  
  169.         } elseif ($hour >= 22 && $hour <= 23) {  
  170.             $period = '深夜';  
  171.         }  
  172.         return $period;  
  173.     }  
  174.   
  175.     public static function timeFromNow($dateline)  
  176.     {  
  177.         if (empty($dateline)) return false;  
  178.         $seconds = time() - $dateline;  
  179.         if ($seconds < 60) {  
  180.             return "1分钟前";  
  181.         } elseif ($seconds < 3600) {  
  182.             return floor($seconds / 60) . "分钟前";  
  183.         } elseif ($seconds < 24 * 3600) {  
  184.             return floor($seconds / 3600) . "小时前";  
  185.         } elseif ($seconds < 48 * 3600) {  
  186.             return date("昨天 H:i", $dateline) . "";  
  187.         } else {  
  188.             return date('Y-m-d', $dateline);  
  189.         }  
  190.     }  
  191.   
  192.     /** 
  193.      * 日期数字转中文,适用于日、月、周 
  194.      * @param int $day 日期数字,默认为当前日期 
  195.      * @return string 
  196.      */  
  197.     public static function numberToChinese($number)  
  198.     {  
  199.         $chineseArr = array('一''二''三''四''五''六''七''八''九''十');  
  200.         $chineseStr = null;  
  201.         if ($number < 10) {  
  202.             $chineseStr = $chineseArr[$number - 1];  
  203.         } elseif ($number < 20) {  
  204.             $chineseStr = '十' . $chineseArr[$number - 11];  
  205.         } elseif ($number < 30) {  
  206.             $chineseStr = '二十' . $chineseArr[$number - 21];  
  207.         } else {  
  208.             $chineseStr = '三十' . $chineseArr[$number - 31];  
  209.         }  
  210.         return $chineseStr;  
  211.     }  
  212.   
  213.     /** 
  214.      * 年份数字转中文 
  215.      * 
  216.      * @param int $year 年份数字,默认为当前年份 
  217.      * @return string 
  218.      */  
  219.     public static function yearToChinese($year = null, $flag = false)  
  220.     {  
  221.         $year = $year ? intval($year) : self::format('Y');  
  222.         $data = array('零''一''二''三''四''五''六''七''八''九');  
  223.         $chineseStr = null;  
  224.         for ($i = 0; $i < 4; $i++) {  
  225.             $chineseStr .= $data[substr($year, $i, 1)];  
  226.         }  
  227.         return $flag ? '公元' . $chineseStr : $chineseStr;  
  228.     }  
  229.   
  230.   
  231.     /** 
  232.      * 获取日期所属的星座、干支、生肖 
  233.      * 
  234.      * @param string $type 获取信息类型(SX:生肖、GZ:干支、XZ:星座) 
  235.      * @return string 
  236.      */  
  237.     public static function dateInfo($type, $date = null)  
  238.     {  
  239.         $year = self::format('Y', $date);  
  240.         $month = self::format('m', $date);  
  241.         $day = self::format('d', $date);  
  242.         $result = null;  
  243.         switch ($type) {  
  244.             case 'SX':  
  245.                 $data = array('鼠''牛''虎''兔''龙''蛇''马''羊''猴''鸡''狗''猪');  
  246.                 $result = $data[($year - 4) % 12];  
  247.                 break;  
  248.             case 'GZ':  
  249.                 $data = array(  
  250.                     array('甲''乙''丙''丁''戊''己''庚''辛''壬''癸'),  
  251.                     array('子''丑''寅''卯''辰''巳''午''未''申''酉''戌''亥')  
  252.                 );  
  253.                 $num = $year - 1900 + 36;  
  254.                 $result = $data[0][$num % 10] . $data[1][$num % 12];  
  255.                 break;  
  256.             case 'XZ':  
  257.                 $data = array('摩羯''宝瓶''双鱼''白羊''金牛''双子''巨蟹''狮子''处女''天秤''天蝎''射手');  
  258.                 $zone = array(1222122222321421522622722822922102211221222);  
  259.                 if ((100 * $month + $day) >= $zone[0] || (100 * $month + $day) < $zone[1]) {  
  260.                     $i = 0;  
  261.                 } else {  
  262.                     for ($i = 1; $i < 12; $i++) {  
  263.                         if ((100 * $month + $day) >= $zone[$i] && (100 * $month + $day) < $zone[$i + 1]) break;  
  264.                     }  
  265.                 }  
  266.                 $result = $data[$i] . '座';  
  267.                 break;  
  268.         }  
  269.         return $result;  
  270.     }  
  271.   
  272.   
  273.     /** 
  274.      * 获取两个日期的差 
  275.      * 
  276.      * @param string $interval 日期差的间隔类型,(Y:年、M:月、W:星期、D:日期、H:时、N:分、S:秒) 
  277.      * @param int $startDateTime 开始日期 
  278.      * @param int $endDateTime 结束日期 
  279.      * @return int 
  280.      */  
  281.     public static function dateDiff($interval, $startDateTime, $endDateTime)  
  282.     {  
  283.         $diff = self::getTimeStamp($endDateTime) - self::getTimeStamp($startDateTime);  
  284.         switch ($interval) {  
  285.             case 'Y'//年  
  286.                 $result = bcdiv($diff, 60 * 60 * 24 * 365);  
  287.                 break;  
  288.             case 'M'//月  
  289.                 $result = bcdiv($diff, 60 * 60 * 24 * 30);  
  290.                 break;  
  291.             case 'W'//星期  
  292.                 $result = bcdiv($diff, 60 * 60 * 24 * 7);  
  293.                 break;  
  294.             case 'D'//日  
  295.                 $result = bcdiv($diff, 60 * 60 * 24);  
  296.                 break;  
  297.             case 'H'//时  
  298.                 $result = bcdiv($diff, 60 * 60);  
  299.                 break;  
  300.             case 'N'//分  
  301.                 $result = bcdiv($diff, 60);  
  302.                 break;  
  303.             case 'S'//秒  
  304.             default:  
  305.                 $result = $diff;  
  306.                 break;  
  307.         }  
  308.         return $result;  
  309.     }  
  310.   
  311.   
  312.     /** 
  313.      * 返回指定日期在一段时间间隔时间后的日期 
  314.      * 
  315.      * @param string $interval 时间间隔类型,(Y:年、Q:季度、M:月、W:星期、D:日期、H:时、N:分、S:秒) 
  316.      * @param int $value 时间间隔数值,数值为正数获取未来的时间,数值为负数获取过去的时间 
  317.      * @param string $dateTime 日期 
  318.      * @param string $format 返回的日期转换格式 
  319.      * @return string 返回追加后的日期 
  320.      */  
  321.     public static function dateAdd($interval, $value, $dateTime = null, $format = null)  
  322.     {  
  323.         $dateTime = $dateTime ? $dateTime : self::format();  
  324.         $date = getdate(self::getTimeStamp($dateTime));  
  325.         switch ($interval) {  
  326.             case 'Y'//年  
  327.                 $date['year'] += $value;  
  328.                 break;  
  329.             case 'Q'//季度  
  330.                 $date['mon'] += ($value * 3);  
  331.                 break;  
  332.             case 'M'//月  
  333.                 $date['mon'] += $value;  
  334.                 break;  
  335.             case 'W'//星期  
  336.                 $date['mday'] += ($value * 7);  
  337.                 break;  
  338.             case 'D'//日  
  339.                 $date['mday'] += $value;  
  340.                 break;  
  341.             case 'H'//时  
  342.                 $date['hours'] += $value;  
  343.                 break;  
  344.             case 'N'//分  
  345.                 $date['minutes'] += $value;  
  346.                 break;  
  347.             case 'S'//秒  
  348.             default:  
  349.                 $date['seconds'] += $value;  
  350.                 break;  
  351.         }  
  352.         return self::format($format, mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']));  
  353.     }  
  354.   
  355.   
  356.     /** 
  357.      * 根据年份获取每个月的天数 
  358.      * 
  359.      * @param int $year 年份 
  360.      * @return array 月份天数数组 
  361.      */  
  362.     public static function getDaysByMonthsOfYear($year = null)  
  363.     {  
  364.         $year = $year ? $year : self::format('Y');  
  365.         $months = array(312831303130313130313031);  
  366.         if (self::isLeapYear($year)) $months[1] = 29;  
  367.         return $months;  
  368.     }  
  369.   
  370.   
  371.     /** 
  372.      * 返回某年的某个月有多少天 
  373.      * 
  374.      * @param int $month 月份 
  375.      * @param int $year 年份 
  376.      * @return int 月份天数 
  377.      */  
  378.     public static function getDaysByMonth($month, $year)  
  379.     {  
  380.         $months = self::getDaysByMonthsOfYear($year);  
  381.         $value = $months[$month - 1];  
  382.         return !$value ? 0 : $value;  
  383.     }  
  384.   
  385.   
  386.     /** 
  387.      * 获取年份的第一天 
  388.      * 
  389.      * @param int $year 年份 
  390.      * @param int $format 返回的日期格式 
  391.      * @return string 返回的日期 
  392.      */  
  393.     public static function firstDayOfYear($year = null, $format = 'Y-m-d')  
  394.     {  
  395.         $year = $year ? $year : self::format('Y');  
  396.         return self::format($format, mktime(00011, $year));  
  397.     }  
  398.   
  399.   
  400.     /** 
  401.      * 获取年份最后一天 
  402.      * 
  403.      * @param int $year 年份 
  404.      * @param int $format 返回的日期格式 
  405.      * @return string 返回的日期 
  406.      */  
  407.     public static function lastDayOfYear($year = null, $format = 'Y-m-d')  
  408.     {  
  409.         $year = $year ? $year : self::format('Y');  
  410.         return self::format($format, mktime(00010, $year + 1));  
  411.     }  
  412.   
  413.   
  414.     /** 
  415.      * 获取月份的第一天 
  416.      * 
  417.      * @param int $month 月份 
  418.      * @param int $year 年份 
  419.      * @param int $format 返回的日期格式 
  420.      * @return string 返回的日期 
  421.      */  
  422.     public static function firstDayOfMonth($month = null, $year = null, $format = 'Y-m-d')  
  423.     {  
  424.         $year = $year ? $year : self::format('Y');  
  425.         $month = $month ? $month : self::format('m');  
  426.         return self::format($format, mktime(000, $month, 1, $year));  
  427.     }  
  428.   
  429.   
  430.     /** 
  431.      * 获取月份最后一天 
  432.      * 
  433.      * @param int $month 月份 
  434.      * @param int $year 年份 
  435.      * @param int $format 返回的日期格式 
  436.      * @return string 返回的日期 
  437.      */  
  438.     public static function lastDayOfMonth($month = null, $year = null, $format = 'Y-m-d')  
  439.     {  
  440.         $year = $year ? $year : self::format('Y');  
  441.         $month = $month ? $month : self::format('m');  
  442.         return self::format($format, mktime(000, $month + 10, $year));  
  443.     }  
  444.   
  445.   
  446.     /** 
  447.      * 获取两个日期之间范围 
  448.      * 
  449.      * @param string $startDateTime 
  450.      * @param string $endDateTime 
  451.      * @param string $format 
  452.      * @return array 返回日期数组 
  453.      */  
  454.     public static function getDayRangeInBetweenDate($startDateTime, $endDateTime, $sort = false, $format = 'Y-m-d')  
  455.     {  
  456.         $startDateTime = self::getTimeStamp($startDateTime);  
  457.         $endDateTime = self::getTimeStamp($endDateTime);  
  458.         $num = ($endDateTime - $startDateTime) / 86400;  
  459.         $dateArr = array();  
  460.         for ($i = 0; $i <= $num; $i++) {  
  461.             $dateArr[] = self::format($format, $startDateTime + 86400 * $i);  
  462.         }  
  463.         return $sort ? array_reverse($dateArr) : $dateArr;  
  464.     }  
  465.   
  466. }  
  467.   
  468. if (!function_exists('bcdiv')) {  
  469.     function bcdiv($first, $second, $scale = 0)  
  470.     {  
  471.         $res = $first / $second;  
  472.         return round($res, $scale);  
  473.     }  
  474. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值