PHP时间类完整实例(非常实用)

这篇文章主要介绍了PHP时间类完整实例,涉及PHP针对日期、时间、星期等的获取与比较等操作技巧,非常简单实用,需要的朋友可以参考下,本文实例讲述了PHP时间类,分享给大家供大家参考,具体如下:

  1. <?php
  2. header("Content-type:text/html;Charset=utf-8");
  3. class time{
  4. private $year;//年
  5. private $month;//月
  6. private $day;//天
  7. private $hour;//小时
  8. private $minute;//分钟
  9. private $second;//秒
  10. private $microtime;//毫秒
  11. private $weekday;//星期
  12. private $longDate;//完整的时间格式
  13. private $diffTime;//两个时间的差值
  14. //返回年份 time:时间格式为时间戳 2013-3-27
  15. function getyear($time="",$type=""){
  16. if($time==""){
  17. $time=time();
  18. }
  19. if($type==1){
  20. return $this->year=date("y",$time); //返回两位的年份 13
  21. }else{
  22. return $this->year=date("Y",$time); //返回四位的年份 2013
  23. }
  24. }
  25. //返回当前时间的月份 time:时间格式为时间戳 2013-3-27
  26. function getmonth($time="",$type=""){
  27. if($time==""){
  28. $time=time();
  29. }
  30. switch($type){
  31. case 1:$this->month=date("n",$time);//返回格式 3
  32. break;
  33. case 2:$this->month=date("m",$time);//返回格式 03
  34. break;
  35. case 3:$this->month=date("M",$time);//返回格式 Mar
  36. break;
  37. case 4:$this->month=date("F",$time);//返回格式 March
  38. break;
  39. default:$this->month=date("n",$time);
  40. }
  41. return $this->month;
  42. }
  43. //返回当前时间的天数 time:时间格式为时间戳 2013-3-4
  44. function getday($time="",$type=""){
  45. if($time==""){
  46. $time=time();
  47. }
  48. if($type==1){
  49. $this->day=date("d",$time);//返回格式 04
  50. }else{
  51. $this->day=date("j",$time);//返回格式 4
  52. }
  53. return $this->day;
  54. }
  55. //返回当前时间的小时 2010-11-10 1:19:21 20:19:21
  56. function gethour($time="",$type=""){
  57. if($time==""){
  58. $time=time();
  59. }
  60. switch($type){
  61. case 1:$this->hour=date("H",$time);//格式: 1 20
  62. break;
  63. case 2:$this->hour=date("h",$time);//格式 01 08
  64. break;
  65. case 3:$this->hour=date("G",$time);//格式 1 20
  66. break;
  67. case 4:$this->hour=date("g",$time);//格式 1 8
  68. break;
  69. default :$this->hour=date("H",$time);
  70. }
  71. return $this->hour;
  72. }
  73. //返回当前时间的分钟数 1:9:18
  74. function getminute($time="",$type=""){
  75. if($time==""){
  76. $time=time();
  77. }
  78. $this->minute=date("i",$time); //格式 09
  79. return $this->minute;
  80. }
  81. //返回当前时间的秒数 20:19:01
  82. function getsecond($time="",$type=""){
  83. if($time==""){
  84. $time=time();
  85. }
  86. $this->second=date("s",$time); //格式 01
  87. return $this->second;
  88. }
  89. //返回当前时间的星期数
  90. function getweekday($time="",$type=""){
  91. if($time==""){
  92. $time=time();
  93. }
  94. if($type==1){
  95. $this->weekday=date("D",$time);//格式 Sun
  96. }else if($type==2){
  97. $this->weekday=date("l",$time); //格式 Sunday
  98. }else{
  99. $this->weekday=date("w",$time);//格式 数字表示 0--6
  100. }
  101. return $this->weekday;
  102. }
  103. //比较两个时间的大小 格式 2013-3-4 8:4:3
  104. function compare($time1,$time2){
  105. $time1=strtotime($time1);
  106. $time2=strtotime($time2);
  107. if($time1>=$time2){ //第一个时间大于等于第二个时间 返回1 否则返回0
  108. return 1;
  109. }else{
  110. return -1;
  111. }
  112. }
  113. //比较两个时间的差值
  114. function diffdate($time1="",$time2=""){
  115. //echo $time1.'------'.$time2.'<br>';
  116. if($time1==""){
  117. $time1=date("Y-m-d H:i:s");
  118. }
  119. if($time2==""){
  120. $time2=date("Y-m-d H:i:s");
  121. }
  122. $date1=strtotime($time1);
  123. $date2=strtotime($time2);
  124. if($date1>$date2){
  125. $diff=$date1-$date2;
  126. }else{
  127. $diff=$date2-$date1;
  128. }
  129. if($diff>=0){
  130. $day=floor($diff/86400);
  131. $hour=floor(($diff%86400)/3600);
  132. $minute=floor(($diff%3600)/60);
  133. $second=floor(($diff%60));
  134. $this->diffTime='相差'.$day.'天'.$hour.'小时'.$minute.'分钟'.$second.'秒';
  135. }
  136. return $this->diffTime;
  137. }
  138. //返回 X年X月X日
  139. function buildDate($time="",$type=""){
  140. if($type==1){
  141. $this->longDate = $this->getyear($time) . '年' . $this->getmonth($time) . '月' . $this->getday($time) . '日';
  142. }else{
  143. $this->longDate = $this->getyear($time) . '年' . $this->getmonth($time) . '月' . $this->getday($time) . '日'.$this->gethour($time).':'.$this->getminute($time).':'.$this->getsecond($time);
  144. }
  145. return $this->longDate;
  146. }
  147. }
  148. ?>

希望本文所述对大家PHP程序设计有所帮助。