Linux bash script recipes
19. 9. 2022
Pre vytvorenie náhodného rodného čísla kliknite na tlačidlo "Vygenerovať rodné číslo", prípadne nastavte jeho parametre.
/**
* Validator rodnych cisel pre SR/ČR
* Validator for birth numbers in Slovak / Czech Rep.
*/
class RodneCislo
{
const
// gender constants
GENDER_MALE = 'M',
GENDER_FEMALE = 'F';
protected
/** @var string validated birth number */
$rc,
/** @var string birth year */
$year,
/** @var string birth month */
$month,
/** @var string birth day */
$day,
/** @var bool TRUE when birth number valid */
$bValid,
/** @var string detected gender, M - male, F - female */
$gender;
/**
* Constructor
* @param string $birthid e.g. "8010107182" or with slash "801010 / 7182"
*/
function __construct($birthid)
{
// make birth number format tolerant
$birthid = $this->rc = str_replace(['/','-', ' '], ['','',''], $birthid);
$this->bValid = true;
if (ctype_digit($birthid)) {
$len = strlen($birthid);
if($len == 9){
// old format before 01.01.1954 -> 9-miestne without validation
$this->year = substr($birthid, 0, 2);
$this->month = substr($birthid, 2, 2);
$this->day = substr($birthid, 4, 2);
if($this->year >= 54){
$this->bValid = false;
} else {
if($this->month >= 51 && $this->month <= 62){
$this->gender = self::GENDER_FEMALE;
$this->month -= 50;
}else{
$this->gender = self::GENDER_MALE;
}
$this->year = "19{$this->year}";
if (!checkdate($this->month, $this->day, $this->year)) {
$this->bValid = false;
}
}
}elseif($len == 10){
// new format from 01.01.1954 - with validation via modulo 11
$this->year = substr($birthid, 0, 2);
$this->month = substr($birthid, 2, 2);
$this->day = substr($birthid, 4, 2);
// note: v CR sa od 01.01.2004 moze ku mesiacu pripocitat +20 alebo +70
// https://phpfashion.com/jak-overit-platne-ic-a-rodne-cislo
if($this->month >= 51 && $this->month <= 62){
$this->gender = self::GENDER_FEMALE;
$this->month -= 50;
}else{
$this->gender = self::GENDER_MALE;
}
if(date("y") >= $this->year){
$this->year = "20{$this->year}";
}else{
$this->year = "19{$this->year}";
}
if ($this->year < 1954 || !checkdate($this->month, $this->day, $this->year)) {
$this->bValid = false;
} else {
$check2 = $birthid / 11;
$check = floor($check2);
if($check != $check2){
$this->bValid = false;
}
}
}else{
$this->bValid = false;
}
} else {
$this->bValid = false;
}
if ($this->bValid) {
$this->month = str_pad($this->month, 2, '0', STR_PAD_LEFT);
$this->day = str_pad($this->day, 2, '0', STR_PAD_LEFT);
}
}
/**
* Return TRUE when birth number is valid
*/
function isValid()
{
return $this->bValid;
}
/**
* Return birth number formatted
* @param string $delimiter e.g. "/" or " / "
*/
function getFormatted($delimiter = '')
{
if($this->rc){
$rc = $this->rc; // without slashes, only numbers, e.g. 8010107182
if($delimiter && preg_match('/^[ \/\-]+$/', $delimiter)){
$rc = substr($rc, 0, 6).$delimiter.substr($rc, 6);
}
return $rc;
}
}
/**
* Return formatted birth number e.g. 31.12.2010
* @param string $delimiter for date parts e.g. "." or "/"
*/
function asDate($delimiter = '.')
{
if ($this->bValid) {
return $this->day.$delimiter.$this->month.$delimiter.$this->year;
}
}
/**
* Return parsed info for birth number
*/
function getDetails()
{
$a = [];
if ($this->bValid) {
$a = [
'year' => (int) $this->year,
'month' => (int) $this->month,
'day' => (int) $this->day,
'gender' => $this->gender,
];
}
return $a;
}
/**
* Return real age calculated towards supplied timestamp
* @param int $targetTs Timestamp
* @param string $mode year"empty string - calculate age by year or exact day
*/
function getAge($targetTs = 0, $mode = '')
{
if ($this->bValid) {
if ($targetTs === 0) {
$targetTs = time();
}
if (!checkdate(date("m", $targetTs), date("d", $targetTs), date("Y", $targetTs))){
return false;
}else{
if($mode == "year") {
// age as a difference between years (current year - birth year),
// regardless of exact birth day
return intval(date("Y",$targetTs) - $this->year);
}else {
// age calculated exactly by birth day
// e.g. 20200101 - 19900101 = intval(369108 / 10000) = 30
$birthdate = intval($this->year.$this->month.$this->day);
return intval((date("Ymd",$targetTs) - $birthdate) / 10000);
}
}
}else{
return false;
}
}
/**
* Return gender detected from birth number
*/
function getGender()
{
return $this->gender;
}
/**
* Return whether birth number belongs to woman
*/
function isFemale()
{
return self::GENDER_FEMALE == $this->gender;
}
/**
* Return timestamp for birth number
*/
function getBirthTime()
{
return mktime(0,0,0, intval($this->month), intval($this->day), intval($this->year));
}
}
Máte otázku?
Profesionálna tvorba webových aplikácií a riešení na mieru. Konzultačné služby.