php专区

 首页 > php专区 > PHP高级 > 日期 > PHP日期时间加减程序代码详解 - php日期

PHP日期时间加减程序代码详解 - php日期

分享到:
【字体:
导读:
          今天我们来看在php中计算两个时间之间的时间差 下面我们直接使用了data,strtotime与time三个函数就实现了 有需要的朋友可参考,今天要讲的这...

PHP日期时间加减程序代码详解

今天我们来看在php中计算两个时间之间的时间差.下面我们直接使用了data,strtotime与time三个函数就实现了.有需要的朋友可参考,今天要讲的这个例子,需求是这样的,得知某个日期时间,如:2012-04-25 10:10:00,我要在这个日期时间的基础上加上5个月并返回处理后的日期,结果:2012-04-25 10:10:00 加5个月等于 2012-09-25 10:10:00

结合PHP函数date()与strtotime()两个函数来实现大致也是这个意思,代码如下:

  1. /**  
  2.  * PHP里的日期加减方法  
  3.  * 琼台老屋  
  4.  */ 
  5. // 第一步,假设有一个时间  
  6. $a = '2012-04-25 10:10:00';  
  7.    
  8. // 第二步,获得这个日期的时间戳  
  9. $a_time = strtotime($a);  
  10.    
  11. // 第三步,获得加五个月后的时间戳  
  12. $b_time = strtotime('+5 Month',$a_time);  
  13.    
  14. // 第四部,把时间戳转换回日期格式  
  15. $b = date('Y-m-d H:i:s',$b_time);  
  16. echo '这是加了五个月后的日期'.$b;  
  17.    
  18. // 如果你觉得以上代码过长也可以一行搞定  
  19. $b = date('Y-m-d H:i:s',strtotime('+'.$time.' Month',strtotime($a)));  
  20. echo '这是加了五个月后的日期'.$b;  
  21. ?> 

常用的计算时间,代码如下:

  1. date_default_timezone_set('PRC'); //默认时区   
  2. echo "今天:",date("Y-m-d",time()),"
    "
    ;   
  3. echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"
    "
    ;   
  4. echo "昨天:",date("Y-m-d",strtotime("-1 day")), "
    "
    ;   
  5. echo "明天:",date("Y-m-d",strtotime("+1 day")), "
    "
    ;   
  6. echo "一周后:",date("Y-m-d",strtotime("+1 week")), "
    "
    ;   
  7. echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "
    "
    ;   
  8. echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")), "
    "
    ;   
  9. echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."
    "
    ;   
  10. echo "一个月前:".date("Y-m-d",strtotime("last month"))."
    "
    ;   
  11. echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."
    "
    ;   
  12. echo "十年后:".date("Y-m-d",strtotime("+10 year"))."
    "
    ;   
  13. ?> 
  14. //输出结果 
  15. //今天:2013-06-07 
  16. //今天:2008-06-18 
  17. //昨天:2013-06-06 
  18. //明天:2013-06-08 
  19. //一周后:2013-06-14 
  20. //一周零两天四小时两秒后:2013-06-16 18:18:29 
  21. //下个星期四:2013-06-13 
  22. //上个周一:2013-06-03 
  23. //一个月前:2013-05-07 
  24. //一个月后:2013-07-07 
  25. //十年后:2023-06-07 

这里再看一些日期加减函数,代码如下:

  1. //获取当天的星期(1-7) 
  2. function GetWeek($times
  3. $res = date('w'strtotime($times)); 
  4. if($res==0) 
  5. $res=7; 
  6. return $res
  7. //获取当天时间 
  8. function GetTime($times
  9. $res = date('H:i'strtotime($times)); 
  10. return $res
  11. //获取现在过几月的的时间 
  12. function GetMonth($Month,$type='l'
  13. if(!strcmp($type,'b')) 
  14. $res=date("Y-m-d H:i:s",strtotime("-$Month months")); 
  15. if(!strcmp($type,'l')) 
  16. $res=date("Y-m-d H:i:s",strtotime("+$Month months")); 
  17. return $res
  18. //获取当前时间 
  19. function GetCurrentDateTime() 
  20. $res=date("Y-m-d H:i:s",time()); 
  21. return $res
  22. //获取当前时间隔几小时之前或之后的时间 
  23. function GetDiffHours($hours,$type='l'
  24. if(!strcmp($type,'b')) 
  25. $res=date("Y-m-d H:i:s",strtotime("-$hours hour")); 
  26. if(!strcmp($type,'l')) 
  27. $res=date("Y-m-d H:i:s",strtotime("+$hours hour")); 
  28. return $res;  
  29. //间隔几分钟之前或之后的时间 
  30. function GetDiffMinute($Minute,$type='l'
  31. if(!strcmp($type,'b')) 
  32. $res=date("Y-m-d H:i:s",strtotime("-$Minute minute")); 
  33. if(!strcmp($type,'l')) 
  34. $res=date("Y-m-d H:i:s",strtotime("+$Minute minute")); 
  35. return $res;  
  36. //间隔几秒之前或之后的时间 
  37. function GetDiffSec($sec,$type='l'
  38. if(!strcmp($type,'b')) 
  39. $res=date("Y-m-d H:i:s",strtotime("-$sec second")); 
  40. if(!strcmp($type,'l')) 
  41. $res=date("Y-m-d H:i:s",strtotime("+$sec second")); 
  42. return $res;  
  43. //间隔几个星期之前或之后的时间 
  44. function GetDiffWeek($Week,$type='l'
  45. if(!strcmp($type,'b')) 
  46. $res=date("Y-m-d H:i:s",strtotime("-$Week week")); 
  47. if(!strcmp($type,'l')) 
  48. $res=date("Y-m-d H:i:s",strtotime("+$Week week")); 
  49. return $res;  
  50. // 间隔几天之间的时间 
  51. function GetDiffDays($days,$type='l'
  52. if(!strcmp($type,'b')) 
  53. $res=date("Y-m-d H:i:s",strtotime("-$days day")); 
  54. if(!strcmp($type,'l')) 
  55. $res=date("Y-m-d H:i:s",strtotime("+$days day")); 
  56. return $res;  
  57. //间隔几年之前或之后的时间 
  58. function GetDiffYears($year,$type='l'
  59. if(!strcmp($type,'b')) 
  60. $res=date("Y-m-d H:i:s",strtotime("-$year year")); 
  61. if(!strcmp($type,'l')) 
  62. $res=date("Y-m-d H:i:s",strtotime("+$year year")); 
  63. return $res;  
分享到:
PHP计算时间段某天是星期几,当前时间后几...
PHP计算时间段某天是星期几,当前时间后几天及日期时间差 这几个时间函数都放出来了,使用方法不用说了吧,直接看代码:  
php 获取星期几实例程序 - php日期
php 获取星期几实例程序 php为我们提供了大量的函数可以获取星期几,我们常用的就是 date()函数了,下面我来介绍获取指定日期星期几的实例。 PHP date()参数说明 这里主要用到了 w 这个参数,关于这个参数的解释是,w 表示星期中的第几天,数字表示 0(表示星期天)到6(表示星期六),有了这个一切就...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……