Radical_Activity
Radical_Activity

Reputation: 2738

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.

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}

Upvotes: 0

Views: 726

Answers (3)

joshplien
joshplien

Reputation: 346

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.

Upvotes: 0

Pagerange
Pagerange

Reputation: 234

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.

Upvotes: 1

Jenno Richi Benat
Jenno Richi Benat

Reputation: 671

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

Upvotes: 0

Related Questions