Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] płeć z numeru pesel
Forum PHP.pl > Forum > Przedszkole
dentopolis
  1. <?php
  2.  
  3. /**
  4.  * Class that validate and retrieve data from PESEL number in object-oriented style
  5.  *
  6.  * Example:
  7.  * $pesel = new Pesel('85042510356');
  8.  * $pesel->getBirthday()->format("d-m-Y")
  9.  * $pesel->getSex()
  10.  * $pesel->getAge("%a")
  11.  *
  12.  *
  13.  * @author Bartosz 'B3k' Pietrzak
  14.  * @license MIT
  15.  */
  16. class Pesel {
  17.  
  18. private static $scales = array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3);
  19. private $pesel, $birthday, $sex, $vaild;
  20.  
  21. /**
  22.   * Constructor
  23.   *
  24.   * @throws InvalidArgumentException
  25.   * @param string $pesel
  26.   * @return void
  27.   */
  28. public function __construct($pesel) {
  29. if (!is_numeric($pesel) || strlen($pesel) != 11 || $pesel == '00000000000') {
  30. throw new InvalidArgumentException("Invaild PESEL number");
  31. } else {
  32. $this->pesel = $pesel;
  33. $this->extract();
  34. }
  35. }
  36.  
  37. /**
  38.   * Return actual PESEL number
  39.   *
  40.   * @return string
  41.   */
  42. public function getPesel() {
  43. return $this->pesel;
  44. }
  45.  
  46. /**
  47.   * Return birthday DateTime object or date in format as parameter
  48.   *
  49.   * Example:
  50.   * $pesel->getBirthday("Y-m-d");
  51.   * $pesel->getBirthday()->format('Y-m-d')
  52.   *
  53.   * @see <a href="http://www.php.net/manual/en/datetime.format.php" target="_blank">http://www.php.net/manual/en/datetime.format.php</a>
  54.   * @param bool|string $format
  55.   * @return Mixed
  56.   */
  57. public function getBirthday($format = FALSE) {
  58. return ($format ? $this->birthday->format($format) : $this->birthday);
  59. }
  60.  
  61. /**
  62.   * Return diff between date from pesel and current time
  63.   * as DateInterval object or string in typed format
  64.   *
  65.   * Example:
  66.   * $pesel->getAge('%a days');
  67.   * $pesel->getAge()->format("%y years");
  68.   *
  69.   * @see <a href="http://www.php.net/manual/en/dateinterval.format.php" target="_blank">http://www.php.net/manual/en/dateinterval.format.php</a>
  70.   * @params string
  71.   * @return DateInterval
  72.   */
  73. public function getAge($format = FALSE) {
  74. $interval = $this->birthday->diff(new DateTime("now"));
  75. return ($format ? $interval->format($format) : $interval);
  76. }
  77.  
  78. /**
  79.   * Return sex: 1 - female , 2 - male
  80.   *
  81.   * @return int
  82.   */
  83. public function getSex() {
  84. return $this->sex;
  85. }
  86.  
  87. /**
  88.   * Return that if pesel checksum control is vaild.
  89.   *
  90.   * @return bool
  91.   */
  92. public function isVaild() {
  93. return $this->vaild;
  94. }
  95.  
  96. /**
  97.   * Extract data from PESEL
  98.   *
  99.   * @return void
  100.   */
  101. private function extract() {
  102. if (($this->pesel [9] % 2) == 0) {
  103. $this->sex = 1;
  104. } else {
  105. $this->sex = 2;
  106. }
  107. if (intval($this->pesel [2] . $this->pesel [3]) >= 81 && intval($this->pesel [2] . $this->pesel [3]) <= 92) {
  108. $month = (($this->pesel [2] . $this->pesel [3]) - 80);
  109. $year = (($this->pesel [0] . $this->pesel [1])) + 1800;
  110. } elseif (intval($this->pesel [2] . $this->pesel [3]) >= 1 && intval($this->pesel [2] . $this->pesel [3]) <= 12) {
  111. $month = ($this->pesel [2] . $this->pesel [3]);
  112. $year = (($this->pesel [0] . $this->pesel [1])) + 1900;
  113. } elseif (intval($this->pesel [2] . $this->pesel [3]) >= 21 && intval($this->pesel [2] . $this->pesel [3]) <= 32) {
  114. $month = (intval($this->pesel [2] . $this->pesel [3]) - 20);
  115. $year = (intval($this->pesel [0] . $this->pesel [1])) + 2000;
  116. } else {
  117. throw new InvalidArgumentException("Invaild PESEL number - birthday part out of range");
  118. }
  119. if (!checkdate($month, intval($this->pesel [4] . $this->pesel [5]), $year)) {
  120. throw new InvalidArgumentException("Invaild PESEL number - birthday part is invaild");
  121. }
  122. $this->birthday = new DateTime($year . "-" . $month . "-" . intval($this->pesel [4] . $this->pesel [5]));
  123. $sum = 0;
  124. for ($x = 0; $x < 10; $x++) {
  125. $sum += self::$scales [$x] * $this->pesel [$x];
  126. }
  127. $res = 10 - $sum % 10;
  128. $res = ($res == 10 ? 0 : $res);
  129. if ($res == $this->pesel [10]) {
  130. $this->vaild = TRUE;
  131. } else {
  132. $this->vaild = FALSE;
  133. }
  134. }
  135.  
  136. }
  137.  
  138.  
  139.  
  140. $pesel = new Pesel('08241305193');
  141. $dataurodzenia=$pesel->getBirthday()->format("d-m-Y");
  142. $plec=$pesel->getSex();
  143. $wiek=$pesel->getAge("%y");
  144.  
  145. echo $dataurodzenia;
  146. echo $plec;
  147. echo $wiek;
nospor
A pytanie to?
dentopolis
jak wyświetlić płeć?
mortus
Przecież wyświetlasz płeć jako liczbę 1 lub 2. Jeśli chcesz wyświetlić tekst, to zmień funkcję getSex() i zastosuj prostego ifa, w którym sprawdzisz czy $this->sex to 1, czy dwa i zwrócisz (return) odpowiednio "female" lub "male".
dentopolis
działa. może się komuś przyda

  1. if ($pesel->getSex() == '1') {
  2. echo "K";
  3. }
  4. else {
  5. echo "M";
  6. }
mortus
Jeśli porównujesz liczby, to nie używaj cudzysłowów i apostrofów. W tym przypadku wystarczy po prostu 1 zamiast '1'.

PS. Może warto byłoby tę funkcjonalność umieścić w klasie Pesel?
nospor
Cytat
PS. Może warto byłoby tę funkcjonalność umieścić w klasie Pesel?

@Mortus zewnetrznych klas nie powinno sie modyfikowac
mortus
Zgadzam się. Nie zwróciłem uwagi, że to klasa z zewnątrz.
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.