设为首页 收藏本站
查看: 680|回复: 0

[经验分享] 自学php找工作【二】 PHP计算时间加一天

[复制链接]
累计签到:1365 天
连续签到:1 天
发表于 2015-8-25 11:34:11 | 显示全部楼层 |阅读模式
  最近几天在做一个项目,主要是将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 $Feb  2月的天数
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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-103984-1-1.html 上篇帖子: PHP 判断是否为 AJAX 请求 下篇帖子: php的memcache和memcached扩展区别
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表