资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用PHP怎么输出时间差-创新互联

使用PHP怎么输出时间差?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。成都创新互联专业提供成都网站设计、成都网站建设,网页设计,网站制作(企业站、成都响应式网站建设公司、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!

 代码如下:



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



在学习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;    
}


看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


网站栏目:使用PHP怎么输出时间差-创新互联
链接分享:http://cdkjz.cn/article/djigee.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220