Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Generare CNP

·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views
```// Generare CNP

```

2000 && !\$rezident && !\$strain)  \$sex = \$sex + 4;
//daca este rezident
if (\$rezident) \$sex = \$sex+6;
\$luna=rand(1,12);
\$zi = rand(1,31);
//daca este un copil care s-a nascut anul acesta atunci avem grija ca data generata sa nu fie mai mare decat data de azi
if (\$an == date('Y')) {
\$luna = rand(1,date('m'));
\$ziua = rand(1,date('d'));
}

\$date = new DateTime(\$an.'-'.\$luna.'-'.\$zi);

\$judet=rand(1,52);
\$judet=(\$judet<10) ? '0'.\$judet : \$judet;
\$control = rand(1,999);
if (\$control<10) {
\$control='00'.\$control;
}
else if (\$control<100) {
\$control='0'.\$control;
}
\$cnp =  \$sex . \$date->format("ymd") . \$judet.\$control;
\$suma=0;
for(\$i=0;\$i<=11;\$i++) {
\$suma+=\$sirCiudat[\$i]*\$cnp[\$i];
}
\$rest = \$suma%11;
\$cifraControl = (\$rest==10) ? 1 : \$rest;
\$cnp.=\$cifraControl;
if (strlen(\$cnp)!=13 ) {
return \$errorMsg;
}

else return \$cnp;

}

for(\$i=0;\$i<100;\$i++) {
echo generareCnp(rand(1,2),rand(0,209))."\n";
}

?>
``````
Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.