1+ <?php
2+ /**
3+ *
4+ * @package Thaidate
5+ * @version 2.0
6+ * @author Vee W.
7+ * @license http://opensource.org/licenses/MIT
8+ *
9+ */
10+
11+ namespace Rundiz \Thaidate ;
12+
13+ /**
14+ * Format date in Thai locale.
15+ */
16+ class Thaidate
17+ {
18+
19+
20+ /**
21+ * Use Buddhist Era? (พ.ศ.)
22+ * @var boolean Set to true to use or false not to use.
23+ */
24+ public $ buddhist_era = true ;
25+
26+ /**
27+ * Locale to use in setlocale() function. Some server support different locales, with .UTF8, or without .UTF8. Detect and use at your own.
28+ * @var type
29+ */
30+ public $ locale = 'th ' ;
31+
32+ public $ month_long = array ('มกราคม ' , 'กุมภาพันธ์ ' , 'มีนาคม ' , 'เมษายน ' , 'พฤษภาคม ' , 'มิถุนายน ' , 'กรกฎาคม ' , 'สิงหาคม ' , 'กันยายน ' , 'ตุลาคม ' , 'พฤศจิกายน ' , 'ธันวาคม ' );
33+ public $ month_short = array ('ม.ค. ' , 'ก.พ. ' , 'มี.ค. ' , 'เม.ย. ' , 'พ.ค. ' , 'มิ.ย. ' , 'ก.ค. ' , 'ส.ค. ' , 'ก.ย. ' , 'ต.ค. ' , 'พ.ย. ' , 'ธ.ค. ' );
34+
35+ public $ day_long = array ('อาทิตย์ ' , 'จันทร์ ' , 'อังคาร ' , 'พุธ ' , 'พฤหัสบดี ' , 'ศุกร์ ' , 'เสาร์ ' );
36+ public $ day_short = array ('อา. ' , 'จ. ' , 'อ. ' , 'พ. ' , 'พฤ. ' , 'ศ. ' , 'ส. ' );
37+
38+
39+ /**
40+ * Thai date() function.
41+ *
42+ * @param string $format The format as same as PHP date function format. See http://php.net/manual/en/function.date.php
43+ * @param integer $timestamp The optional timestamp is an integer Unix timestamp.
44+ * @return string Return the formatted date/time string.
45+ */
46+ public function date ($ format , $ timestamp = '' )
47+ {
48+ if ($ timestamp == null ) {
49+ $ timestamp = time ();
50+ }
51+
52+ // if use Buddhist era, convert the year.
53+ if ($ this ->buddhist_era === true ) {
54+ if (mb_strpos ($ format , 'o ' ) !== false ) {
55+ $ year = (date ('o ' , $ timestamp )+543 );
56+ $ format = str_replace ('o ' , $ year , $ format );
57+ } elseif (mb_strpos ($ format , 'Y ' ) !== false ) {
58+ $ year = (date ('Y ' , $ timestamp )+543 );
59+ $ format = str_replace ('Y ' , $ year , $ format );
60+ } elseif (mb_strpos ($ format , 'y ' ) !== false ) {
61+ $ year = (date ('y ' , $ timestamp )+43 );
62+ $ format = str_replace ('y ' , $ year , $ format );
63+ }
64+ unset($ year );
65+ }
66+
67+ // replace format that will be convert month into name (F, M) to Thai.
68+ if (mb_strpos ($ format , 'F ' ) !== false || mb_strpos ($ format , 'M ' ) !== false ) {
69+ $ month_num = (date ('n ' , $ timestamp )-1 );
70+ if (mb_strpos ($ format , 'F ' ) !== false && array_key_exists ($ month_num , $ this ->month_long )) {
71+ $ format = str_replace ('F ' , $ this ->month_long [$ month_num ], $ format );
72+ } elseif (mb_strpos ($ format , 'M ' ) !== false && array_key_exists ($ month_num , $ this ->month_short )) {
73+ $ format = str_replace ('M ' , $ this ->month_short [$ month_num ], $ format );
74+ }
75+ unset($ month_num );
76+ }
77+
78+ // replace format that will be convert day into name (D, l) to Thai.
79+ if (mb_strpos ($ format , 'l ' ) !== false || mb_strpos ($ format , 'D ' ) !== false ) {
80+ $ day_num = date ('w ' , $ timestamp );
81+ if (mb_strpos ($ format , 'l ' ) !== false && array_key_exists ($ day_num , $ this ->day_long )) {
82+ $ format = str_replace ('l ' , $ this ->day_long [$ day_num ], $ format );
83+ } elseif (mb_strpos ($ format , 'D ' ) !== false && array_key_exists ($ day_num , $ this ->day_short )) {
84+ $ format = str_replace ('D ' , $ this ->day_short [$ day_num ], $ format );
85+ }
86+ unset($ day_num );
87+ }
88+
89+ return date ($ format , $ timestamp );
90+ }// date
91+
92+
93+ /**
94+ * Thai date use strftime() function.
95+ *
96+ * @param string $format The format as same as PHP date function format. See http://php.net/manual/en/function.strftime.php
97+ * @param integer $timestamp The optional timestamp is an integer Unix timestamp.
98+ * @return string Return the formatted date/time string.
99+ */
100+ public function strftime ($ format , $ timestamp = '' )
101+ {
102+ if ($ timestamp == null ) {
103+ $ timestamp = time ();
104+ }
105+
106+ setlocale (LC_TIME , $ this ->locale );
107+
108+ // if use Buddhist era, convert the year (y, Y).
109+ if ($ this ->buddhist_era === true ) {
110+ if (mb_strpos ($ format , '%Y ' ) !== false ) {
111+ $ year = (strftime ('%Y ' , $ timestamp )+543 );
112+ $ format = str_replace ('%Y ' , $ year , $ format );
113+ } elseif (mb_strpos ($ format , '%y ' ) !== false ) {
114+ $ year = (strftime ('%y ' , $ timestamp )+43 );
115+ $ format = str_replace ('%y ' , $ year , $ format );
116+ }
117+ unset($ year );
118+ }
119+
120+ $ converted_datetime = strftime ($ format , $ timestamp );
121+ $ detect_encoding = mb_detect_encoding ($ converted_datetime , mb_detect_order (), true );
122+ return iconv ($ detect_encoding , 'UTF-8 ' , $ converted_datetime );
123+ }// strftime
124+
125+
126+ }
0 commit comments