PHP 日期、时间、字节转换

1、转换字节数为其他单位(Gb、Mb等)

2、时间转换(将秒转换为时间)

/**
 * Function dataformat
 * 时间转换(将秒转换为时间)
 * @param $n INT时间
 */
function dataformat($n) {
    $hours = floor($n/3600);
    $minite    = floor($n%3600/60);
    $secend = floor($n%3600%60);
    $minite = $minite < 10 ? "0".$minite : $minite;
    $secend = $secend < 10 ? "0".$secend : $secend;
    if($n >= 3600){
        return $hours."时".$minite."分".$secend."秒";
    }else{
        return "0时".$minite."分".$secend."秒";
    }
}
/**
 * 获取秒数对应的时间
 * @param int $second
 * @return array
 */
function secondConversionArray($second = 0)
{
    $d       = floor($second / (3600 * 24));
    $h       = floor(($second % (3600 * 24)) / 3600);
    $m       = floor((($second % (3600 * 24)) % 3600) / 60);
    $s       = floor((($second % (3600 * 24)) % 3600) % 60);
    $newtime = [
        'day'    => $d,
        'hour'   => $h,
        'minute' => $m,
        'second' => $s
    ];
    return $newtime;
}

3、将秒转换成 天-时分秒

/*将秒转换成天-时 分 秒*/
function SetTime($time)
{
    if (is_numeric($time)) {
        $value = array("years" => 0, "days" => 0, "hours" => 0, "minutes" => 0, "seconds" => 0,);
        if ($time >= 31556926) {
            $value["years"] = floor($time / 31556926);
            $time = ($time % 31556926);
        }
        if ($time >= 86400) {
            $value["days"] = floor($time / 86400);
            $time = ($time % 86400);
        }
        if ($time >= 3600) {
            $value["hours"] = floor($time / 3600);
            $time = ($time % 3600);
        }
        if ($time >= 60) {
            $value["minutes"] = floor($time / 60);
            $time = ($time % 60);
        }
        $value["seconds"] = floor($time);
        //return (array) $value;
        if ($value["years"] > 0) {
            $t = $value["years"] . "年" . $value["days"] . "天" . " " . $value["hours"] . "小时" . $value["minutes"] . "分" . $value["seconds"] . "秒";
        } elseif ($value['days'] > 0) {
            $t = $value["days"] . "天" . " " . $value["hours"] . "小时" . $value["minutes"] . "分" . $value["seconds"] . "秒";
        } elseif ($value['hours'] > 0) {
            $t = $value["hours"] . "小时" . $value["minutes"] . "分" . $value["seconds"] . "秒";
        } elseif ($value['minutes'] > 0) {
            $t = $value["minutes"] . "分" . $value["seconds"] . "秒";
        } elseif ($value['seconds'] > 0) {
            $t = $value["seconds"] . "秒";
        } else {
            $t = 0;
        }
        Return $t;
        
    } else {
        return (bool)FALSE;
    }
}

4、时间转换(将时分秒转换为秒)

/**
 * Function gettime
 * 时间转换(将时分秒转换为秒)
 * @param $n INT秒
 */
public function gettime($time){
    $explodetime1 = explode("时",$time);
    $time_shi=$explodetime1[0];
    $explodetime2 =explode("分",$explodetime1[1]);
    $time_fen=$explodetime2[0];
    $explodetime3 = explode("秒",$explodetime2[1]);
    $time_miao=$explodetime3[0];
    $timeallmiao = $time_shi*3600+$time_fen*60+$time_miao;
    return $timeallmiao;
}

5、计算两个时间相差的天

6、获取文件大小并格式化

7、返回13位时间戳

/*
返回13位的时间戳
*/
function getMillisecond() { 
  list($t1, $t2) = explode(' ', microtime()); 
  return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000); 
}

8、返回与当前时间的差距,如1分钟前,2小时前,5月前等

/**
* 传入日期格式或时间戳格式时间,返回与当前时间的差距,如1分钟前,2小时前,5月前,3年前等
* @param string or int $date 分两种日期格式"2015-09-12 14:16:12"或时间戳格式"1386743303"
* @param int $type
* @return string
*/
function format_time($date = 0, $type = 1) { //$type = 1为时间戳格式,$type = 2为date时间格式
    switch ($type) {
        case 1:
            //$data时间戳格式
            $second = time()- $date;
            $minute = floor($second / 60) ? floor($second / 60) : 1; 
            if ($minute >= 60 && $minute < (60 * 24)) { 
                $hour = floor($minute / 60); 
            } elseif ($minute >= (60 * 24) && $minute < (60 * 24 * 30)) { 
                $day = floor($minute / ( 60 * 24)); 
            } elseif ($minute >= (60 * 24 * 30) && $minute < (60 * 24 * 365)) { 
                $month = floor($minute / (60 * 24 * 30));
            } elseif ($minute >= (60 * 24 * 365)) { 
                $year = floor($minute / (60 * 24 * 365)); 
            }
            break;
            case 2:
            //$date为字符串格式 2013-06-06 19:16:12
            $date = strtotime($date);
            $second = time()- $date;
            $minute = floor($second / 60) ? floor($second / 60) : 1; 
            if ($minute >= 60 && $minute < (60 * 24)) { 
                $hour = floor($minute / 60); 
            } elseif ($minute >= (60 * 24) && $minute < (60 * 24 * 30)) { 
                $day = floor($minute / ( 60 * 24)); 
            } elseif ($minute >= (60 * 24 * 30) && $minute < (60 * 24 * 365)) { 
               $mont = floor($minute / (60 * 24 * 30)); 
            } elseif ($minute >= (60 * 24 * 365)) { 
               $year = floor($minute / (60 * 24 * 365)); 
            }
            break;
            default:
            break;
    }
    if (isset($year)) {
        return $year . '年前';
    } elseif (isset($month)) {
        return $month . '月前';
    } elseif (isset($day)) {
        return $day . '天前';
    } elseif (isset($hour)) {
        return $hour . '小时前';
    } elseif (isset($minute)) {
        return $minute . '分钟前';
    }
}  
/**
 * 传入时间戳,计算距离现在的时间
 *
 * @param number $time 时间戳
 *
 * @return string     返回多少以前
 */
function word_time($time)
{
    $time = (int)substr($time, 0, 10);
    $int  = time() - $time;
    $str  = '';
    if ($int <= 2) {
        $str = sprintf('刚刚', $int);
    } elseif ($int < 60) {
        $str = sprintf('%d秒前', $int);
    } elseif ($int < 3600) {
        $str = sprintf('%d分钟前', floor($int / 60));
    } elseif ($int < 86400) {
        $str = sprintf('%d小时前', floor($int / 3600));
    } elseif ($int < 1728000) {
        $str = sprintf('%d天前', floor($int / 86400));
    } else {
        $str = date('Y-m-d H:i:s', $time);
    }

    return $str;
}

9、生日(身份证号)转年龄

10、密码加密方法

PHP 日期、时间、字节转换插图
PHP 日期、时间、字节转换-小七笔记
PHP 日期、时间、字节转换
此内容为付费阅读,请付费后查看
1积分
付费阅读
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    blank

    暂无评论内容