2017-08-23 10 views
0

でグループ化されたタイムスタンプ値iはPHPは数ヶ月

$time1 = 1492894800 //23/04/2017 
$time2 = 1503521999 //23/08/2017 

は私が

get timestamp with rangess between months preserving first and last month of $time1 and $time2 such that i should have 

1 23/04/2017 to 30/04/2017 
2 01/05/2017 - 31/05/2017 
....... //continue full months and lastly 

01/08/2017 - 23/08/2017 

私が試しているように数ヶ月ごとにグループ化されたタイムスタンプを取得したいと思い持っていると仮定し得る

$timearrays = $this->getMontTimestampRanges($time1, $time2); 

public function getMontTimestampRanges($ts1, $ts2){ 

    $start = (new DateTime(date("Y-m-d", $ts1))); 
    $end  = (new DateTime(date("Y-m-d", $ts2))); 

    $interval = DateInterval::createFromDateString('1 day'); 
    $period = new DatePeriod($start, $interval, $end); 

    $timestamps = []; 
    foreach ($period as $dt) { 
     //am stuck here on how to group based on the months  

     ..push to array 
    } 

    return $timestamps; 

} 

最終的な配列は次のような形になります:

$timestamps = [ 
    0=>["from":, "to": ] //first month(april) 
    1=>[ ] //second month may 
] 

どうすればよいですか?

答えて

1

私は

はこれを試してみてください、あなたのコードを編集した:

<?php 
$time1 = 1492894800 ;//23/04/2017 
$time2 = 1503521999; //23/08/2017 
$timearrays = getMontTimestampRanges($time1, $time2); 
var_dump($timearrays); 
function getMontTimestampRanges($ts1, $ts2){ 

    $start = new DateTime(date("Y-m-d", $ts1)); 
    $end  = new DateTime(date("Y-m-d", $ts2)); 

    $interval = DateInterval::createFromDateString('1 month'); 
    $period = new DatePeriod($start, $interval, $end); 

    $timestamps = []; 

    $startM = $period->getStartDate()->format('m'); 
    $endM = $period->getEndDate()->format('m'); 

    foreach ($period as $dt) { 
     //am stuck here on how to group based on the months  
     //start 
     if($startM == $dt->format('m')) 
     { 
      $timestamps[] = array('start'=>$start->format('Y-m-d'),'end'=>$dt->format('Y-m-t')); 
     } 
     elseif($endM == $dt->format('m')) 
     { 
      $timestamps[] = array('start'=>$dt->format('Y-m').'-01','end'=>$end->format('Y-m-d')); 
     } 
     else 
     { 
      $timestamps[] = array('start'=>$dt->format('Y-m').'-01','end'=>$dt->format('Y-m-t')); 
     } 


    } 

    return $timestamps; 

} 

http://sandbox.onlinephpfunctions.com/code/8527e45427fd0114f1e058fffc1885e460807a0a

はそれがお役に立てば幸いです。

+0

日付が例えば接近しているとき、これは失敗します。5月31日と6月1日の期間の配列は、一つの値だけ、ともしたときのタイムゾーンです。 –

1

私の前回の回答であなたの質問を明確にした後、私は自分の答えを削除し、新しい回答を投稿することに決めました。

$start = 1492894800; //23/04/2017 
$end = 1503521999; //23/08/2017 

$steps = date('Ym', $end) - date('Ym', $start); 

$timestamps = array(); 
for($i = 0; $i <= $steps; $i++) { 
    $base = strtotime("+{$i} months", $start); 

    $timestamps[] = array(
     'from' => $i == 0 ? date('Y-m-d', $start) : date('Y-m-01', $base), 
     'to' => $i == $steps ? date('Y-m-d', $end) : date('Y-m-t', $base) 
    ); 

// If we want timestamps 
// $timestamps[] = array(
//  'from' => strtotime(date('Y-m-01', $base)), 
//  'to' => strtotime(date('Y-m-t', $base)) 
// ); 
} 

print_r($timestamps); 

Ideone link

+0

すごくうまく答え、マークする必要があります –