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ć:
<?php
class Pesel
{
/**
* String containing polish national identification number.
*
* @var string
*/
protected $peselString;
/**
* Day of birth.
*
* @var int
*/
protected $dayOfBirth;
/**
* Month of birth.
*
* @var int
*/
protected $monthOfBirth;
/**
* Year of birth.
*
* @var int
*/
protected $yearOfBirth;
/**
* Sex.
*
* 0 = male, 1 = female
*
* @var int
*/
protected $sex;
/**
* Creates PESEL object and validates it's contents.
*
* @param string $peselString String containing elevel digits of the PESEL number.
* @return Pesel
*/
public function __construct($peselString) {
$this->peselString = $peselString;
$this->validate();
}
/**
* Validates PESEL number;
*
* @return void
*/
protected function validate() {
if(!preg_match('~^[\d]{11}$~', $this->peselString)) { throw new Exception('pesel powinien zawierać 11 znaków');
}
$this->sex = (int
)(substr($this->peselString, 9
, 1
) % 2
=== 0
); $this->dayOfBirth = (int
)substr($this->peselString, 4
, 2
); $this->monthOfBirth = (int
)substr($this->peselString, 2
, 2
); $century = 1900;
if($this->monthOfBirth > 12) {
switch($this->monthOfBirth[0]) {
case 2:
case 3:
$century = 2000;
$this->monthOfBirth -= 20;
break;
case 4:
case 5:
$century = 2100;
$this->monthOfBirth -= 40;
break;
case 6:
case 7:
$century = 2200;
$this->monthOfBirth -= 60;
break;
case 8:
case 9:
$century = 1800;
$this->monthOfBirth -= 80;
break;
default:
break;
}
}
if($this->monthOfBirth == 0 || $this->monthOfBirth > 12) {
throw new Exception('nieprawidłowa data urodzenia');
}
$this->yearOfBirth = ((int
)substr($this->peselString, 0
, 2
)) + $century;
if(!checkdate($this->monthOfBirth, $this->dayOfBirth, $this->yearOfBirth)) { throw new Exception('nieprawidłowa data urodzenia');
}
$p = $this->peselString;
$crc =
1 * $p[0] +
3 * $p[1] +
7 * $p[2] +
9 * $p[3] +
1 * $p[4] +
3 * $p[5] +
7 * $p[6] +
9 * $p[7] +
1 * $p[8] +
3 * $p[9];
$crc = 10
- ((int
) substr($crc, -1
)); if($crc != $p[10]) {
throw new Exception('nieprawidłowa suma kontrolna');
}
}
/**
* Returns calculated year of birth.
*
* @return int Day of birth.
*/
public function getYearOfBirth() {
return $this->yearOfBirth;
}
/**
* Returns calculated month of birth.
*
* @return int Month of birth.
*/
public function getMonthOfBirth() {
return $this->monthOfBirth;
}
/**
* Returns calculated day of birth.
*
* @return int Day of birth.
*/
public function getDayOfBirth() {
return $this->dayOfBirth;
}
/**
* Checks if given person is male.
*
* @return bool True if male, false otherwise.
*/
public function isMale() {
return $this->sex === 0;
}
/**
* Checks if given person is female.
*
* @return bool True if female, false otherwise.
*/
public function isFemale() {
return $this->sex === 1;
}
/**
* Returns PESEL number.
*
* @return string PESEL number.
*/
public function getPesel() {
return $this->peselString;
}
}