Use php's strtotime() function with caution, phpstrtotime function_PHP tutorial

WBOY
Release: 2016-07-13 10:21:36
Original
773 people have browsed it

Use php's strtotime() function carefully, phpstrtotime function

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> }
Copy after login

 

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> }  
Copy after login

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> }
Copy after login

Use php's strtotime() function with caution, phpstrtotime function_PHP tutorial(Note: strtotime("-$n month"), the second parameter is omitted, the second parameter indicates the distance time, omitting indicates the current time)

So if this is the case, how do we use strtotime("-$n month") to handle our needs?

A piece of handwritten code is provided below for reference


Problem with strtotime function in php

mktime() requires parameters. The easiest way to get two days later is: echo date("Y-m-d",strtotime("+2 days")); echo '
';
echo date("Y-m-d H:i:s",strtotime("+2 days"));
?>Take a good look at the strtotime function Note, you can also +10 hours
-2 hours
and so on. How does the php strtotime function change the specified time? $date = date("Y-m-d", strtotime('2012-01-20') + 60*60*24); http://www.bkjia.com/PHPjc/855628.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/855628.htmlTechArticleUse php’s strtotime() function with caution. The phpstrtotime function is often used in our daily business based on business volume. The database is divided into tables horizontally according to time. The query after the table is divided will often...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template

  时间

差值

理论时间

结果

7月31号

1月前

6月31号

6月只有30天,则加一天到7月1号

7月31号

2月前

5月31号

7月31号

3月前

4月31号

4月只有30天,则加一天到5月1号

……

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
Results

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

……