Home  >  Article  >  Backend Development  >  php实现兼容2038年后Unix时间戳转换函数_PHP

php实现兼容2038年后Unix时间戳转换函数_PHP

WBOY
WBOYOriginal
2016-06-01 11:06:30920browse

使用方法跟旧的函数一模一样.

代码如下:


function fun_strtotime($var1=0,$var2=0){
    if(!$var2){
        $var2 = $var1;
        $var1 = 0;
    }
    if(is_numeric($var2)){
       $var2 = '@'.$var2;
    }
    try{
        $date  =  new DateTime($var2);
        $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
    }catch(Exception $e){ $date = 0;}
    if(is_object($date)){
        if($var1)
            $date->modify($var1);
        return $date->format('U');
    }else{
        return 0;
    }
}
function fun_date($var1=0,$var2=0){
    if(!$var2){
        $var2 = $var1;
        $var1 = 'Y-m-d H:i:s';
    }
    try{
        $date  =  new DateTime('@'.$var2);
        $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
    }catch(Exception $e){ $date = 0;}
    if(is_object($date)){
        return $date->format($var1);
    }else{
        return '';
    }
}

以上就是本文所述的全部内容了,希望对大家学习php有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn