当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP输出时间差函数代码

PHP输出时间差函数代码

2019年04月14日  | 移动技术网IT编程  | 我要评论

php输出时间差函数

复制代码 代码如下:

<?php 
date_default_timezone_set('prc'); //默认时区 
echo "今天:",date("y-m-d",time()),"<br>"; 
echo "今天:",date("y-m-d",strtotime("18 june 2008")),"<br>"; 
echo "昨天:",date("y-m-d",strtotime("-1 day")), "<br>"; 
echo "明天:",date("y-m-d",strtotime("+1 day")), "<br>"; 
echo "一周后:",date("y-m-d",strtotime("+1 week")), "<br>"; 
echo "一周零两天四小时两秒后:",date("y-m-d g:h:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>"; 
echo "下个星期四:",date("y-m-d",strtotime("next thursday")), "<br>"; 
echo "上个周一:".date("y-m-d",strtotime("last monday"))."<br>"; 
echo "一个月前:".date("y-m-d",strtotime("last month"))."<br>"; 
echo "一个月后:".date("y-m-d",strtotime("+1 month"))."<br>"; 
echo "十年后:".date("y-m-d",strtotime("+10 year"))."<br>"; 
?>

在学习php 的时候,经常会用到获取现在之前或之后,某个时间段的日期。现在已经进行收集,大家同时也可以进行扩展丰富
复制代码 代码如下:

//获取当天的星期(1-7)
function getweek($times)
{
    $res = date('w', strtotime($times));
    if($res==0)
       $res=7;
    return $res;
}
//获取当天时间
function gettime($times)
{
    $res = date('h:i', strtotime($times));
    return $res;
}
//获取现在过几月的的时间
function getmonth($month,$type='l')
{
    if(!strcmp($type,'b'))
      $res=date("y-m-d h:i:s",strtotime("-$month months"));
    if(!strcmp($type,'l'))
      $res=date("y-m-d h:i:s",strtotime("+$month months"));
    return $res;
}
//获取当前时间
function getcurrentdatetime()
{
    $res=date("y-m-d h:i:s",time());
    return $res;
}
//获取当前时间隔几小时之前或之后的时间
function getdiffhours($hours,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("y-m-d h:i:s",strtotime("-$hours hour"));
  if(!strcmp($type,'l'))
     $res=date("y-m-d h:i:s",strtotime("+$hours hour"));
  return $res;    
}
//间隔几分钟之前或之后的时间
function getdiffminute($minute,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("y-m-d h:i:s",strtotime("-$minute minute"));
  if(!strcmp($type,'l'))
     $res=date("y-m-d h:i:s",strtotime("+$minute minute"));
  return $res;    
}
//间隔几秒之前或之后的时间
function getdiffsec($sec,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("y-m-d h:i:s",strtotime("-$sec second"));
  if(!strcmp($type,'l'))
     $res=date("y-m-d h:i:s",strtotime("+$sec second"));
  return $res;    
}

//间隔几个星期之前或之后的时间
function getdiffweek($week,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("y-m-d h:i:s",strtotime("-$week week"));
  if(!strcmp($type,'l'))
     $res=date("y-m-d h:i:s",strtotime("+$week week"));
  return $res;    
}
// 间隔几天之间的时间
function getdiffdays($days,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("y-m-d h:i:s",strtotime("-$days day"));
  if(!strcmp($type,'l'))
     $res=date("y-m-d h:i:s",strtotime("+$days day"));
  return $res;    
}
//间隔几年之前或之后的时间
function getdiffyears($year,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("y-m-d h:i:s",strtotime("-$year year"));
  if(!strcmp($type,'l'))
     $res=date("y-m-d h:i:s",strtotime("+$year year"));
  return $res;    
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网