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

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

WBOY
Release: 2016-06-01 11:06:30
Original
980 people have browsed it

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

代码如下:


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有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template