首页  >  文章  >  后端开发  >  谨慎使用php的strtotime()函数,phpstrtotime函数_PHP教程

谨慎使用php的strtotime()函数,phpstrtotime函数_PHP教程

WBOY
WBOY原创
2016-07-13 10:21:36787浏览

谨慎使用php的strtotime()函数,phpstrtotime函数

  我们在日常业务中,针对业务量,经常会采用对数据库按时间做横向分表,分表后的查询往往会涉及到时间问题。例如,我们想查询某个用户距离当前时间1个月的订单情况,在这个时候,我们有些会用到strtotime()函数去处理。

  但是使用strtotime(),需要非常谨慎。我们先看一段代码,代码目的是想拿到几个月以前的年份月份,例如今天是2014年8月1号,我想拿到2个月前的年份月份是 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> }

  

  表面看代码似乎没有问题,但是我们做个测试,下面是测试代码,测试的目的很简单,只是想测试一下,每个月最后一天的前一个月的日期是多少

<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> }  

  我们看一下测试结果,从测试结果中,我们发现我们忽略了每个月天数不同,那么strtotime()会带来不一样的结果

 

  那么究竟 strtotime("-$n month") 是怎么计算的呢?在做一个测试,如下:查看一下结果

<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> }

 

谨慎使用php的strtotime()函数,phpstrtotime函数_PHP教程(注:strtotime("-$n month"),第二个参数省略,第二个参数表示距离的时间,省略表示当前时间)

  时间

差值

理论时间

结果

7月31号

1月前

6月31号

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

7月31号

2月前

5月31号

 

7月31号

3月前

4月31号

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

……

 

那么如果这样的话,我们怎么用strtotime("-$n month")处理我们的需求呢?

下面提供一段手写代码供参考

<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> }

 

php 中strtotime函数的问题

mktime() 需要带参数的。

想要得到两天后的最简单方法是:

echo date("Y-m-d",strtotime("+2 days"));
echo '
';
echo date("Y-m-d H:i:s",strtotime("+2 days"));
?>
好好看看 strtotime的函数说明,你还可以
+10 hours
-2 hours
等等。
 

php strtotime函数怎在指定的时间上变动?

$date = date("Y-m-d", strtotime('2012-01-20') + 60*60*24);
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/855628.htmlTechArticle谨慎使用php的strtotime()函数,phpstrtotime函数 我们在日常业务中,针对业务量,经常会采用对数据库按时间做横向分表,分表后的查询往往会...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn