code_junkie

Order dates by upcoming

Nov 11th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. $a = ([0]=>"1980-04-14", [1]=>"2007-06-08",
  2. [2]=>"2008-12-25", [3]=>"1978-11-03")
  3.  
  4. $a = ([0]=>"1978-11-03", [1]=>"2008-12-25",
  5. [2]=>"1980-04-14", [3]=>"2007-06-08")
  6.  
  7. function cmp($a, $b)
  8. {
  9. $a_tmp = split("-", $a);
  10. $b_tmp = split("-", $b);
  11. return strcmp($a_tmp[1], $b_tmp[1]);
  12. }
  13.  
  14. function relative_year_day($date) {
  15. $value = date('z', strtotime($date)) - date('z');
  16.  
  17. if ($value < 0)
  18. $value += 365;
  19.  
  20. return $value;
  21. }
  22.  
  23. function cmp($a, $b)
  24. {
  25. $aValue = relative_year_day($a);
  26. $bValue = relative_year_day($b);
  27.  
  28. if ($aValue == $bValue)
  29. return 0;
  30.  
  31. return ($aValue < $bValue) ? -1 : 1;
  32. }
  33.  
  34. $a = array("1980-04-14", "2007-06-08",
  35. "2008-12-25", "1978-11-03");
  36.  
  37. usort($a, "cmp");
  38.  
  39. #!/bin/perl -w
  40.  
  41. # Sort sequence of dates by next occurrence of anniversary.
  42. # Today's "birthdays" count as low (will appear first in sequence)
  43.  
  44. use strict;
  45.  
  46. my $refdate = "2008-10-05";
  47.  
  48. my @list = (
  49. "1980-04-14", "2007-06-08",
  50. "2008-12-25", "1978-11-03",
  51. "2008-10-04", "2008-10-05",
  52. "2008-10-06", "2008-02-29"
  53. );
  54.  
  55. sub date_on_or_after
  56. {
  57. my($actdate, $refdate) = @_;
  58. my($answer) = $actdate;
  59. if ($actdate lt $refdate) # String compare OK with ISO8601 format
  60. {
  61. my($act_yy, $act_mm, $act_dd) = split /-/, $actdate;
  62. my($ref_yy, $ref_mm, $ref_dd) = split /-/, $refdate;
  63. $ref_yy++ if ($act_mm < $ref_mm || ($act_mm == $ref_mm && $act_dd < $ref_dd));
  64. $answer = "$ref_yy-$act_mm-$act_dd";
  65. }
  66. return $answer;
  67. }
  68.  
  69. sub anniversary_compare
  70. {
  71. my $r1 = date_on_or_after($a, $refdate);
  72. my $r2 = date_on_or_after($b, $refdate);
  73. return $r1 cmp $r2;
  74. }
  75.  
  76. my @result = sort anniversary_compare @list;
  77.  
  78. print "Before:n";
  79. print "* $_n" foreach (@list);
  80. print "Reference date: $refdaten";
  81. print "After:n";
  82. print "* $_n" foreach (@result);
  83.  
  84. Before:
  85. * 1980-04-14
  86. * 2007-06-08
  87. * 2008-12-25
  88. * 1978-11-03
  89. * 2008-10-04
  90. * 2008-10-05
  91. * 2008-10-06
  92. * 2008-02-29
  93. Reference date: 2008-10-05
  94. After:
  95. * 2008-10-05
  96. * 2008-10-06
  97. * 1978-11-03
  98. * 2008-12-25
  99. * 2008-02-29
  100. * 1980-04-14
  101. * 2007-06-08
  102. * 2008-10-04
  103.  
  104. function cmp($a, $b)
  105. {
  106. $a_tmp = explode("-", $a["date"]);
  107. $b_tmp = explode("-", $b["date"]);
  108. if ($a_tmp[1] < date("m"))
  109. {
  110. $a_tmp[1] += 12;
  111. }
  112. if ($b_tmp[1] < date("m"))
  113. {
  114. $b_tmp[1] += 12;
  115. }
  116. return strcmp($a_tmp[1] . $a_tmp[2], $b_tmp[1] . $b_tmp[2]);
  117. }
  118.  
  119. for ($i=0; $i<count($a); $i++){
  120. if ($currentTimestamp > $a[$i]){
  121. unset($a[$i]);
  122. }
  123. }
  124.  
  125. $a = array_combine(array_map('strtotime', $a), $a);
  126. ksort($a);
  127.  
  128. function dateCmp($date1, $date2) {
  129. return (strtotime($date1) > strtotime($date2))?1:-1;
  130. }
  131.  
  132. usort($a, 'dateCmp');
  133.  
  134. uasort($a, 'dateCmp');
  135.  
  136. $date1 = split("-", $a);
  137. $date2 = split("-", $b);
  138. $seconds1 = mktime(0,0,0,$date1[1],$date1[2],$date1[0]);
  139. $seconds2 = mktime(0,0,0,$date2[1],$date2[2],$date2[0]);
  140. // eliminate years
  141. $seconds1 %= 31536000;
  142. $seconds2 %= 31536000;
  143. return $seconds1 - $seconds2;
Add Comment
Please, Sign In to add comment