01.
error_reporting
(E_ALL);
02.
class
thai_datetime{
03.
var
$datetime
;
04.
function
thai_datetime(
$valuedate
= NULL) {
05.
$this
->set_datetime(
$valuedate
);
06.
}
07.
function
set_datetime(
$valuedate
) {
08.
if
(
empty
(
$valuedate
))
09.
$this
->datetime =
date
(
"Y-m-d H:i:s"
);
10.
else
11.
$this
->datetime =
$valuedate
;
12.
}
13.
function
cast_datetime(
$value
,
$type
) {
14.
$value
=
strtotime
(
$value
);
15.
switch
(
$type
) {
16.
case
"d"
:
return
date
(
"j"
,
$value
);
17.
case
"m"
:
return
date
(
"m"
,
$value
);
18.
case
"y"
:
return
date
(
"y"
,
$value
);
19.
case
"Y"
:
return
date
(
"Y"
,
$value
);
20.
case
"H"
:
return
date
(
"H"
,
$value
);
21.
case
"M"
:
return
date
(
"M"
,
$value
);
22.
case
"S"
:
return
date
(
"S"
,
$value
);
23.
}
24.
}
25.
function
short_month(
$m_val
= NULL) {
26.
if
(
empty
(
$m_val
))
27.
$m_val
=
$this
->cast_datetime(
$this
->datetime,
"m"
);
28.
switch
(
$m_val
) {
29.
case
1:
return
"ม.ค."
;
break
;
30.
case
2:
return
"ก.พ."
;
break
;
31.
case
3:
return
"มี.ค."
;
break
;
32.
case
4:
return
"เม.ย."
;
break
;
33.
case
5:
return
"พ.ค."
;
break
;
34.
case
6:
return
"มิ.ย."
;
break
;
35.
case
7:
return
"ก.ค."
;
break
;
36.
case
8:
return
"ส.ค."
;
break
;
37.
case
9:
return
"ก.ย."
;
break
;
38.
case
10:
return
"ต.ค."
;
break
;
39.
case
11:
return
"พ.ย"
;
break
;
40.
case
12:
return
"ธ.ค."
;
break
;
41.
default
:
return
"ม.ค."
;
break
;
42.
}
43.
}
44.
function
long_month(
$m_val
= NULL) {
45.
if
(
empty
(
$m_val
))
46.
$m_val
=
$this
->cast_datetime(
$this
->datetime,
"m"
);
47.
switch
(
$m_val
) {
48.
case
1:
return
"มกราคม"
;
break
;
49.
case
2:
return
"กุมภาพันธ์"
;
break
;
50.
case
3:
return
"มีนาคม"
;
break
;
51.
case
4:
return
"เมษายน"
;
break
;
52.
case
5:
return
"พฤษภาคม"
;
break
;
53.
case
6:
return
"มิถุนายน"
;
break
;
54.
case
7:
return
"กรกฏาคม"
;
break
;
55.
case
8:
return
"สิงหาคม"
;
break
;
56.
case
9:
return
"กันยายน"
;
break
;
57.
case
10:
return
"ตุลาคม"
;
break
;
58.
case
11:
return
"พฤศจิกายน"
;
break
;
59.
case
12:
return
"ธันวาคม"
;
break
;
60.
default
:
return
"มกราคม"
;
break
;
61.
}
62.
}
63.
function
year(
$value
= NULL) {
64.
if
(
empty
(
$value
))
65.
return
$this
->cast_datetime(
$this
->datetime,
"Y"
)+543;
66.
else
return
$value
+543;
67.
}
68.
function
month() {
69.
return
$this
->cast_datetime(
$this
->datetime,
"m"
);
70.
}
71.
function
day() {
72.
return
$this
->cast_datetime(
$this
->datetime,
"d"
);
73.
}
74.
function
hour() {
75.
return
$this
->cast_datetime(
$this
->datetime,
"H"
);
76.
}
77.
function
minute() {
78.
return
$this
->cast_datetime(
$this
->datetime,
"M"
);
79.
}
80.
function
second() {
81.
return
$this
->cast_datetime(
$this
->datetime,
"S"
);
82.
}
83.
}
84.
85.
$x
=
new
thai_datetime();
86.
echo
$x
->day() ,
" "
,
$x
->short_month(),
" "
,
$x
->year();
87.
echo
'<br />'
;
88.
$y
=
new
thai_datetime(
"2005-1-20"
);
89.
echo
$y
->day() ,
" "
,
$y
->short_month(),
" "
,
$y
->year();