Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] format daty w klasie pesel
Forum PHP.pl > Forum > Przedszkole
neurogen
znalazłem działającą klasę do odczytywania daty urodzenia z nr pesel, ale format daty mam:dd-m-rrrr a chciałbym mieć w miesiący poprzedzające 0 np.21-03-2018. jak to zrobić? nie widzę miejsca w kodzie gdzie mógłbym to poprawić:

  1. <?php
  2. class Pesel
  3. {
  4. /**
  5. * String containing polish national identification number.
  6. *
  7. * @var string
  8. */
  9. protected $peselString;
  10.  
  11. /**
  12. * Day of birth.
  13. *
  14. * @var int
  15. */
  16. protected $dayOfBirth;
  17.  
  18. /**
  19. * Month of birth.
  20. *
  21. * @var int
  22. */
  23. protected $monthOfBirth;
  24.  
  25. /**
  26. * Year of birth.
  27. *
  28. * @var int
  29. */
  30. protected $yearOfBirth;
  31.  
  32. /**
  33. * Sex.
  34. *
  35. * 0 = male, 1 = female
  36. *
  37. * @var int
  38. */
  39. protected $sex;
  40.  
  41. /**
  42. * Creates PESEL object and validates it's contents.
  43. *
  44. * @param string $peselString String containing elevel digits of the PESEL number.
  45. * @return Pesel
  46. */
  47. public function __construct($peselString) {
  48. $this->peselString = $peselString;
  49. $this->validate();
  50. }
  51.  
  52. /**
  53. * Validates PESEL number;
  54. *
  55. * @return void
  56. */
  57. protected function validate() {
  58. if(!preg_match('~^[\d]{11}$~', $this->peselString)) {
  59. throw new Exception('pesel powinien zawierać 11 znaków');
  60. }
  61. $this->sex = (int)(substr($this->peselString, 9, 1) % 2 === 0);
  62. $this->dayOfBirth = (int)substr($this->peselString, 4, 2);
  63. $this->monthOfBirth = (int)substr($this->peselString, 2, 2);
  64. $century = 1900;
  65. if($this->monthOfBirth > 12) {
  66. switch($this->monthOfBirth[0]) {
  67. case 2:
  68. case 3:
  69. $century = 2000;
  70. $this->monthOfBirth -= 20;
  71. break;
  72. case 4:
  73. case 5:
  74. $century = 2100;
  75. $this->monthOfBirth -= 40;
  76. break;
  77. case 6:
  78. case 7:
  79. $century = 2200;
  80. $this->monthOfBirth -= 60;
  81. break;
  82. case 8:
  83. case 9:
  84. $century = 1800;
  85. $this->monthOfBirth -= 80;
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91.  
  92. if($this->monthOfBirth == 0 || $this->monthOfBirth > 12) {
  93. throw new Exception('nieprawidłowa data urodzenia');
  94. }
  95.  
  96. $this->yearOfBirth = ((int)substr($this->peselString, 0, 2)) + $century;
  97.  
  98. if(!checkdate($this->monthOfBirth, $this->dayOfBirth, $this->yearOfBirth)) {
  99. throw new Exception('nieprawidłowa data urodzenia');
  100. }
  101.  
  102. $p = $this->peselString;
  103. $crc =
  104. 1 * $p[0] +
  105. 3 * $p[1] +
  106. 7 * $p[2] +
  107. 9 * $p[3] +
  108. 1 * $p[4] +
  109. 3 * $p[5] +
  110. 7 * $p[6] +
  111. 9 * $p[7] +
  112. 1 * $p[8] +
  113. 3 * $p[9];
  114. $crc = 10 - ((int) substr($crc, -1));
  115. if($crc != $p[10]) {
  116. throw new Exception('nieprawidłowa suma kontrolna');
  117. }
  118. }
  119.  
  120. /**
  121. * Returns calculated year of birth.
  122. *
  123. * @return int Day of birth.
  124. */
  125. public function getYearOfBirth() {
  126. return $this->yearOfBirth;
  127. }
  128.  
  129. /**
  130. * Returns calculated month of birth.
  131. *
  132. * @return int Month of birth.
  133. */
  134. public function getMonthOfBirth() {
  135. return $this->monthOfBirth;
  136. }
  137.  
  138. /**
  139. * Returns calculated day of birth.
  140. *
  141. * @return int Day of birth.
  142. */
  143. public function getDayOfBirth() {
  144. return $this->dayOfBirth;
  145. }
  146.  
  147. /**
  148. * Checks if given person is male.
  149. *
  150. * @return bool True if male, false otherwise.
  151. */
  152. public function isMale() {
  153. return $this->sex === 0;
  154. }
  155.  
  156. /**
  157. * Checks if given person is female.
  158. *
  159. * @return bool True if female, false otherwise.
  160. */
  161. public function isFemale() {
  162. return $this->sex === 1;
  163. }
  164.  
  165. /**
  166. * Returns PESEL number.
  167. *
  168. * @return string PESEL number.
  169. */
  170. public function getPesel() {
  171. return $this->peselString;
  172. }
  173. }
  174.  
Tomplus
Skorzystaj z funkcji:

  1. $month = 3
  2. echo str_pad($month, 2, '0', STR_PAD_LEFT); //03
  3. $month = 12
  4. echo str_pad($month, 2, '0', STR_PAD_LEFT); //12
Pyton_000
Ja bym chyba dodał jeszcze 1 metodę do tego:

  1. public function getBirthDate($format = 'd-m-Y')
  2. {
  3. return (new DateTime())
  4. ->setDate(
  5. $this->yearOfBirth,
  6. $this->monthOfBirth,
  7. $this->dayOfBirth
  8. )
  9. ->format($format);
  10. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.