常用的php用身份证号码计算年龄、计算时间戳等方法!
//18位身份证取性别,倒数第二位奇数是男,偶数是女;
s
e
x
=
s
u
b
s
t
r
(
sex = substr(
sex=substr(idcard, (strlen($idcard)==18 ? -2 : -1), 1) % 2 ? ‘1’ : ‘2’;
//取身份证年月日;
b
i
r
t
h
d
a
y
=
s
t
r
l
e
n
(
birthday = strlen(
birthday=strlen(idcard)==15 ? (‘19’ . substr(
i
d
c
a
r
d
,
6
,
6
)
)
:
s
u
b
s
t
r
(
idcard, 6, 6)) : substr(
idcard,6,6)):substr(idcard, 6, 8);
//身份证年月日转换成时间戳
b
i
r
t
h
d
a
y
s
=
s
t
r
t
o
t
i
m
e
(
s
t
r
l
e
n
(
birthdays = strtotime(strlen(
birthdays=strtotime(strlen(idcard)==15 ? (‘19’ . substr(
i
d
c
a
r
d
,
6
,
6
)
)
:
s
u
b
s
t
r
(
idcard, 6, 6)) : substr(
idcard,6,6)):substr(idcard, 6, 8));
//取当天日期;
$today = strtotime(‘today’);
//用时间戳相减算出年龄;
d
i
f
f
=
f
l
o
o
r
(
(
diff = floor((
diff=floor((today-$birthdays)/86400/365);
//取出年龄值;
a
g
e
=
s
t
r
t
o
t
i
m
e
(
s
u
b
s
t
r
(
age = strtotime(substr(
age=strtotime(substr(idcard,6,8).’+’.
d
i
f
f
.
′
y
e
a
r
s
′
)
>
diff.'years')>
diff.′years′)>today?(
d
i
f
f
+
1
)
:
diff+1):
diff+1):diff;