SHARE
TWEET

Untitled

a guest Jan 12th, 2017 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $data = [
  2.     (object) ['myDate' => '2017-01-12'],
  3.     (object) ['myDate' => '2016-12-25'],
  4.     (object) ['myDate' => '2017-01-01'],
  5. ];
  6.  
  7. function dateSort($a, $b) {
  8.     return $a->myDate <=> $b->myDate;
  9. }
  10.  
  11. usort($data, 'dateSort');
  12. var_dump($data);
  13.    
  14. $data = [
  15.     (object) ['myDate' => '2017-01-12'],
  16.     (object) ['myDate' => '2016-12-25'],
  17.     (object) ['myDate' => '2017-01-01'],
  18. ];
  19.  
  20. function dateSort($a, $b) {
  21.     return $a->myDate <=> $b->myDate;
  22. }
  23.  
  24. usort($data, function($a, $b) { return $a->myDate <=> $b->myDate; });
  25. var_dump($data);
  26.    
  27. $data = [
  28.     (object) ['myDate' => '2017-01-12'],
  29.     (object) ['myDate' => '2016-12-25'],
  30.     (object) ['myDate' => '2017-01-01'],
  31. ];
  32.  
  33. function dateSort($a, $b) {
  34.     return abs(strtotime('2016-12-31') - strtotime($a->myDate)) <=> abs(strtotime('2016-12-31') - strtotime($b->myDate));
  35. }
  36.  
  37. usort($data, 'dateSort');
  38. var_dump($data);
  39.    
  40. $data = [
  41.     (object) ['myDate' => '2017-01-12'],
  42.     (object) ['myDate' => '2016-12-25'],
  43.     (object) ['myDate' => '2017-01-01'],
  44. ];
  45.  
  46.  
  47. $pivotDate = '2017-01-06';
  48. usort(
  49.     $data,
  50.     function ($a, $b) use ($pivotDate){
  51.         return abs(strtotime($pivotDate) - strtotime($a->myDate)) <=> abs(strtotime($pivotDate) - strtotime($b->myDate));
  52. });
  53. var_dump($data);
RAW Paste Data
Top