自学php找工作【二】 PHP计算时间加一天
最近几天在做一个项目,主要是将SQLserver数据到MySQL数据库,一个url跑一次 同步一次昨天的数据,由于很多数据需要同步,所以做了一个操作界面的,一个单纯跑url的在其中涉及到了对于时间的计算!当我写完这个程序的时候,我回头看我写的计算时间的代码。有些都有点儿懵了!。。。在这里记录下来方便以后回忆,也方便别人使用!
代码可能会臃肿!新人一枚!欢迎指正!拒绝骂街哦!
先简单说一下代码,其中主要涉及到计算润年 平年,计算28天 31天 30天月份 等,主要就是判断!代码中有注释,大家看一下,希望大家多多指正缺点!
1/**一年中的31天的月份
2 * @var array
3 */
4 private $month_31 = array(1,3,5,7,8,10,12);
5
6 /**一年中30天的月份
7 * @var array
8 */
9 private $month_30 = array(4,6,9,11);
10
11 /**查询开始的时间戳
12 * @var
13 */
14 private $startTimeStamp;
15
16 /**查询结束的时间戳
17 * @var
18 */
19 private $endTimeStamp;
20
21 /-----------------------------------------------计算年月----------------------------------------------------------
22
23 /**计算年份是否是闰年,如果是闰年 2月份是29天平年是28天, 每调用一次这个函数,天数增 加1天
24 * @return string时间戳,是经过计算的,前加 '00'后加'000'
25 */
26 private function computeTime() {
27 if (($this->year % 4 == 0 && $this->year % 100 != 0) || ($this->year % 400 == 0)) {
28 $this->computeTimeDate(29);
29 } else {
30 $this->computeTimeDate(28);
31 }
32 }
33
34 /**根据月份是多少天,计算日期时间,
35 * @param $Feb2月的天数
36 */
37 private function computeTimeDate($Feb) {
38
39 if ($this->month == 2) {
40
41 if($this->date >= 1 && $this->date <= $Feb) {
42
43 $this->date = $this->date + $this->syncNumDate;
44 //如果加默认天数大于当前月份天数,就计算月份
45 if( $this->date + $this->syncNumDate > $Feb ) {
46 $this->computeDateMonth();
47 }
48
49 } else if($this->date > $Feb) {
50
51 $D_value = $this->syncNumDate - ($Feb - $this->date);
52 if( $D_value != 0 ) {
53 $this->computeDateMonth($D_value);
54 } else {
55 $this->computeDateMonth();
56 }
57
58 } else {
59 die('2月份天数不在正常范围内');
60 }
61
62 } else if( in_array($this->month, $this->month_30) ) {
63
64 if( $this->date >= 1 && $this->date < 30 ) {
65
66 $this->date = $this->date + $this->syncNumDate;
67 if( $this->date + $this->syncNumDate > 30 ) {
68 $this->computeDateMonth();
69 }
70
71 } else if($this->date >= 30){
72
73 $D_value = $this->syncNumDate - (30 - $this->date);
74 if( $D_value != 0 ) {
75 $this->computeDateMonth($D_value);
76 } else {
77 $this->computeDateMonth();
78 }
79
80 } else {
81 die('30天的月份天数不在正常范围内');
82 }
83
84 } else if(in_array($this->month, $this->month_31)) {
85
86 if( $this->date >= 1 && $this->date < 31 ) {
87
88 $this->date = $this->date + $this->syncNumDate;
89 if( $this->date + $this->syncNumDate > 31 ) {
90 $this->computeDateMonth();
91 }
92
93 } else if( $this->date >= 31 ){
94
95 $D_value = $this->syncNumDate - (31 - $this->date);
96 if( $D_value != 0 ) {
97 $this->computeDateMonth($D_value);
98 } else {
99 $this->computeDateMonth();
100 }
101
102 } else {
103 die('31天的月份天数不在正常范围内');
104 }
105
106 } else {
107 // echo $this->month;
108 die('函数computeTimeDate计算年月日发生错误');
109 }
110 }
111
112 /**
113 * 计算加减月份,如果超过12 就让年份 +1 月份恢复到1
114 * @param $D_value 差值,由于在计算天数的时候,存在加值过大,造成的重复计算,例如30+6 可能计算两次,差值就是 30+1 剩下的5天,在新的月份添加
115 */
116 private function computeDateMonth($D_value='') {
117 if($this->month >= 1 && $this->month < 12) {
118 $this->month = $this->month + 1;
119
120 if( $D_value != '' ) {
121 $this->date = $D_value;
122 } else {
123 $this->date = 1;
124 }
125 } else if($this->month == 12) {
126 if( $this->year == date('Y', time()) ) {
127 return;
128 } else {
129 $this->year = $this->year + 1;
130 $this->month = 1;
131
132 if( $D_value != '' ) {
133 $this->date = $D_value;
134 } else {
135 $this->date = 1;
136 }
137 // $this->computeTime();
138 }
139 } else {
140 die('computeDateMonth函数计算错误');
141 }
142 }
写了以上的代码,也算了解了日期处理的一个过程!对于记忆这个函数更深刻了!~~
其实主要还是自己想写一遍! 至少自己对函数也有一个更好的理解!
以下是用PHP代码实现上面的一堆!
data( 'Y-m-d', strtotime( ' +1 days ' ) );
页:
[1]