Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php获取按月份分组的时间戳值_Php - Fatal编程技术网

Php获取按月份分组的时间戳值

Php获取按月份分组的时间戳值,php,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/201

假设我有

$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 
 ]
我该怎么做呢?

我编辑了你的代码

试试这个:

<?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;

}
我编辑了你的代码

试试这个:

<?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;

}

在澄清您对我上一个答案的疑问后,我决定删除我的答案并发布新答案

$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);
$start=1492894800//23/04/2017
$end=1503521999//23/08/2017
$steps=日期($Ym',$end)-日期($Ym',$start);
$timestamps=array();
对于($i=0;$i$i==0)日期($Y-m-d',$start):日期($Y-m-01',$base),
'至'=>$i==$steps?日期('Y-m-d',$end):日期('Y-m-t',$base)
);
//如果我们想要时间戳
//$timestamps[]=数组(
//'from'=>strottime(日期('Y-m-01',$base)),
//'to'=>strotime(日期('Y-m-t',$base))
//  );
}
打印(时间戳);

在澄清您对我上一个答案的疑问后,我决定删除我的答案并发布新答案

$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);
$start=1492894800//23/04/2017
$end=1503521999//23/08/2017
$steps=日期($Ym',$end)-日期($Ym',$start);
$timestamps=array();
对于($i=0;$i$i==0)日期($Y-m-d',$start):日期($Y-m-01',$base),
'至'=>$i==$steps?日期('Y-m-d',$end):日期('Y-m-t',$base)
);
//如果我们想要时间戳
//$timestamps[]=数组(
//'from'=>strottime(日期('Y-m-01',$base)),
//'to'=>strotime(日期('Y-m-t',$base))
//  );
}
打印(时间戳);

当日期关闭时,此操作失败,例如:5月31日和6月1日期间数组仅为一个值,并且当您选择时区时。当日期关闭时,此操作失败,例如:5月31日和6月1日期间数组仅为一个值,并且当您选择时区时。