You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Carbon\Carbon;
|
|
use InvalidArgumentException;
|
|
|
|
class ChineseIdCardHelper
|
|
{
|
|
public static function parseBirthdate(string $idCard): ?Carbon
|
|
{
|
|
$idCard = strtoupper(trim($idCard));
|
|
if (strlen($idCard) !== 18) {
|
|
return null;
|
|
}
|
|
if (! preg_match('/^\d{17}[\dX]$/', $idCard)) {
|
|
return null;
|
|
}
|
|
$ymd = substr($idCard, 6, 8);
|
|
try {
|
|
return Carbon::createFromFormat('Ymd', $ymd)->startOfDay();
|
|
} catch (InvalidArgumentException) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static function isBirthInRange(?Carbon $birth, $start, $end, string $tz): bool
|
|
{
|
|
if (! $birth) {
|
|
return false;
|
|
}
|
|
if (! $start && ! $end) {
|
|
return true;
|
|
}
|
|
$b = $birth->copy()->timezone($tz)->toDateString();
|
|
if ($start) {
|
|
$s = $start instanceof \Carbon\Carbon ? $start->toDateString() : (string) $start;
|
|
if ($b < $s) {
|
|
return false;
|
|
}
|
|
}
|
|
if ($end) {
|
|
$e = $end instanceof \Carbon\Carbon ? $end->toDateString() : (string) $end;
|
|
if ($b > $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|