Home >php教程 >php手册 >php日期处理函数(计算时间差,转换时间戳日期)

php日期处理函数(计算时间差,转换时间戳日期)

WBOY
WBOYOriginal
2016-06-13 09:54:461419browse


php教程 转换时间戳为常用的日期格式与计算时间差:默认返回类型为“分钟”
function trans_time($timestamp){
 if($timestamp  else return date("y-m-d h:i:s",$timestamp);
}

//获取ip
function get_ip() {
    if ($_server["http_x_forwarded_for"])
        $ip = $_server["http_x_forwarded_for"];
    else if ($_server["http_client_ip"])
        $ip = $_server["http_client_ip"];
    else if ($_server["remote_addr"])
        $ip = $_server["remote_addr"];
    else if (getenv("http_x_forwarded_for"))
        $ip = getenv("http_x_forwarded_for");
    else if (getenv("http_client_ip"))
        $ip = getenv("http_client_ip");
    else if (getenv("remote_addr"))
        $ip = getenv("remote_addr");
    else
        $ip = "unknown";
    return $ip;
}

//计算时间差:默认返回类型为“分钟”
//$old_time 只能是时间戳,$return_type 为 h 是小时,为 s 是秒
function timelag($old_time,$return_type='m'){
 if($old_time   echo '无效的unix时间戳';
 }else{
  switch($return_type){
   case 'h':
   $type = 3600; break;
   case 'm':
   $type = 60; break;
   case 's':
   $type = 1; break;
   case '':
   $type = 60; break;
  }
  $dif = round( (time()-$old_time)/$type ) ;
  return $dif;
 }
}

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