PHP输出时间差函数代码

 更新时间:2013年01月28日 12:18:40   作者:  
在学习PHP 的时候,经常会用到获取现在之前或之后,某个时间段的日期。现在已经进行收集,大家同时也可以进行扩展丰富

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;    
}

相关文章

  • 修改Laravel5.3中的路由文件与路径

    修改Laravel5.3中的路由文件与路径

    本文先是回顾了Laravel5.2中路由的修改,然后给大家用实例代码介绍了如何修改Laravel5.3中的路由,有需要的小伙伴们可以参考学习。
    2016-08-08
  • php中json_encode中文编码问题分析

    php中json_encode中文编码问题分析

    众所周知使用json_encode可以方便快捷地将对象进行json编码,但是如果对象的属性中存在着中文,问题也就随之而来了。json_encode会将中文转换为unicode编码
    2011-09-09
  • 解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别

    解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别

    本篇文章是对mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php 魔术方法使用说明

    php 魔术方法使用说明

    一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!!
    2009-10-10
  • PHP使用JpGraph绘制折线图操作示例【附源码下载】

    PHP使用JpGraph绘制折线图操作示例【附源码下载】

    这篇文章主要介绍了PHP使用JpGraph绘制折线图操作,结合实例形式分析了php使用JpGraph的相关操作技巧与注意事项,并附带源码供读者下载参考,需要的朋友可以参考下
    2019-10-10
  • php-accelerator网站加速PHP缓冲的方法

    php-accelerator网站加速PHP缓冲的方法

    我们知道 Zend 有免费的优化引擎针对 PHP 而作,但是 FreeLAMP 这次采用的是一个叫做 PHP Accelerator 的缓冲产品。
    2008-07-07
  • windows下配置apache+php+mysql时出现问题的处理方法

    windows下配置apache+php+mysql时出现问题的处理方法

    windows下配置apache+php+mysql应该是每个phper必须掌握的基础技能了,这也是熟悉php的一个过程,小编当年自己配环境的时候也遇到过这样那样的问题,现在把当时记录的几个问题的处理方法分享给大家
    2014-06-06
  • PHP8新特性之JIT案例讲解

    PHP8新特性之JIT案例讲解

    这篇文章主要介绍了PHP8新特性之JIT案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)

    PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)

    UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式
    2012-07-07
  • PHP常见数组函数用法小结

    PHP常见数组函数用法小结

    这篇文章主要介绍了PHP常见数组函数用法,结合实例形式分析了array_merge、array_slice及array_map函数的使用技巧,需要的朋友可以参考下
    2016-03-03

最新评论