Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. <?php
  2.  
  3. class Dates{
  4.  
  5. private $dayMonth;
  6. private $month;
  7. private $year;
  8.  
  9. public function __construct($valueDayMonth, $valueMonth, $valueYear){
  10. $this->set_dayMonth($valueDayMonth);
  11. $this->set_month($valueMonth);
  12. $this->set_year($valueYear);
  13. }
  14.  
  15. //Setters
  16.  
  17. public function set_dayMonth($new_dayMonth){
  18. if($new_dayMonth <= 0 || $new_dayMonth > 32){
  19. throw new Exception("You need to insert a day of month between 1 and 31");
  20. }
  21. $this->dayMonth = $new_dayMonth;
  22. }
  23.  
  24. public function set_month($new_month){
  25. if(is_numeric($new_month)){
  26. if($new_month < 1 || $new_month > 12){
  27. throw new Exception("You need to insert a month between 1 and 12");
  28. }
  29. }else{
  30. if ($new_month!="January" && $new_month!="February"
  31. && $new_month!="March" && $new_month!="April"
  32. && $new_month!="May" && $new_month!="June" && $new_month!="July"
  33. && $new_month!="August" && $new_month!="September" && $new_month!="October"
  34. && $new_month!="November" && $new_month!="December"){
  35. throw new Exception("Insert a valid month between January and December.");
  36. }
  37. }
  38. $this->month = $new_month;
  39. }
  40.  
  41. public function set_year($new_year){
  42. if($new_year < 1990 || $new_year > 2018){
  43. throw new Exception("You need to insert a year between 1990 and 2018");
  44. }
  45. $this->year = $new_year;
  46. }
  47.  
  48.  
  49. //Getters
  50.  
  51. public function get_dayMonth(){
  52. return $this->dayMonth;
  53. }
  54.  
  55. public function get_month(){
  56. return $this->month;
  57. }
  58.  
  59. public function get_year(){
  60. return $this->year;
  61. }
  62.  
  63. //Helper function.Format the prints.
  64.  
  65. //This function formats the date as MDY:
  66. public function printDateMDY(){
  67. $format = '%02d/%02d/%04d';
  68. return sprintf($format, $this->month, $this->dayMonth, $this->year);
  69. }
  70.  
  71. //This function formats the date as DMY:
  72. public function printDateDMY(){
  73. $format = '%02d/%02d/%04d';
  74. return sprintf($format, $this->dayMonth, $this->month, $this->year);
  75. }
  76.  
  77. //This function formats the date as Month D, Y:
  78. public function printDateMonthDY(){
  79. $format = '%s %02d, %04d';
  80. return sprintf($format, $this->month, $this->dayMonth, $this->year);
  81. }
  82.  
  83. //This function formats the date as D, Month, Year six months ahead:
  84. public function printDateDMYSixMonthsAhead(){
  85. $date = new DateTime($this->year."/".$this->month."/".$this->dayMonth);
  86. $date->add(new DateInterval('P6M'));
  87. return $date->format('d-m-Y');
  88. }
  89.  
  90. //This function formats the date as D, Month, Year three months ahead:
  91. public function printDateDMYThreeMonthsAhead(){
  92. $date = new DateTime($this->year."/".$this->month."/".$this->dayMonth);
  93. $date->add(new DateInterval('P3M'));
  94. return $date->format('d-m-Y');
  95. }
  96.  
  97. //This function formats the date as D, Month, Year nine months ahead:
  98. public function printDateDMYNineMonthsAhead(){
  99. $date = new DateTime($this->year."/".$this->month."/".$this->dayMonth);
  100. $date->add(new DateInterval('P9M'));
  101. return $date->format('d-m-Y');
  102. }
  103.  
  104. }
  105.  
  106. // $obj1 = new Dates(30, 10, 2016);
  107. // echo $obj1->printDateMDY() . "<br />";
  108.  
  109. // $obj2 = new Dates(25, 12, 2017);
  110. // echo $obj2->printDateDMY() . "<br />";
  111.  
  112. // $obj3 = new Dates(25, 'August', 2017);
  113. // echo $obj3->printDateMonthDY();
  114.  
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement