当前位置:  编程技术>php
本页文章导读:
    ▪php时间日期工具类的实现代码      php检测日期与时间的工具类,分享给大家。 注意:如果系统没有设置时区,则获得的结果是UTC时间,与我们的时间相差了8个小时。 代码:   代码示例: <?php /** * 功能:php时间与日期工.........
    ▪分享一个判断干支、生肖和星座的php函数      php实现判断干支、生肖与星座的自定义函数。 代码:   代码示例: <?php  /**  * 判断干支、生肖和星座  * @param string $type 返回类型: array.  * @param date $birth = 时间戳,其它时间写法  *.........
    ▪PHP读取配置文件的类 php读取ini、yaml、xml配置文件信息      php读取配置文件的类,可以读取ini、yaml、xml类型的配置文件信息。 代码:   代码示例: <?php /** * 功能:读取配置文件 * 编辑:www. * 最后修改:2013/10/11 */ class Settings {      var $_sett.........

[1]php时间日期工具类的实现代码
    来源: 互联网  发布时间: 2013-12-24

php检测日期与时间的工具类,分享给大家。
注意:如果系统没有设置时区,则获得的结果是UTC时间,与我们的时间相差了8个小时。

代码:
 

代码示例:
<?php
/**
* 功能:php时间与日期工具类
* 编辑:www.
*/
DateTimeUtils::addDate('2013-12-01',1,'y'); 
DateTimeUtils::getWeekDay('2013/10/01','/'); 
DateTimeUtils::isLeapYear('2013'); 
DateTimeUtils::timeFromNow(strtotime("2013-10-26 14:15:13")); 
class DateTimeUtils { 
    /**
     * Checks for leap year, returns true if it is. No 2-digit year check. Also
     * handles julian calendar correctly.
     * @param integer $year year to check
     * @return boolean true if is leap year
     */ 
    public static function isLeapYear($year) 
    { 
        $year = self::digitCheck($year); 
        if ($year % 4 != 0) 
        return false; 
 
        if ($year % 400 == 0) 
        return true; 
        // if gregorian calendar (>1582), century not-divisible by 400 is not leap 
        else if ($year > 1582 && $year % 100 == 0) 
        return false; 
        return true; 
    } 
 
    /**
     * Fix 2-digit years. Works for any century.
     * Assumes that if 2-digit is more than 30 years in future, then previous century.
     * @param integer $y year
     * @return integer change two digit year into multiple digits
     */ 
    protected static function digitCheck($y) 
    { 
        if ($y < 100){ 
            $yr = (integer) date("Y"); 
            $century = (integer) ($yr /100); 
 
            if ($yr%100 > 50) { 
                $c1 = $century + 1; 
                $c0 = $century; 
            } else { 
                $c1 = $century; 
                $c0 = $century - 1; 
            } 
            $c1 *= 100; 
            // if 2-digit year is less than 30 years in future, set it to this century 
            // otherwise if more than 30 years in future, then we set 2-digit year to the prev century. 
            if (($y + $c1) < $yr+30) $y = $y + $c1; 
            else $y = $y + $c0*100; 
        } 
        return $y; 
    } 
 
    /**
     * Returns 4-digit representation of the year.
     * @param integer $y year
     * @return integer 4-digit representation of the year
     */ 
    public static function get4DigitYear($y) 
    { 
        return self::digitCheck($y); 
    } 
    /**
     * Checks to see if the year, month, day are valid combination.
     * @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); 
    } 
 
    public static function checkDate($date, $separator = "-") { //检查日期是否合法日期 
        $dateArr = explode() ($separator, $date); 
        return self::isValidDate ($dateArr[0], $dateArr[1], $dateArr[2]); 
    } 
    /**
     * Checks to see if the hour, minute and second are valid.
     * @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.
     * @since 1.0.5
     */ 
    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; 
    } 
 
    public static function checkTime($time, $separator = ":") { //检查时间是否合法时间 
        $timeArr = explode($separator, $time); 
        return self::isValidTime($timeArr[0], $timeArr[1],$timeArr[2]); 
    } 
 
    public static function addDate($date, $int, $unit = "d") { //日期的增加 
        $value = array('y'=>'', 'm'=>'', 'd'=>''); 
        $dateArr = explode ( "-", $date); 
        if(array_key_exists($unit, $value)){ 
            $value[$unit] = $int; 
        }else{ 
            return false; 
        } 
        return date ("Y-m-d", mktime (0, 0, 0, $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] +$value['y'])); 
    } 
 
    public static function addDateTime($date, $int, $unit = "d") { //日期的增加 
        $value = array('y'=>'', 'm'=>'', 'd'=>'', 'h'=>'', 'i'=>''); 
        $dateArr = preg_split ( "/-|\s|:/", $date); 
        if(array_key_exists($unit, $value)){ 
            $value[$unit] = $int; 
        }else{ 
            return false; 
        } 
        return date ("Y-m-d H:i:s", mktime($dateArr[3]+ $value['h'], $dateArr[4]+ $value['i'], $dateArr[5], $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] + $value['y'])); 
    } 
 
    public static function addDayTimestamp($ntime, $aday) { //取当前时间后几天,天数增加单位为1 
        $dayst = 3600 * 24; 
        $oktime = $ntime + ($aday * $dayst); 
        return $oktime; 
    } 
 
    public static function dateDiff($begin, $end, $unit = "d") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天 
        $diff = strtotime($end) - strtotime($begin); 
        switch($unit) 
        { 
            case "y": $retval = bcdiv($diff, (60 * 60 * 24 * 365)); break; 
            case "m": $retval = bcdiv($diff, (60 * 60 * 24 * 30)); break; 
            case "w": $retval = bcdiv($diff, (60 * 60 * 24 * 7)); break; 
            case "d": $retval = bcdiv($diff, (60 * 60 * 24)); break; 
            case "h": $retval = bcdiv($diff, (60 * 60)); break; 
            case "i": $retval = bcdiv($diff, 60); break; 
            case "s": $retval = $diff; break; 
        } 
        return $retval; 
    } 
 
    public static function getWeekDay($date, $separator = "-") { //计算出给出的日期是星期几 
        $dateArr = explode ($separator, $date); 
        return date ("w", mktime ( 0, 0, 0, $dateArr[1], $dateArr[2], $dateArr[0])); 
    } 
 
    public static function timeFromNow($dateline) { //让日期显示为:XX天XX年以前 
        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('Y-m-d', $dateline); 
        } 
    } 
 
    public static function transDateToChs($date) { 
        if (empty ($date)) return '今日'; 
        date_default_timezone_set('PRC'); 
        $dates = date ('Y年m月d日', strtotime ($date)); 
        return $dates; 
    } 
 
    // 08/31/2004 => 2004-08-31 
    public static function TransDateUI($datestr, $type = 'Y-m-d') { 
        if ($datestr == Null) 
        return Null; 
        $target = $datestr; 
        $arr_date = preg_split ( "/\//", $target); 
        $monthstr = $arr_date[0]; 
        $daystr = $arr_date[1]; 
        $yearstr = $arr_date[2]; 
        $result = date ($type, mktime (0, 0, 0, $monthstr, $daystr, $yearstr)); 
        return $result; 
    } 
 
    // 12/20/2004 10:55 AM => 2004-12-20 10:55:00 
    public static function TransDateTimeUI($datestr, $type = 'Y-m-d H:i:s') { 
        if ($datestr == Null) 
        return Null;
        $target = $datestr;
        $arr_date = preg_split ( "/\/|\s|:/", $target);
        $monthstr = $arr_date[0];
        $daystr = $arr_date[1];
        $yearstr = $arr_date[2];
        $hourstr = $arr_date[3];
        $minutesstr = $arr_date[4];
        $result = date ($type, mktime ($hourstr, $minutesstr, 0, $monthstr, $daystr, $yearstr));
        return $result;
    }
}
?>

    
[2]分享一个判断干支、生肖和星座的php函数
    来源: 互联网  发布时间: 2013-12-24

php实现判断干支、生肖与星座的自定义函数。
代码:
 

代码示例:
<?php 
/**
 * 判断干支、生肖和星座
 * @param string $type 返回类型: array.
 * @param date $birth = 时间戳,其它时间写法
 * @编辑:www.
//示例
$arr = birthext ( '474768000' ); //时间戳
print_r ( $arr );
$arr = birthext ( '1985-01-17' );
print_r ( $arr );
$arr = birthext ( '19850117' );
print_r ( $arr );
 */ 
function birthext($birth) { 
    if (strstr ( $birth, '-' ) === false && strlen ( $birth ) !== 8) 
        $birth = date ( "Y-m-d", $birth ); 
    if (strlen ( $birth ) === 8) { 
        if (eregi ( '([0-9]{4})([0-9]{2})([0-9]{2})$', $birth, $bir )) 
            $birth = "{$bir[1]}-{$bir[2]}-{$bir[3]}"; 
    }
     
    if (strlen ( $birth ) < 8) 
        return false; 
     
    $tmpstr = explode() ( '-', $birth ); 
     
    if (count ( $tmpstr ) !== 3) 
        return false; 
     
    $y = ( int ) $tmpstr [0]; 
    $m = ( int ) $tmpstr [1]; 
    $d = ( int ) $tmpstr [2]; 
    $result = array (); 
    $xzdict = array ('摩羯', '宝瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手' ); 
    $zone = array (1222, 122, 222, 321, 421, 522, 622, 722, 822, 922, 1022, 1122, 1222 ); 
    if ((100 * $m + $d) >= $zone [0] || (100 * $m + $d) < $zone [1]) { 
        $i = 0; 
    } else { 
        for($i = 1; $i < 12; $i ++) { 
            if ((100 * $m + $d) >= $zone [$i] && (100 * $m + $d) < $zone [$i + 1]) 
                break; 
        } 
    }
    $result ['xz'] = $xzdict [$i] . '座'; 
     
    $gzdict = array (array ('甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸' ), array ('子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥' ) ); 
    $i = $y - 1900 + 36; 
    $result ['gz'] = $gzdict [0] [($i % 10)] . $gzdict [1] [($i % 12)]; 
     
    $sxdict = array ('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪' ); 
    $result ['sx'] = $sxdict [(($y - 4) % 12)]; 
    return $result;

?>

    
[3]PHP读取配置文件的类 php读取ini、yaml、xml配置文件信息
    来源: 互联网  发布时间: 2013-12-24

php读取配置文件的类,可以读取ini、yaml、xml类型的配置文件信息。

代码:
 

代码示例:
<?php
/**
* 功能:读取配置文件
* 编辑:www.
* 最后修改:2013/10/11
*/
class Settings { 
    var $_settings = array(); 
 
    function get($var) { 
        $var = explode()('.', $var); 
        $result = $this->_settings; 
        foreach ($var as $key) { 
            if (!isset() ($result [$key])) { 
                return false; 
            } 
            $result = $result [$key]; 
        } 
        return $result; 
    } 
 
    function load() { 
        trigger_error('Not yet implemented', E_USER_ERROR); 
    } 

 
class Settings_PHP extends Settings { 
    function load($file) { 
        if (file_exists($file) == false) { 
            return false; 
        } 
 
        // Include file 
        include ($file); 
        unset ($file); 
 
        // Get declared variables 
        $vars = get_defined_vars(); 
 
        // Add to settings array 
        foreach ($vars as $key => $val) { 
            if ($key == 'this') 
                continue; 
            $this->_settings [$key] = $val; 
        } 
 
    } 

 
class Settings_INI extends Settings { 
    function load($file) { 
        if (file_exists($file) == false) { 
            return false; 
        } 
        $this->_settings = parse_ini_file($file, true); 
    } 

 
class Settings_YAML extends Settings { 
    function load($file) { 
        if (file_exists($file) == false) { 
            return false; 
        } 
 
        include ('spyc.php'); 
        $this->_settings = Spyc::YAMLLoad($file); 
    } 

 
class Settings_XML extends Settings { 
    function load($file) { 
        if (file_exists($file) == false) { 
            return false; 
        } 
 
        include ('xmllib.php'); 
        $xml = file_get_contents($file); 
        $data = XML_unserialize($xml); 
 
        $this->_settings = $data ['settings']; 
    } 
}
?>

    
最新技术文章:
▪PHP函数microtime()时间戳的定义与用法
▪PHP单一入口之apache配置内容
▪PHP数组排序方法总结(收藏)
▪php数组排序方法大全(脚本学堂整理奉献)
▪php数组排序的几个函数(附实例)
▪php二维数组排序(实例)
▪php根据键值对二维数组排序的小例子
▪php验证码(附截图)
▪php数组长度的获取方法(三个实例)
▪php获取数组长度的方法举例
▪判断php数组维度(php数组长度)的方法
▪php获取图片的exif信息的示例代码
▪PHP 数组key长度对性能的影响实例分析
▪php函数指定默认值的方法示例
▪php提交表单到当前页面、提交表单后页面重定...
▪php四舍五入的三种实现方法
▪php获得数组长度(元素个数)的方法
▪php日期函数的简单示例代码
▪php数学函数的简单示例代码
▪php字符串函数的简单示例代码
▪php文件下载代码(多浏览器兼容、支持中文文...
▪php实现文件下载、支持中文文件名的示例代码...
▪php文件下载(防止中文文件名乱码)的示例代码
▪解决PHP文件下载时中文文件名乱码的问题
▪php数组去重(一维、二维数组去重)的简单示例
▪php小数点后取两位的三种实现方法
▪php Redis 队列服务的简单示例
▪PHP导出excel时数字变为科学计数的解决方法
▪PHP数组根据值获取Key的简单示例
▪php数组去重的函数代码示例
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3