0

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.

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).

How is it possible, without making huge queries or extensive PHP functions?

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}
8
  • 1
    I would still use a query - i guess the performance will be much better instead of reading ALL Data into an Array, and then filter it. Is this no possibility? Commented Aug 8, 2014 at 17:30
  • Basically I'm using a chart here. I provide the visitors the feature to look back in their reports by selecting 2 dates, than I'd like to draw some data to the chart, between those dates. Of course when they select 1 day only, I want them to see a more detailed report of that day and otherwise give them a broader look to the chart. This would require a lot of queriing to decide if it's just 1 day, than sum the values for each 2 hour and so on. Commented Aug 8, 2014 at 17:36
  • So you got all the needed data in your database? Why not creating a query that fits to your requirements? Everthing else would be very slow (if you have lots of users and data) - you could easiliy use the BETWEEN Operator in MySQL. Commented Aug 8, 2014 at 17:38
  • Yes, but you know I have to decide if it's 1 day or 5 a week or a year and than get all the data based on that criteria. It seems like a really long query. Isn'tthere any other possible solution to get chart data from the DB? Commented Aug 8, 2014 at 17:46
  • Show us the data structure for your chart plugin. Commented Aug 8, 2014 at 17:50

3 Answers 3

1

Are you able to do your sorting in MySQL? If so, you can use the BETWEEN operator:

'SELECT * FROM my_table where date_column BETWEEN '2014-08-03' and '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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

array_filter is probably a better option since he wants to return it
0

As Pagerange mentioned, it would be best to use a SQL query, but if you must do this in PHP here is one solution:

// 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.