22/**
33 *
44 * @package Thaidate
5- * @version 2.0.5
5+ * @version 2.1.0
66 * @author Vee W.
77 * @license http://opensource.org/licenses/MIT
88 *
@@ -40,12 +40,12 @@ class Thaidate
4040 * Thai date() function.
4141 *
4242 * @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.
43+ * @param int $timestamp The optional timestamp is an integer Unix timestamp.
4444 * @return string Return the formatted date/time string.
4545 */
4646 public function date ($ format , $ timestamp = '' )
4747 {
48- if ($ timestamp == null ) {
48+ if (! is_numeric ( $ timestamp) ) {
4949 $ timestamp = time ();
5050 }
5151
@@ -90,16 +90,59 @@ public function date($format, $timestamp = '')
9090 }// date
9191
9292
93+ /**
94+ * Thai date use `\IntlDateFormatter()` class.
95+ *
96+ * @since 2.1.0
97+ * @param string $format The format or pattern as **same** as ICU format. See https://unicode-org.github.io/icu/userguide/format_parse/datetime/
98+ * @param int $timestamp
99+ * @return string Return the formatted date/time string.
100+ */
101+ public function intlDate ($ format , $ timestamp = '' )
102+ {
103+ if (!is_numeric ($ timestamp )) {
104+ $ timestamp = time ();
105+ }
106+
107+ if ($ this ->buddhist_era === true ) {
108+ $ calendar = \IntlDateFormatter::TRADITIONAL ;
109+ } else {
110+ $ calendar = null ;
111+ }
112+ $ locale = $ this ->locale ;
113+ if (is_array ($ this ->locale )) {
114+ $ localeVals = array_values ($ locale );
115+ $ locale = array_shift ($ localeVals );
116+ unset($ localeVals );
117+ } elseif (!is_scalar ($ this ->locale )) {
118+ $ locale = 'th ' ;
119+ }
120+ $ IntlDateFormatter = new \IntlDateFormatter ($ locale , \IntlDateFormatter::FULL , \IntlDateFormatter::FULL , null , $ calendar );
121+ $ IntlDateFormatter ->setPattern ($ format );
122+ return $ IntlDateFormatter ->format ($ timestamp );
123+ }// intlDate
124+
125+
93126 /**
94127 * Thai date use strftime() function.
95128 *
129+ * Function `strftime()` is deprecated since PHP 8.1. This method will be here to keep it working from old projects to new.<br>
130+ * However, please validate the result that it really is correct once PHP removed this function in version 9.0.<br>
131+ * Use other method instead of this is recommended.
132+ *
96133 * @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.
134+ * @param int $timestamp The optional timestamp is an integer Unix timestamp.
98135 * @return string Return the formatted date/time string.
99136 */
100137 public function strftime ($ format , $ timestamp = '' )
101138 {
102- if ($ timestamp == null ) {
139+ if (!function_exists ('strftime ' )) {
140+ if (class_exists ('\IntlDateFormatter ' )) {
141+ return $ this ->intlDate ($ this ->strftimeFormatToIntlDatePattern ($ format ), $ timestamp );
142+ }
143+ }
144+
145+ if (!is_numeric ($ timestamp )) {
103146 $ timestamp = time ();
104147 }
105148
@@ -108,16 +151,16 @@ public function strftime($format, $timestamp = '')
108151 // if use Buddhist era, convert the year (y, Y).
109152 if ($ this ->buddhist_era === true ) {
110153 if (mb_strpos ($ format , '%Y ' ) !== false ) {
111- $ year = (strftime ('%Y ' , $ timestamp )+543 );
154+ $ year = (@ strftime ('%Y ' , $ timestamp )+543 );
112155 $ format = str_replace ('%Y ' , $ year , $ format );
113156 } elseif (mb_strpos ($ format , '%y ' ) !== false ) {
114- $ year = (strftime ('%y ' , $ timestamp )+43 );
157+ $ year = (@ strftime ('%y ' , $ timestamp )+43 );
115158 $ format = str_replace ('%y ' , $ year , $ format );
116159 }
117160 unset($ year );
118161 }
119162
120- $ converted_datetime = strftime ($ format , $ timestamp );
163+ $ converted_datetime = @ strftime ($ format , $ timestamp );
121164 $ detect_encoding = mb_detect_encoding ($ converted_datetime , mb_detect_order (), true );
122165 if ($ detect_encoding === false ) {
123166 // if some server cannot detect encoding at all.
@@ -145,4 +188,72 @@ public function strftime($format, $timestamp = '')
145188 }// strftime
146189
147190
191+ /**
192+ * Convert from `strftime()` format to `\IntlDateFormatter()` pattern.
193+ *
194+ * There are no patterns that has no word 'วัน' from day of week.
195+ *
196+ * @since 2.1.0
197+ * @param string $format The date format that used by `strftime()` function.
198+ * @return string Return converted format.
199+ */
200+ protected function strftimeFormatToIntlDatePattern ($ format )
201+ {
202+ $ output = $ format ;
203+
204+ $ replaces = array (
205+ '%a ' => 'E ' ,
206+ '%A ' => 'EEEE ' ,
207+ '%d ' => 'dd ' ,
208+ '%e ' => 'd ' ,
209+ '%j ' => 'D ' ,
210+ '%u ' => 'e ' ,// not 100% correct
211+ '%w ' => 'c ' ,// not 100% correct
212+ '%U ' => 'w ' ,
213+ '%V ' => 'ww ' ,// not 100% correct
214+ '%W ' => 'w ' ,// not 100% correct
215+ '%b ' => 'MMM ' ,
216+ '%B ' => 'MMMM ' ,
217+ '%h ' => 'MMM ' ,// alias of %b
218+ '%m ' => 'MM ' ,
219+ '%C ' => 'yy ' ,// no replace for this
220+ '%g ' => 'yy ' ,// no replace for this
221+ '%G ' => 'Y ' ,// not 100% correct
222+ '%y ' => 'yy ' ,
223+ '%Y ' => 'yyyy ' ,
224+ '%H ' => 'HH ' ,
225+ '%k ' => 'H ' ,
226+ '%I ' => 'hh ' ,
227+ '%l ' => 'h ' ,
228+ '%M ' => 'mm ' ,
229+ '%p ' => 'a ' ,
230+ '%P ' => 'a ' ,// no replace for this
231+ '%r ' => 'hh:mm:ss a ' ,
232+ '%R ' => 'HH:mm ' ,
233+ '%S ' => 'ss ' ,
234+ '%T ' => 'HH:mm:ss ' ,
235+ '%X ' => 'HH:mm:ss ' ,// no replace for this
236+ '%z ' => 'ZZ ' ,
237+ '%Z ' => 'v ' ,// no replace for this
238+ '%c ' => 'd/M/YYYY HH:mm:ss ' ,// Buddhist era may not converted.
239+ '%D ' => 'MM/dd/yy ' ,
240+ '%F ' => 'yyyy-MM-dd ' ,
241+ '%s ' => '' ,// no replace for this
242+ '%x ' => 'd/MM/yyyy ' ,// Buddhist era may not converted.
243+ '%n ' => "\n" ,
244+ '%t ' => "\t" ,
245+ '%% ' => '% ' ,
246+ );
247+
248+ $ output = preg_replace ('/(%%[a-zA-Z])/u ' , "'$1' " , $ output );// escape %%x with '%%x'.
249+ foreach ($ replaces as $ strftime => $ intl ) {
250+ $ output = preg_replace ('/(?<!%)( ' . $ strftime . ')/u ' , $ intl , $ output );
251+ }// endforeach;
252+ unset($ intl , $ strftime );
253+
254+ unset($ replaces );
255+ return $ output ;
256+ }// strftimeFormatToIntlDatePattern
257+
258+
148259}
0 commit comments