Skip to content

Commit 624d805

Browse files
committed
v2.0.5
* Fix `mb_detect_encoding()` function cannot detect encoding on some servers. * Add **strftime-function.php** page to listing and testing supported encoding.
1 parent a3c5208 commit 624d805

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

Rundiz/Thaidate/Thaidate.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
*
44
* @package Thaidate
5-
* @version 2.0.4
5+
* @version 2.0.5
66
* @author Vee W.
77
* @license http://opensource.org/licenses/MIT
88
*
@@ -119,7 +119,29 @@ public function strftime($format, $timestamp = '')
119119

120120
$converted_datetime = strftime($format, $timestamp);
121121
$detect_encoding = mb_detect_encoding($converted_datetime, mb_detect_order(), true);
122-
return iconv($detect_encoding, 'UTF-8', $converted_datetime);
122+
if ($detect_encoding === false) {
123+
// if some server cannot detect encoding at all.
124+
// in this case, i assume that the encoding from `strfime()` is thai (base on locale).
125+
if (is_string($this->locale) && stripos($this->locale, 'th') !== false) {
126+
$detect_encoding = 'TIS-620';
127+
} elseif (is_array($this->locale)) {
128+
foreach ($this->locale as $locale) {
129+
if (is_string($locale) && stripos($locale, 'th') !== false) {
130+
$detect_encoding = 'TIS-620';
131+
break;
132+
}
133+
}// endforeach;
134+
unset($locale);
135+
}
136+
}
137+
138+
if ($detect_encoding !== false) {
139+
// if detect encoding with no problem.
140+
return iconv($detect_encoding, 'UTF-8', $converted_datetime);
141+
} else {
142+
// if detect encoding found some problem, return as-is.
143+
return $converted_datetime;
144+
}
123145
}// strftime
124146

125147

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
4+
5+
header('Content-Type: text/html; charset=UTF-8');
6+
7+
8+
$locale = 'th';
9+
setlocale(LC_ALL, $locale);
10+
11+
12+
$datetime = strftime('%#d %B %Y');
13+
echo ' <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> ' . PHP_EOL;
14+
$encodings = mb_list_encodings();
15+
foreach ($encodings as $encoding) {
16+
$detect_encoding = mb_detect_encoding($datetime, $encoding, true);
17+
echo 'detect ' . $encoding . ' =&gt; <code style="color: #999;">' . ($detect_encoding !== false ? 'true' : 'false') . '</code><br>' . PHP_EOL;
18+
if ($detect_encoding !== false) {
19+
echo ' &nbsp; &nbsp;<strong>' . iconv($detect_encoding, 'UTF-8', $datetime) . '</strong>';
20+
echo '<br style="margin-bottom: 20px;">' . PHP_EOL;
21+
}
22+
}
23+
echo '<p>force using TIS-620 as charset.</p>' . PHP_EOL;
24+
$datetime = iconv('TIS-620', 'UTF-8', $datetime);
25+
echo $datetime . '<br>' . PHP_EOL;

tests/via-http/thaidate-function.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
require dirname(dirname(__DIR__)).'/Rundiz/Thaidate/thaidate-functions.php';
99

1010

11+
header('Content-Type: text/html; charset=UTF-8');
12+
echo '<meta charset="utf-8">' . PHP_EOL;
13+
1114
echo 'Begin test thaidate();.'."<br>\n";
1215
echo 'time(); = '.time()."<br>\n";
1316
echo 'Current date/time use date(); = '.date('Y-m-d H:i:s')."<br>\n";

tests/via-http/thaistrftime-function.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
require dirname(dirname(__DIR__)).'/Rundiz/Thaidate/thaidate-functions.php';
99

1010

11+
header('Content-Type: text/html; charset=UTF-8');
12+
echo '<meta charset="utf-8">' . PHP_EOL;
13+
1114
echo 'Begin test thaistrftime();.'."<br>\n";
1215
echo 'time(); = '.time()."<br>\n";
1316
echo 'Current date/time use date(); = '.date('Y-m-d H:i:s')."<br>\n";

0 commit comments

Comments
 (0)