首页 后端开发 php教程 PHP时间类完整实例(非常实用)_PHP

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

May 28, 2016 am 11:49 AM
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

<&#63;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.'------'.$time2.'<br>';

 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='相差'.$day.'天'.$hour.'小时'.$minute.'分钟'.$second.'秒';

 }

 return $this->diffTime;

 }

 //返回 X年X月X日

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

 if($type==1){  

  $this->longDate = $this->getyear($time) . '年' . $this->getmonth($time) . '月' . $this->getday($time) . '日'

 }else{

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

 }

 return $this->longDate; 

 }

}

&#63;>

登录后复制

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

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和时间

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

CakePHP 文件上传

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

CakePHP 项目配置

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

讨论 CakePHP

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发

See all articles