PHP 擷取兩個時間之間的月份

來源:互聯網
上載者:User

標籤:存在   輸出   有一個   logs   for   時間   foreach   考勤   頭部   

## 擷取兩個時間之間的間距時間

$s = ‘2017-02-05‘;$e = ‘2017-07-20‘;

 

$start    = new \DateTime($s);$end      = new \DateTime($e);// 時間間距 這裡設定的是一個月$interval = \DateInterval::createFromDateString(‘1 month‘);$period   = new \DatePeriod($start, $interval, $end);
foreach ($period as $dt) {    echo $dt->format("Y-m") . "<br>\n";}

輸出結果:

2017-032017-042017-052017-06

 

擷取頭部和尾部的時間範圍:

有時候會遇到這樣一個情境如:

存在兩行表:

  1. 月度考情表(記錄每個員工一個月的考勤)

  2. 日度考勤表(記錄每個員工每一天的考勤)

 

這時候有一個需求是這樣的:

查詢 “2017-02-05” —— “2017-012-20”

這之間的員工的考勤資料。

一般情況下會這麼做,直接 根據時間篩選日度考勤表。結果是沒問題的,但是試想一下,如果有2000個員工,每天的考勤, 那一年是不是  73W的資料了,如果兩年呢? 加上做一些其他的操作,是不是效率很慢?

這個時候可以查詢 “2017-02-05” —— “2017-012-20” 兩個日期時間的完整月份也就是 2017-03,2017-04,2017-05..以此類推

剩下的不完整日期再拿出來 篩選日度考勤表,那麼篩選的量會大大降低。

以下是擷取不完整日期的代碼:

$s = ‘2017-02-05‘;$e = ‘2017-07-20‘;// 擷取頭部的時間範圍if (date(‘j‘, strtotime($s)) > 1) {    $start_day_head = date(‘Y-m-01‘, strtotime($s));    $end_day_head = date(‘Y-m-t‘, strtotime($s));    $s = date(‘Y-m-01‘, strtotime("+1 months", strtotime($s)));}// 擷取尾部的時間範圍if (date(‘j‘, strtotime($e)) > 1) {    $start_day_tail = date(‘Y-m-01‘, strtotime($e));    $end_day_tail = date(‘Y-m-t‘, strtotime($e));    $e = date(‘Y-m-t‘, strtotime("-1 months", strtotime($e)));}

  

PHP 擷取兩個時間之間的月份

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.