首頁 > 後端開發 > php教程 > PHP時間類別完整實例

PHP時間類別完整實例

不言
發布: 2023-03-25 16:20:02
原創
1625 人瀏覽過

這篇文章主要介紹了PHP時間類完整實例,涉及PHP針對日期、時間、星期等的獲取與比較等操作技巧,非常簡單實用,需要的朋友可以參考下

本文實例講述了PHP時間類。分享給大家參考,如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

<?php

header("Content-type:text/html;Charset=utf-8");

class time{

 private $year;//年

 private $month;//月

 private $day;//天

 private $hour;//小时

 private $minute;//分钟

 private $second;//秒

 private $microtime;//毫秒

 private $weekday;//星期

 private $longDate;//完整的时间格式

 private $diffTime;//两个时间的差值

 //返回年份 time:时间格式为时间戳  2013-3-27

 function getyear($time="",$type=""){

 if($time==""){

  $time=time();

 }

 if($type==1){

  return $this->year=date("y",$time); //返回两位的年份 13

 }else{

  return $this->year=date("Y",$time); //返回四位的年份 2013

 }

 }

 //返回当前时间的月份 time:时间格式为时间戳 2013-3-27

 function getmonth($time="",$type=""){

 if($time==""){

  $time=time();

 }

 switch($type){

  case 1:$this->month=date("n",$time);//返回格式 3

   break;

  case 2:$this->month=date("m",$time);//返回格式 03

   break;

  case 3:$this->month=date("M",$time);//返回格式 Mar

   break;

  case 4:$this->month=date("F",$time);//返回格式 March

   break;

  default:$this->month=date("n",$time);

 }

 return $this->month;

 }

 //返回当前时间的天数 time:时间格式为时间戳 2013-3-4

 function getday($time="",$type=""){

 if($time==""){

  $time=time();

 }

 if($type==1){

  $this->day=date("d",$time);//返回格式 04

 }else{

  $this->day=date("j",$time);//返回格式 4

 }

 return $this->day;

 }

 //返回当前时间的小时  2010-11-10 1:19:21 20:19:21

 function gethour($time="",$type=""){

 if($time==""){

  $time=time();

 }

 switch($type){

  case 1:$this->hour=date("H",$time);//格式: 1 20

   break;

  case 2:$this->hour=date("h",$time);//格式  01 08

   break;

  case 3:$this->hour=date("G",$time);//格式  1 20

   break;

  case 4:$this->hour=date("g",$time);//格式  1 8

   break;

  default :$this->hour=date("H",$time);

 }

 return $this->hour;

 }

 //返回当前时间的分钟数 1:9:18 

 function getminute($time="",$type=""){

 if($time==""){

  $time=time();

 }

 $this->minute=date("i",$time); //格式  09

 return $this->minute;

 }

 //返回当前时间的秒数  20:19:01

 function getsecond($time="",$type=""){

 if($time==""){

  $time=time();

 }

 $this->second=date("s",$time); //格式  01

 return $this->second;

 }

 //返回当前时间的星期数

 function getweekday($time="",$type=""){

 if($time==""){

  $time=time();

 }

 if($type==1){

  $this->weekday=date("D",$time);//格式  Sun

 }else if($type==2){

  $this->weekday=date("l",$time); //格式 Sunday

 }else{

  $this->weekday=date("w",$time);//格式 数字表示 0--6

 }

 return $this->weekday;

 }

 //比较两个时间的大小 格式 2013-3-4 8:4:3 

 function compare($time1,$time2){

 $time1=strtotime($time1);

 $time2=strtotime($time2);

 if($time1>=$time2){  //第一个时间大于等于第二个时间 返回1 否则返回0

  return 1;

 }else{

  return -1;

 }

 }

 //比较两个时间的差值

 function diffdate($time1="",$time2=""){

 //echo $time1.&#39;------&#39;.$time2.&#39;<br>&#39;;

 if($time1==""){

  $time1=date("Y-m-d H:i:s");

 }

 if($time2==""){

  $time2=date("Y-m-d H:i:s");

 }

 $date1=strtotime($time1);

 $date2=strtotime($time2);

 if($date1>$date2){

  $diff=$date1-$date2;

 }else{

  $diff=$date2-$date1;

 }

 if($diff>=0){

  $day=floor($diff/86400);

  $hour=floor(($diff%86400)/3600);

  $minute=floor(($diff%3600)/60);

  $second=floor(($diff%60));

  $this->diffTime=&#39;相差&#39;.$day.&#39;天&#39;.$hour.&#39;小时&#39;.$minute.&#39;分钟&#39;.$second.&#39;秒&#39;;

 }

 return $this->diffTime;

 }

 //返回 X年X月X日

 function buildDate($time="",$type=""){

 if($type==1){  

  $this->longDate = $this->getyear($time) . &#39;年&#39; . $this->getmonth($time) . &#39;月&#39; . $this->getday($time) . &#39;日&#39;; 

 }else{

  $this->longDate = $this->getyear($time) . &#39;年&#39; . $this->getmonth($time) . &#39;月&#39; . $this->getday($time) . &#39;日&#39;.$this->gethour($time).&#39;:&#39;.$this->getminute($time).&#39;:&#39;.$this->getsecond($time); 

 }

 return $this->longDate; 

 }

}

?>

登入後複製

相關推薦:

PHP時間戳函數詳解

以上是PHP時間類別完整實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板