php解决2038年后时间转化时间戳以及时间搓转化时间的问题,超级详细

首先编写两个函数如下:

/**
 * # +========================================================================
 * # | - @NodeAnotation(title="时间戳转化时间")
 * # | - @author     cq <just_leaf@foxmail.com> 
 * # | - @copyright zmtek 2021-08-09
 * # +------------------------------------------------------------------------
 * # | - 1.此函数相当于date(),用法一样
 * # +========================================================================
 */
function systemdate($curformat, $utc_value) {
    
    while(1) {
        if($utc_value > 2147483647) {
            
            if(@date('Y', $utc_value) < 2038) {
                $mydate2 = new DateTime('@'.$utc_value);
                $string = $mydate2->format($curformat);
                break;
            }
        }

        $string = date($curformat, $utc_value);
        break;
    }

    return $string;
}

/**
 * # +========================================================================
 * # | - @NodeAnotation(title="时间转化时间戳")
 * # | - @author     cq <just_leaf@foxmail.com> 
 * # | - @copyright zmtek 2021-08-09
 * # +------------------------------------------------------------------------
 * # | - 1.此函数相当于strtotime(),用法一样
 * # +========================================================================
 */
function systemstrtotime($str_time){
    
    $result = strtotime($str_time);
    if(empty($result)){
        $date = new DateTime($str_time);
        $result = $date->format('U');
    }
    return $result;
}

然后开始调用就可以了

# 时间搓转换日期
echo systemdate('Y-m-d',4782079738);
# 日期转换时间搓
echo systemstrtotime('2100-01-01');

 

posted @ 2021-08-09 12:03  正义的棒棒糖  阅读(231)  评论(0编辑  收藏  举报