2

I am trying to filter more on start_date and end date .

   <?php 
        /* 
         * filtering an array 
         */ 
        function filter_by_value ($array, $index, $value){ 
            if(is_array($array) && count($array)>0)  
            { 
                foreach(array_keys($array) as $key){ 
                    $temp[$key] = $array[$key][$index]; 

                    if ($temp[$key] == $value){ 
                        $newarray[$key] = $array[$key]; 
                    } 
                } 
              } 
          return $newarray; 
        } 
    ?> 

Example:

<?php 
$results = array (
  0 => 
  array (
    'a' => 'U/H',
    'b' => 'Modification',
    'date' => '02/11/11',
    'h' => '17:15:13',
    'fullname' => 'First1 Name1',
    'desc' => 'Descript ',
  ),
  1 => 
  array (
    'a' => 'J/M',
    'b' => '',
    'date' => '03/11/11',
    'h' => '17:18:27',
    'fullname' =>  'First1 Name1',
    'desc' => 'Descript more '
  ),
);

$nResults = filter_by_value($results, 'fullname', 'First1 Name1'); 
echo '<pre>'; 
print_r($nResults); 
echo '</pre>'; 


Array
(
    [0] => Array
        (
            [a] => U/H
            [b] => Modification
            [date] => 02/11/11
            [h] => 17:15:13
            [fullname] => First1 Name1
            [desc] => Descript 
        )

    [1] => Array
        (
            [a] => J/M
            [b] => 
            [date] => 03/11/11
            [h] => 17:18:27
            [fullname] => First1 Name1
            [desc] => Descript more 
        )

)

For Now I want to filter that the function can filter by start_date and end_date

so I change the

function filter_by_value ($array, $index, $value,$start_date,$end_date)

Anyone could How can I filter in the period of date?

4
  • The most obvious way to go about this would be a loop. Have you tried that, or anything else yet? Commented Nov 2, 2011 at 21:31
  • yes I am trying :) with array-filter php but not yet got Commented Nov 2, 2011 at 21:32
  • OK, if you could post whatever you have tried, I'll be happy to look at the code and see if there is something you are missing. I see your edit but that is a different function, right? It does not accept the parameters you are looking for. Commented Nov 2, 2011 at 21:37
  • yes I have update to more clear,thanks Commented Nov 2, 2011 at 21:58

1 Answer 1

1

I always convert my time data to timestamps, so you can easily calculate everything, after converting to a timestamp, it's just basic mathematics.

Your new function wil look something like this:

<?php 
function filter_by_date($array, $index, $start_date, $end_date) {
      $newarray = array();
      if(is_array($array) && count($array)>0)  
        { 
            $start_time = strtotime($start_date); //make sure your date is written as  
            $end_time = strtotime($end_date);     // dd/mm/YYYY, will also work with           
                                                 //hours etc

            foreach(array_keys($array) as $key){ 
                $temp[$key] = strtotime($array[$key][$index]); //$index will be date

                if ($temp[$key] >= $start_time && $temp[$key] <= $end_time ){ 
                    $newarray[$key] = $array[$key]; 
                } 
            } 
          } 
      return $newarray; 
}

?>

For other notations etc, best check the strtotime() page on php.net! http://www.php.net/manual/en/function.strtotime.php

Off course you can also create it in the same function, so you don't have to write more functions:

if ($index == ('date' || 'h')) {
   //the content of filter by date, or the function itself
} else {
   //the content of your first function
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, made some mistakes, did not test. Also, be sure to define your newarray as array before you filter, otherwise your print_r will indeed tell you that you didn't give an array if nothing comes out of your filter. I tested it this time, and it works.
By the way, it looks like your YY notation does also work. If you check out the link I gave you, you can find almost everything you can ask the strtotime() function.

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.