謹慎使用php的strtotime()函數

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   strong   io   

  我們在日常業務中,針對業務量,經常會採用對資料庫按時間做橫向分表,分表後的查詢往往會涉及到時間問題。例如,我們想查詢某個使用者距離目前時間1個月的訂單情況,在這個時候,我們有些會用到strtotime()函數去處理。

  但是使用strtotime(),需要非常謹慎。我們先看一段代碼,代碼目的是想拿到幾個月以前的年份月份,例如今天是2014年8月1號,我想拿到2個月前的年份月份是 array("0"=>"201406", "1"=>"201407",)

 1 /**** 2 *$mthNum 幾月以前 3 * return like array(‘0‘=>‘201401‘,‘1‘=>‘201402‘),結果不包含當前月份 4 ************/ 5 function getTimeYm($mthNum) 6 { 7     $timeArr = array(); 8      9     if($mthNum <= 0)10         return $timeArr;11 12     do 13     {14         $timeArr[] = date("Ym", strtotime("-$mthNum month"));15         $mthNum --;16     }17     while ($mthNum > 0);18 19     return $timeArr;20 }

  

  表面看代碼似乎沒有問題,但是我們做個測試,下面是測試代碼,測試的目的很簡單,只是想測試一下,每個月最後一天的前一個月的日期是多少

 1 <?php 2 $dateArr = array( 3     "2014-01-31    00:00:00 -1 month", 4     "2014-02-28    00:00:00 -1 month", 5     "2014-03-31    00:00:00 -1 month", 6     "2014-04-30    00:00:00 -1 month", 7     "2014-05-31    00:00:00 -1 month", 8     "2014-06-30    00:00:00 -1 month", 9     "2014-07-31    00:00:00 -1 month",10     "2014-08-31    00:00:00 -1 month",11     "2014-09-30    00:00:00 -1 month",12     "2014-10-31    00:00:00 -1 month",13     "2014-11-30    00:00:00 -1 month",14     "2014-12-31    00:00:00 -1 month",15 );16 17 foreach ($dateArr as $val)18 {19     $time = strtotime($val);20     echo [$time][$val]."\r\n";21 }  

  我們看一下測試結果,從測試結果中,我們發現我們忽略了每個月天數不同,那麼strtotime()會帶來不一樣的結果

 

  那麼究竟 strtotime("-$n month") 是怎麼計算的呢?在做一個測試,如下:查看一下結果

 1 <?php 2  3 $testTime = date("Y-m-d H:i:s", time()); 4 echo "測試時間:{$testTime}  \r\n"; 5  6 $flag = 0;  7 $time = 0; 8 $tmp  = 0; 9 10 while(1)11 {12     if($flag ++ > 12)13         break;14 15     $time      =  strtotime("-$flag month");16     $monthDiff = ($time - $tmp)/86400;  //86400 = 24 * 60 * 60,17     $tmp       = $time;18 19     $dispDate = date("Y-m-d H:i:s", $time);20 21     echo "{$flag}月前: {$time},  日期:{$dispDate)}   差值:{$dispDate}天 \r\n";22 }

 

 

通過這個我們發現原來strtotime("-$n month")是這樣計算的 (註: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")處理我們的需求呢?

下面提供一段手寫代碼供參考

 1 /**************** 2 *解決兩個時間段之間的年月 3 * $btm, $etm 是unix時間戳記 4 *****************/ 5 function getTimeDis($btm, $etm) 6 { 7     $resArr = array(); 8     if($etm < $btm) 9         return $resArr;10 11     //將btm和etm都轉成每月1號12     $btmc = strtotime(date("Y-m-01 00:00:00", $btm));13     $etmc = strtotime(date("Y-m-01 00:00:00", $etm));14 15 16     $flag = 0; //時間差標識符17     $resArr[] = date("Ym", $etmc);18 19     while(1)20     {21         $flag ++;22         $compTime = strtotime("-{$flag} month",  $etmc);23         24         if($compTime < $btm)25             break;26 27         $resArr[] = date("Ym", $compTime);28     }29 30     return array_unique($resArr);31 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.