001.
<?php
002.
function
timespan(
$seconds
= 1,
$time
=
''
)
003.
{
004.
if
( !
is_numeric
(
$seconds
))
005.
{
006.
$seconds
= 1;
007.
}
008.
009.
if
( !
is_numeric
(
$time
))
010.
{
011.
$time
= time();
012.
}
013.
014.
if
(
$time
<=
$seconds
)
015.
{
016.
$seconds
= 1;
017.
}
018.
else
019.
{
020.
$seconds
=
$time
-
$seconds
;
021.
}
022.
023.
$str
=
''
;
024.
$years
=
floor
(
$seconds
/ 31536000);
025.
026.
if
(
$years
> 0)
027.
{
028.
$str
.=
$years
.
' ปี, '
;
029.
}
030.
031.
$seconds
-=
$years
* 31536000;
032.
$months
=
floor
(
$seconds
/ 2628000);
033.
034.
if
(
$years
> 0 OR
$months
> 0)
035.
{
036.
if
(
$months
> 0)
037.
{
038.
$str
.=
$months
.
' เดือน, '
;
039.
}
040.
041.
$seconds
-=
$months
* 2628000;
042.
}
043.
044.
$weeks
=
floor
(
$seconds
/ 604800);
045.
046.
if
(
$years
> 0 OR
$months
> 0 OR
$weeks
> 0)
047.
{
048.
if
(
$weeks
> 0)
049.
{
050.
$str
.=
$weeks
.
' สัปดาห์, '
;
051.
}
052.
053.
$seconds
-=
$weeks
* 604800;
054.
}
055.
056.
$days
=
floor
(
$seconds
/ 86400);
057.
058.
if
(
$months
> 0 OR
$weeks
> 0 OR
$days
> 0)
059.
{
060.
if
(
$days
> 0)
061.
{
062.
$str
.=
$days
.
' วัน, '
;
063.
}
064.
065.
$seconds
-=
$days
* 86400;
066.
}
067.
068.
$hours
=
floor
(
$seconds
/ 3600);
069.
070.
if
(
$days
> 0 OR
$hours
> 0)
071.
{
072.
if
(
$hours
> 0)
073.
{
074.
$str
.=
$hours
.
' ชั่วโมง, '
;
075.
}
076.
077.
$seconds
-=
$hours
* 3600;
078.
}
079.
080.
$minutes
=
floor
(
$seconds
/ 60);
081.
082.
if
(
$days
> 0 OR
$hours
> 0 OR
$minutes
> 0)
083.
{
084.
if
(
$minutes
> 0)
085.
{
086.
$str
.=
$minutes
.
' นาที, '
;
087.
}
088.
089.
$seconds
-=
$minutes
* 60;
090.
}
091.
092.
if
(
$str
==
''
)
093.
{
094.
$str
.=
$seconds
.
' วินาที'
;
095.
}
096.
097.
return
substr
(trim(
$str
), 0, -1);
098.
}
099.
100.
101.
102.
$birthdate
=
strtotime
(
'1973-11-13'
);
103.
$today
= time();
104.
105.
echo
timespan(
$birthdate
,
$today
);
106.