几周之前的汇总时间

时间:2014-01-16 19:03:22

标签: php arrays date mktime

所以我有这种情况。我有多个这样的数组:

$array('date'=>'00-00-00', 'value'=>'xx');

我想要做的是遍历所有数组,并制作4个变量:$week1, $week2, $week3, $week4我将存储聚合值,具体取决于它是1周前,2周前,3周前还是4周从某个日期开始。

我试过的是这个,但它不起作用。即使我有很多日期,并且它们有值,但它对于所有内容都返回0,名称只是占位符,我实际上并没有将它们称为'数组':

     $date = 'xx-xx-xx';
     $month = array();

     $week1 = strtotime($date . '-1 week');
     $week2 = strtotime($date . '-2 week');
     $week3 = strtotime($date . '-3 week');
     $week4 = strtotime($date . '-4 week');


     foreach($arrays as $array){

        if( (date('W', strtotime($array['date']) == date('W', strtotime($week1)))) AND (date('Y', strtotime($array['date'])) == date('Y', strtotime($week1)))){
            $month['week1'] += $array['value'];
        }

        if( (date('W', strtotime($array['date']) == date('W', strtotime($week2)))) AND (date('Y', strtotime($array['date'])) == date('Y', strtotime($week2)))){
            $month['week2'] += $array['value'];
        }

        if( (date('W', strtotime($array['date']) == date('W', strtotime($week3)))) AND (date('Y', strtotime($array['date'])) == date('Y', strtotime($week3)))){
            $month['week3'] += $array['value'];
        }

        if( (date('W', strtotime($array['date']) == date('W', strtotime($week4)))) AND (date('Y', strtotime($array['date'])) == date('Y', strtotime($week4)))){
            $month['week4'] += $array['value'];
        }           
     }

     return $month;

2 个答案:

答案 0 :(得分:1)

您的数据格式存在一些问题,因为您使用的是XX-XX-XX的模糊格式。那是什么意思? YY-MM-DD? MM-DD-YY? DD-MM-YY?如果没有进一步定义日期字符串代表的内容,您不能指望strtotime转换该格式。

我还建议使用DateTime,DateInterval,DatePeriod等类,因为它们比旧的PHP日期操作函数功能更全面。

所以,我可能会这样实现:

$array = array(...); // your input array
$start_time = new DateTime();
// Alternately, if you need week boundary to not be based on "now" 
// but rather on fixed start of week you could do something like
// $current_time = new DateTime('Monday this week 00:00:00');
$date_interval = new DateInterval('P1W');
$date_interval->invert = 1; // make it negative interval
// build DatePeriod object with 4 interval periods - don't include start date
$date_period = new DatePeriod($start_time, $date_interval, 4, DatePeriod::EXCLUDE_START_DATE);

$final_array = array(0,0,0,0); // the destination array with default start values
array_walk($array, function($item, $key_not_used) use ($date_period, $final_array) {
    // change date format as necessary below
    $item_time = DateTime::createFromFormat('y-m-d', $item['date']);
    $value = $item['value'];
    foreach($date_period as $i => $comparison_time) {
        if ($item_time > $comparison_time) {
            // aggregate this value into $final_array.
            // I assume this is simple addition but any sort of aggregation can be used by changing this line below
            $final_array[$i] = $final_array[$i] + $value;
            // break out of loop since comparison already completed
            break;
        }
    }
});
var_dump($final_array);

这将给最终数组赋值如下:

Array(
    [0] => ?, // aggregation for dates > 1 week ago
    [1] => ?, // aggregation for dates between 1 and 2 weeks ago
    [2] => ?, // aggregation for dates between 2 and 3 weeks ago
    [3] => ? // aggregation for dates between 3 and 4 weeks ago
)

答案 1 :(得分:0)

你的方法看起来很好。你为什么认为它很慢?嗯,你可以,例如使用elseif而不是if。

如果你还是感觉不好,可以考虑一周是7 * 24 * 3600 = 604800秒。所以周一早上(00:00)开始了一周:

$timestamp = strtotime($date);
$time_since_thursday = $timestamp % 604800;
$time_since_monday = ($time_since_thursday + 259200) % 604800;
$monday = $timestamp - $time_since_monday;

然后你可以使用从这个星期一早上到另一个日期的差异:

foreach($arrays as $array) {
  $difference = $monday - $array['date'];
  if ($difference > 0) { // else it would be in the same week or even later
    $difference_weeks = (int)($difference / 604800);
    if ($differenceWeeks <= 3) { // else it is more than the 4th week behind
      $month['week' . ($difference_weeks + 1)] += $array['value'];
    }
  }
}

测试自己,如果这个解决方案更快......

编辑:出于某种原因,我认为01.01.1970是星期一,结果证明是错的,所以我不得不改变代码。