|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Utils; |
|
4
|
|
|
|
|
5
|
|
|
class Date |
|
6
|
|
|
{ |
|
7
|
|
|
public static function validatePersonCode($personCode) |
|
8
|
|
|
{ |
|
9
|
|
|
$personCode = str_replace('-', '', $personCode); |
|
10
|
|
|
$result = true; |
|
11
|
|
|
|
|
12
|
|
|
if (\strlen($personCode) !== 11 || preg_match('/^\d+$/', $personCode) === null) { |
|
13
|
|
|
$result = false; |
|
14
|
|
|
} elseif (((int)substr($personCode, 0, 2) === 32 && !self::newPKValidate($personCode)) || !self::validateDate($personCode)) { |
|
15
|
|
|
$result = false; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
return $result; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function validateDate($date) |
|
22
|
|
|
{ |
|
23
|
|
|
$date = str_replace('-', '', $date); |
|
24
|
|
|
$day = (int)substr($date, 0, 2); |
|
25
|
|
|
$month = (int)substr($date, 2, 2); |
|
26
|
|
|
|
|
27
|
|
|
if ($month < 0 || $month > 12) { |
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
// @formatter:off |
|
31
|
|
|
$months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
|
32
|
|
|
// @formatter:on |
|
33
|
|
|
if ((int)substr($date, 4, 2) % 4 === 0) { |
|
34
|
|
|
$months[1] = 29; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $day > 0 && $day <= $months[$month - 1]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function validateDate2($date, $format = 'd-m-Y H:i:s') |
|
41
|
|
|
{ |
|
42
|
|
|
$object = \DateTime::createFromFormat($format, $date); |
|
43
|
|
|
|
|
44
|
|
|
return $object && $object->format($format) === $date; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function excelDate($timestamp, $format = 'd-m-Y H:i:s') |
|
48
|
|
|
{ |
|
49
|
|
|
$base = 25569; |
|
50
|
|
|
if ($timestamp >= $base) { |
|
51
|
|
|
$unix = ($timestamp - $base) * 86400; |
|
52
|
|
|
$date = gmdate($format, $unix); |
|
53
|
|
|
if (self::validateDate2($date, $format)) { |
|
54
|
|
|
return $date; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $timestamp; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function newPKValidate($personCode) |
|
62
|
|
|
{ |
|
63
|
|
|
$personCode = str_replace('-', '', $personCode); |
|
64
|
|
|
|
|
65
|
|
|
// @formatter:off |
|
66
|
|
|
$calculations = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; |
|
67
|
|
|
// @formatter:on |
|
68
|
|
|
|
|
69
|
|
|
$sum = 0; |
|
70
|
|
|
foreach ($calculations as $key => $calculation) { |
|
71
|
|
|
$sum += ($personCode[$key] * $calculation); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$remainder = $sum % 11; |
|
75
|
|
|
|
|
76
|
|
|
if (1 - $remainder < -1) { |
|
77
|
|
|
return $personCode[10] === (1 - $remainder + 11); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $personCode[10] === (1 - $remainder); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|