简体   繁体   English

PHP:从两个日期之间的数组中获取元素

[英]PHP: Get elements from an array which are between two dates

Is there a function to do this? 有功能可以做到这一点吗?

For example if I have an array like this: 例如,如果我有一个像这样的数组:

array(
   2014-08-05 10:23:34,
   2014-08-08 13:12:56,
   2014-08-07 08:02:21,
   2014-08-06 11:22:33,
   2014-08-03 6:02:44,
   2014-08-08 10:23:34
);

and I'd like to return all the dates BETWEEN 2014-08-03 AND 2014-08-06. 并且我想返回2014-08-03和2014-08-06之间的所有日期。

There is a huge amount of data in these arrays, there may be even tens of thousands of data. 这些阵列中有大量数据,甚至可能有数以万计的数据。 I'm actually getting everything from the database and I'd like to divide the data by date (like 2 hours, 1 day, 3 days and so on, based on the time range a visitor selects). 我实际上是从数据库中获取所有内容,我想按日期划分数据(例如2小时,1天,3天,依访问者选择的时间范围等)。

How is it possible, without making huge queries or extensive PHP functions? 在没有进行大量查询或没有大量PHP函数的情况下,怎么可能?

EDIT: 编辑:

As per request I'm showing the data structure of the chart plugin (the values are just quick examples): 根据请求,我正在显示图表插件的数据结构(这些值只是简单的示例):

 {x: '2014-08-05 10:23:34', y: 3, data1: 3, data2: 320, data3: 76},
 {x: '2014-08-05 10:23:34', y: 2, data1: 1, data2: 300, data3: 27},
 {x: '2014-08-05 10:23:34', y: 2, data1: 4, data2: 653, data3: 33},
 {x: '2014-08-05 10:23:34', y: 3, data1: 3, data2: 120, data3: 54}

Are you able to do your sorting in MySQL? 您可以在MySQL中进行排序吗? If so, you can use the BETWEEN operator: 如果是这样,则可以使用BETWEEN运算符:

'SELECT * FROM my_table where date_column BETWEEN '2014-08-03' and '2014-08-06' 'SELECT * FROM my_table where date_column在'2014-08-03'和'2014-08-06'之间

Edit: same as using >= and <=, so be careful with the second date. 编辑:与使用> =和<=相同,因此请注意第二个日期。 2014-08-06 00:00:01 would not be returned by this query because it is greater than 2014-08-06. 此查询不会返回2014-08-06 00:00:01,因为它大于2014-08-06。

You can try using array walk 您可以尝试使用数组遍历

function checkDate(&$item1, $key, $dateToCompare)
{
    //Your Function to compare and echo or anything else
}

array_walk($fruits, 'checkDate', $dateToCompare);

As Pagerange mentioned, it would be best to use a SQL query, but if you must do this in PHP here is one solution: 如Pagerange所述,最好使用SQL查询,但是如果必须在PHP中执行此操作,则这是一种解决方案:

// start date chosen by user
$start_year = 2014;
$start_month = 8;
$start_day = 3;

// end date chosen by user
$end_year = 2014;
$end_month = 8;
$end_day = 6;

$dates = array(
   '2014-08-05 10:23:34',
   '2014-08-08 13:12:56',
   '2014-08-07 08:02:21',
   '2014-08-06 11:22:33',
   '2014-08-03 6:02:44',
   '2014-08-08 10:23:34'
);

$data_in_range = array();

// using FOR instead of FOREACH for performance
// (assuming zero-based index for $dates array)
for($i = 0; $i < count($dates); $i++) {
  $year = substr($dates[$i], 0, 4);
  // using intval for values with a leading 0
  $month = intval(substr($dates[$i], 5, 2));
  $day = intval(substr($dates[$i], 8, 2));

  if ($year >= $start_year && $year <= $end_year) {
    if ($month >= $start_month && $month <= $end_month) {
      // depending on your definition of 'between' (MySQL
      // includes the boundaries), you may want
      // this conditional to read > && <
      if ($day >= $start_day && $day <= $end_day) {
        $data_in_range[] = $dates[$i];
      }
    }
  }
}

print_r($data_in_range);

I am not recommending this answer, but offering a PHP solution. 我不建议这个答案,但是提供了一个PHP解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM