网站前端建设报价单,外国建筑设计网站,做简历模板的网站都有哪些,企业电子商务网站建设和一般最近几天在做一个项目#xff0c;主要是将SQLserver数据到MySQL数据库#xff0c;一个url跑一次 同步一次昨天的数据#xff0c;由于很多数据需要同步#xff0c;所以做了一个操作界面的#xff0c;一个单纯跑url的 在其中涉及到了对于时间的计算#xff01;当我写完这个…最近几天在做一个项目主要是将SQLserver数据到MySQL数据库一个url跑一次 同步一次昨天的数据由于很多数据需要同步所以做了一个操作界面的一个单纯跑url的 在其中涉及到了对于时间的计算当我写完这个程序的时候我回头看我写的计算时间的代码。有些都有点儿懵了。。。在这里记录下来方便以后回忆也方便别人使用 代码可能会臃肿新人一枚欢迎指正拒绝骂街哦 先简单说一下代码其中主要涉及到计算润年 平年计算28天 31天 30天月份 等主要就是判断代码中有注释大家看一下希望大家多多指正缺点 1 /**一年中的31天的月份2 * var array3 */4 private $month_31 array(1,3,5,7,8,10,12);5 6 /**一年中30天的月份7 * var array8 */9 private $month_30 array(4,6,9,11);10 11 /**查询开始的时间戳12 * var13 */14 private $startTimeStamp;15 16 /**查询结束的时间戳17 * var18 */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 差值由于在计算天数的时候存在加值过大造成的重复计算例如306 可能计算两次差值就是 301 剩下的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 ) ); 转载于:https://www.cnblogs.com/itafter/p/4202957.html