Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP: getting in between timestamps data from an array
  2. echo"<pre>";print_r($array1);echo"</pre>";
  3.        
  4. Array
  5.     (
  6.         [0] => stdClass Object
  7.             (
  8.                 [id] => 4d6f6aec35993704d52c0d9d
  9.                 [createdAt] => 1299147500
  10.                 [place] => stdClass Object
  11.                     (
  12.                         [id] => 4adcda40f964a5208a3e21e3
  13.                     )
  14.  
  15.  
  16.             )
  17.  
  18.                 [1] => stdClass Object
  19.             (
  20.                 [id] => 654jk654n646g54j6kl54j645
  21.                 [createdAt] => 1299147500
  22.                 [place] => stdClass Object
  23.                     (
  24.                         [id] => 4gh543gh5h5g354h3gg53gh
  25.                     )
  26.  
  27.  
  28.             )
  29. .
  30. .
  31. .
  32.        
  33. $array2 = array();
  34.  
  35. $begin = strtotime("2011-02-17 12:22:49");
  36. $end = strtotime("2011-03-03 10:00:00");
  37.  
  38. foreach($array1 as $timestamp){
  39.     if($timestamp <= $end && $timestamp >= $begin){
  40.                 $array2[] = $timestamp;
  41.     }
  42.  
  43. }
  44.        
  45. $begin = strtotime("2011-02-17 12:22:49");
  46. $end   = strtotime("2011-03-03 10:00:00");
  47.  
  48. $array2 = array_map(function($item) {
  49.     return $item->place->id;
  50. }, array_filter($array1, function($item) use($begin, $end) {
  51.     return $item->createdAt >= $begin && $item->createdAt <= $end;
  52. }));
  53.        
  54. $begin = strtotime("2011-02-17 12:22:49");
  55. $end = strtotime("2011-03-03 10:00:00");
  56.  
  57. $array2 = array_filter($array1, create_function('$o', "return $o->createdAt >= $begin && $o->createdAt <= $end;"));
  58. $array2 = array_map(create_function('$o', 'return $o->place->id;'), $array2);
  59.        
  60. $array2 = array();
  61.  
  62. $begin = strtotime("2011-02-17 12:22:49");
  63. $end = strtotime("2011-03-03 10:00:00");
  64.  
  65. foreach($array1 as $item){
  66.     $timestamp = $item->createdAt;
  67.     if($timestamp <= $end && $timestamp >= $begin){
  68.                 $array2[] = $item->place;
  69.     }
  70.  
  71. }
  72.        
  73. echo '<pre>' . print_r($array2, 1) . '</pre>';