Skip to content

Repository files navigation

personnummer GitHub Workflow StatusPackagist VersionPHP from Packagist

Validate Swedish social security numbers.

Installation

composer require personnummer/personnummer

Methods

Static

MethodArgumentsReturns
parsestring personnummer, [ array options* ]Instance
validstring personnummer, [ array options* ]bool

Instance

MethodArgumentsReturns
formatbool longFormatstring
getAgenoneint
isMalenonebool
isFemalenonebool
isCoordinationNumbernonebool
isInterimNumbernonebool
getDatenoneDateTime
PropertyTypeDescription
centurystringCentury, two digits
yearstringYear, two digits
fullYearstringYear, four digits
monthstringMonth, two digits
daystringDay, two digits
sepstringSeparator (-/+)
numstringSuffix number, three digits
checkstringLuhn check digit, one digit

Errors

When a personnummer is invalid a PersonnummerException is thrown.

Options

OptionTypeDefaultDescription
allowCoordinationNumberbooltrueAccept coordination numbers
allowInterimNumberboolfalseAccept interim/T numbers
clockPsr\Clock\ClockInterfacePersonummer\SystemClockClock used for time calculations

Examples

Validation

usePersonnummer\Personnummer;
Personnummer::valid(1212121212);
//=> true
Personnummer::valid('20121212-1212');
//=> true

Format

usePersonnummer\Personnummer;
// Short format (YYMMDD-XXXX)
(newPersonnummer(1212121212))->format();
//=> 121212-1212// Short format for 100+ years old
(newPersonnummer('191212121212'))->format();
//=> 121212+1212// Long format (YYYYMMDDXXXX)
(newPersonnummer('1212121212'))->format(true);
//=> 201212121212

Get Age

usePersonnummer\Personnummer;
(newPersonnummer('1212121212'))->getAge();
//=> 7

Get Sex

usePersonnummer\Personnummer;
(newPersonnummer('1212121212'))->isMale();
//=> true
(newPersonnummer('1212121212'))->isFemale();
//=> false

See PersonnummerTest.php for more examples.

License

MIT

About

Validate Swedish personal identity numbers

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

34 stars

Watchers

4 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages