2015-05-22 75 views
2

今天是2015年5月22日(理解該示例)PHP日期時間:獲取當前周/月/年的間隔時間(開始 - 結束)與上年相比

我看到一些統計數據來自數據庫。最初我不得不做這些比較

  • 本週與前一週相比。 [2015-05-18 00:00:002015-05-24 23:59:59] - [2015-05-11 00:00:002015-05-17 00: 00:00]
  • 本月與前一個月比較。 [2015-05-01 00:00:002015-05-31 23:59:59] - [2015-04-01 00:00:002015-04-30 23: 59:59]
  • 今年與上年相比。 [2015-01-01 00:00:002015-12-31 23:59:59] - [2014-01-01 00:00:002014-12-31 23: 59:59]

對於所有這些控件我做這個函數計算所有各種日期間隔。

function getIntervalDate(){ 

    $today = new DateTime(); 
    $appToday = new DateTime(); 

    $numDay = date("w"); 
    $numYear = (int)date("Y"); 

    switch($numDay){ 
     //monday 
     case 1 : 
      $startWeek = $today->format('Y-m-d 00:00:00'); 
      $endWeek = $today->modify('next sunday')->format('Y-m-d 23:59:59'); 

      $startWeek2 = $today->modify('last monday -1 week')->format('Y-m-d 00:00:00'); 
      $endWeek2 = $today->modify('next sunday')->format('Y-m-d 00:00:00'); 

     break; 
     default: 
      $startWeek = $today->modify('last monday')->format('Y-m-d 00:00:00'); 
      $endWeek = $today->modify('next sunday')->format('Y-m-d 23:59:59'); 

      $startWeek2 = $today->modify('last monday -1 week')->format('Y-m-d 00:00:00'); 
      $endWeek2 = $today->modify('next sunday')->format('Y-m-d 00:00:00'); 

     break; 
    } 

    $startMonth = $today->modify('first day of this month')->format('Y-m-d H:i:s'); 
    $endMonth = $today->modify('last day of this month')->format('Y-m-d 23:59:59'); 

    $today1 = $appToday; 

    $startMonth2 = $today1->modify('first day of previous month')->format('Y-m-d H:i:s'); 
    $endMonth2 = $today1->modify('last day of this month')->format('Y-m-d 23:59:59'); 

    $today2 = $appToday; 

    $startYear = $today2->modify('first day of January '.$numYear)->format('Y-m-d 00:00:00'); 
    $endYear = $today2->modify('last day of December '.$numYear)->format('Y-m-d 23:59:59'); 

    $today3 = $appToday; 

    $startYear2 = $today3->modify('first day of January '.($numYear-1))->format('Y-m-d 00:00:00'); 
    $endYear2 = $today3->modify('last day of December '.($numYear-1))->format('Y-m-d 23:59:59'); 

    return array(

     "startWeek" => $startWeek, 
     "endWeek" => $endWeek, 
     "startWeek2" => $startWeek2, 
     "endWeek2" => $endWeek2, 

     "startMonth" => $startMonth, 
     "endMonth" => $endMonth, 
     "startMonth2" => $startMonth2, 
     "endMonth2" => $endMonth2, 

     "startYear" => $startYear, 
     "endYear" => $endYear, 
     "startYear2" => $startYear2, 
     "endYear2" => $endYear2 

    ); 

} 

enter image description here

一切完美的作品。但現在我必須改變各種比較。我必須總是比較目前的周/月/年的區間,但與上一年的周/月/年(在一年的情況下沒有問題,代碼保持不變)

我做了一些測試,但我失敗了。我主要使用->modify('-1 year'),但沒有一個完美的結果。

+2

很高興看到有人實際使用,而不是重整的strtotime()和日期() –

+1

日期時間「但與不完美的結果。」仔細解釋出了什麼問題? –

+0

你的問題是什麼? –

回答

0

基於上述這裏你討論的是你的問題的解決方案:

$startWeek->modify("-1 year")->modify('monday')->format('Y-m-d H:i:s');

這裏是考驗。

$today = DateTime::createFromFormat('Y-m-d H:i:s', '2015-05-18 00:00:00'); 
$startWeekPrevYear=$today->modify('-1 year')->modify('monday'); 

echo $startWeekPrevYear->format('Y-m-d H:i:s');//outputs 2014-05-19 00:00:00 
+0

這取決於你,但答案背後的主要目的是邏輯,如果它幫助請接受它。 –

相關問題