网站地图    收藏   

主页 > php专栏 > php日期 >

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

来源:自学PHP网    时间:2014-11-30 11:50 作者: 阅读:次

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

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

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

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

  1. //获取当天的星期(1-7) 
  2. function GetWeek($times) 
  3. { 
  4. $res = date('w', strtotime($times)); 
  5. if($res==0) 
  6. $res=7; 
  7. return $res; 
  8. } 
  9. //获取当天时间 
  10. function GetTime($times) 
  11. { 
  12. $res = date('H:i', strtotime($times)); 
  13. return $res; 
  14. } 
  15. //获取现在过几月的的时间 
  16. function GetMonth($Month,$type='l') 
  17. { 
  18. if(!strcmp($type,'b')) 
  19. $res=date("Y-m-d H:i:s",strtotime("-$Month months")); 
  20. if(!strcmp($type,'l')) 
  21. $res=date("Y-m-d H:i:s",strtotime("+$Month months")); 
  22. return $res; 
  23. } 
  24. //获取当前时间 
  25. function GetCurrentDateTime() 
  26. { 
  27. $res=date("Y-m-d H:i:s",time()); 
  28. return $res; 
  29. } 
  30. //获取当前时间隔几小时之前或之后的时间 
  31. function GetDiffHours($hours,$type='l') 
  32. { 
  33. if(!strcmp($type,'b')) 
  34. $res=date("Y-m-d H:i:s",strtotime("-$hours hour")); 
  35. if(!strcmp($type,'l')) 
  36. $res=date("Y-m-d H:i:s",strtotime("+$hours hour")); 
  37. return $res;  
  38. } 
  39. //间隔几分钟之前或之后的时间 
  40. function GetDiffMinute($Minute,$type='l') 
  41. { 
  42. if(!strcmp($type,'b')) 
  43. $res=date("Y-m-d H:i:s",strtotime("-$Minute minute")); 
  44. if(!strcmp($type,'l')) 
  45. $res=date("Y-m-d H:i:s",strtotime("+$Minute minute")); 
  46. return $res;  
  47. } 
  48. //间隔几秒之前或之后的时间 
  49. function GetDiffSec($sec,$type='l') 
  50. { 
  51. if(!strcmp($type,'b')) 
  52. $res=date("Y-m-d H:i:s",strtotime("-$sec second")); 
  53. if(!strcmp($type,'l')) 
  54. $res=date("Y-m-d H:i:s",strtotime("+$sec second")); 
  55. return $res;  
  56. } 
  57. //间隔几个星期之前或之后的时间 
  58. function GetDiffWeek($Week,$type='l') 
  59. { 
  60. if(!strcmp($type,'b')) 
  61. $res=date("Y-m-d H:i:s",strtotime("-$Week week")); 
  62. if(!strcmp($type,'l')) 
  63. $res=date("Y-m-d H:i:s",strtotime("+$Week week")); 
  64. return $res;  
  65. } 
  66. // 间隔几天之间的时间 
  67. function GetDiffDays($days,$type='l') 
  68. { 
  69. if(!strcmp($type,'b')) 
  70. $res=date("Y-m-d H:i:s",strtotime("-$days day")); 
  71. if(!strcmp($type,'l')) 
  72. $res=date("Y-m-d H:i:s",strtotime("+$days day")); 
  73. return $res;  
  74. } 
  75. //间隔几年之前或之后的时间 
  76. function GetDiffYears($year,$type='l') 
  77. { 
  78. if(!strcmp($type,'b')) 
  79. $res=date("Y-m-d H:i:s",strtotime("-$year year")); 
  80. if(!strcmp($type,'l')) 
  81. $res=date("Y-m-d H:i:s",strtotime("+$year year")); 
  82. return $res;  
  83. } 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论