In our daily business, in view of the business volume, we often use the horizontal partitioning of the database by time. Later inquiries often involve time issues. For example, we want to query the order status of a user one month from the current time. At this time, we will use the strtotime() function to process.
But when using strtotime(), you need to be very careful. Let's look at a piece of code first. The purpose of the code is to get the year and month a few months ago. For example, today is August 1, 2014. I want to get the year and month 2 months ago, which is array("0"=> "201406", "1"=>"201407",)
<span> 1</span> <span>/*</span><span>*** </span><span> 2</span> <span>*$mthNum 几月以前 </span><span> 3</span> <span>* return like array('0'=>'201401','1'=>'201402'),结果不包含当前月份 </span><span> 4</span> <span>***********</span><span>*/</span> <span> 5</span> <span>function</span> getTimeYm(<span>$mthNum</span><span>) </span><span> 6</span> <span>{ </span><span> 7</span> <span>$timeArr</span> = <span>array</span><span>(); </span><span> 8</span> <span> 9</span> <span>if</span>(<span>$mthNum</span> <= 0<span>) </span><span>10</span> <span>return</span> <span>$timeArr</span><span>; </span><span>11</span> <span>12</span> <span>do</span> <span>13</span> <span> { </span><span>14</span> <span>$timeArr</span>[] = <span>date</span>("Ym", <span>strtotime</span>("-<span>$mthNum</span> month"<span>)); </span><span>15</span> <span>$mthNum</span> --<span>; </span><span>16</span> <span> } </span><span>17</span> <span>while</span> (<span>$mthNum</span> > 0<span>); </span><span>18</span> <span>19</span> <span>return</span> <span>$timeArr</span><span>; </span><span>20</span> }
On the surface, there seems to be no problem with the code, but let’s do a test. The following is the test code. The purpose of the test is very simple. I just want to test what is the date of the month before the last day of each month
<span> 1</span> <?<span>php </span><span> 2</span> <span>$dateArr</span> = <span>array</span><span>( </span><span> 3</span> "2014-01-31 00:00:00 -1 month", <span> 4</span> "2014-02-28 00:00:00 -1 month", <span> 5</span> "2014-03-31 00:00:00 -1 month", <span> 6</span> "2014-04-30 00:00:00 -1 month", <span> 7</span> "2014-05-31 00:00:00 -1 month", <span> 8</span> "2014-06-30 00:00:00 -1 month", <span> 9</span> "2014-07-31 00:00:00 -1 month", <span>10</span> "2014-08-31 00:00:00 -1 month", <span>11</span> "2014-09-30 00:00:00 -1 month", <span>12</span> "2014-10-31 00:00:00 -1 month", <span>13</span> "2014-11-30 00:00:00 -1 month", <span>14</span> "2014-12-31 00:00:00 -1 month", <span>15</span> <span>); </span><span>16</span> <span>17</span> <span>foreach</span> (<span>$dateArr</span> <span>as</span> <span>$val</span><span>) </span><span>18</span> <span>{ </span><span>19</span> <span>$time</span> = <span>strtotime</span>(<span>$val</span><span>); </span><span>20</span> <span>echo</span> [<span>$time</span>][<span>$val</span>]."\r\n"<span>; </span><span>21</span> }
Let’s take a look at the test results. From the test results, we found that we ignored the number of days in each month is different, then strtotime() will bring different results
So how is strtotime("-$n month") calculated? Doing a test, as follows: Check the results
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>$testTime</span> = <span>date</span>("Y-m-d H:i:s", <span>time</span><span>()); </span><span> 4</span> <span>echo</span> "测试时间:{<span>$testTime</span>} \r\n"<span>; </span><span> 5</span> <span> 6</span> <span>$flag</span> = 0<span>; </span><span> 7</span> <span>$time</span> = 0<span>; </span><span> 8</span> <span>$tmp</span> = 0<span>; </span><span> 9</span> <span>10</span> <span>while</span>(1<span>) </span><span>11</span> <span>{ </span><span>12</span> <span>if</span>(<span>$flag</span> ++ > 12<span>) </span><span>13</span> <span>break</span><span>; </span><span>14</span> <span>15</span> <span>$time</span> = <span>strtotime</span>("-<span>$flag</span> month"<span>); </span><span>16</span> <span>$monthDiff</span> = (<span>$time</span> - <span>$tmp</span>)/86400; <span>//</span><span>86400 = 24 * 60 * 60,</span> <span>17</span> <span>$tmp</span> = <span>$time</span><span>; </span><span>18</span> <span>19</span> <span>$dispDate</span> = <span>date</span>("Y-m-d H:i:s", <span>$time</span><span>); </span><span>20</span> <span>21</span> <span>echo</span> "{<span>$flag</span>}月前: {<span>$time</span>}, 日期:{<span>$dispDate</span>)} 差值:{<span>$dispDate</span>}天 \r\n"<span>; </span><span>22</span> }
(Note: strtotime("-$n month"), the second parameter is omitted, the second parameter indicates the distance time, omitting indicates the current time)
|
Difference |
Theory time |
<span> 1</span> <span>/*</span><span>*************** </span><span> 2</span> <span>*解决两个时间段之间的年月 </span><span> 3</span> <span>* $btm, $etm 是unix时间戳 </span><span> 4</span> <span>****************</span><span>*/</span> <span> 5</span> <span>function</span> getTimeDis(<span>$btm</span>, <span>$etm</span><span>) </span><span> 6</span> <span>{ </span><span> 7</span> <span>$resArr</span> = <span>array</span><span>(); </span><span> 8</span> <span>if</span>(<span>$etm</span> < <span>$btm</span><span>) </span><span> 9</span> <span>return</span> <span>$resArr</span><span>; </span><span>10</span> <span>11</span> <span>//</span><span>将btm和etm都转成每月1号</span> <span>12</span> <span>$btmc</span> = <span>strtotime</span>(<span>date</span>("Y-m-01 00:00:00", <span>$btm</span><span>)); </span><span>13</span> <span>$etmc</span> = <span>strtotime</span>(<span>date</span>("Y-m-01 00:00:00", <span>$etm</span><span>)); </span><span>14</span> <span>15</span> <span>16</span> <span>$flag</span> = 0; <span>//</span><span>时间差标识符</span> <span>17</span> <span>$resArr</span>[] = <span>date</span>("Ym", <span>$etmc</span><span>); </span><span>18</span> <span>19</span> <span>while</span>(1<span>) </span><span>20</span> <span> { </span><span>21</span> <span>$flag</span> ++<span>; </span><span>22</span> <span>$compTime</span> = <span>strtotime</span>("-{<span>$flag</span>} month", <span>$etmc</span><span>); </span><span>23</span> <span>24</span> <span>if</span>(<span>$compTime</span> < <span>$btm</span><span>) </span><span>25</span> <span>break</span><span>; </span><span>26</span> <span>27</span> <span>$resArr</span>[] = <span>date</span>("Ym", <span>$compTime</span><span>); </span><span>28</span> <span> } </span><span>29</span> <span>30</span> <span>return</span> <span>array_unique</span>(<span>$resArr</span><span>); </span><span>31</span> } Copy after login
| ||||||||||||||||||||
July 31 |
1 month ago
|
June 31 |
There are only 30 days in June, so add one day to July 1 | ||||||||||||||||||||
July 31
|
2 months ago |
May 31 |
|
||||||||||||||||||||
July 31 |
3 months ago |
April 31 |
April only has 30 days, so add one day to May 1 |
||||||||||||||||||||
…… |