当前位置:Gxlcms > PHP教程 > php输出时间差的函数代码实例

php输出时间差的函数代码实例

时间:2021-07-01 10:21:17 帮助过:16人阅读

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

PHP输出时间差函数

代码如下:

  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. ?>

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

代码如下:

  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 输出时间差的函数代码实例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行