Skip to content

Commit 1ff8053

Browse files
committed
v2.1.0
* Fix `strftime()` deprecated error in PHP 8.1+. * Add new method `intlDate()` that use PHP class `\IntlDateFormatter()`.
1 parent 0a2dfa6 commit 1ff8053

File tree

4 files changed

+165
-14
lines changed

4 files changed

+165
-14
lines changed

Rundiz/Thaidate/Thaidate.php

Lines changed: 119 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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
}

tests/phpunit/ThaidateTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ public function testThaidateClass()
2424
{
2525
$timestamp = 1460619637;
2626
$Thaidate = new \Rundiz\Thaidate\Thaidate();
27-
28-
$this->assertEquals('วันพฤหัสบดีที่ 14 เมษายน พ.ศ.2559', $Thaidate->date('วันlที่ j F พ.ศ.Y', $timestamp));
2927
if (version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.5.0', '<') && stripos(PHP_OS, 'WIN') === 0) {
30-
$this->assertEquals('พฤ. 14 เม.ย. 59', $Thaidate->strftime('%a %d %b %y', $timestamp, true, 'Thai'));
28+
$Thaidate->locale = 'Thai';
3129
} else {
32-
$this->assertEquals('พฤ. 14 เม.ย. 59', $Thaidate->strftime('%a %d %b %y', $timestamp, true, array('th', 'th_TH.utf8', 'th_TH.UTF8', 'th_TH.utf-8', 'th_TH.UTF-8', 'th_TH', 'th-TH')));
30+
$Thaidate->locale = array('th', 'th_TH.utf8', 'th_TH.UTF8', 'th_TH.utf-8', 'th_TH.UTF-8', 'th_TH', 'th-TH');
3331
}
32+
33+
$this->assertEquals('วันพฤหัสบดีที่ 14 เมษายน พ.ศ.2559', $Thaidate->date('วันlที่ j F พ.ศ.Y', $timestamp));
34+
$this->assertEquals('พฤ. 14 เม.ย. 59', $Thaidate->strftime('%a %d %b %y', $timestamp));
35+
$this->assertEquals('พฤ. 14 เม.ย. 59', $Thaidate->intlDate('EE d MMM yy', $timestamp));
36+
$this->assertEquals('วันพฤหัสบดี 14 เมษายน 2559', $Thaidate->intlDate('EEEE d MMMM yyyy', $timestamp));// can't get rid of the word 'วัน' using pattern.
3437
}// testThaidateClass
3538

3639

tests/via-http/thaiintldate.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* This is test file. You can remove this.
4+
*/
5+
6+
require dirname(dirname(__DIR__)).'/Rundiz/Thaidate/Thaidate.php';
7+
8+
header('Content-Type: text/html; charset=UTF-8');
9+
echo '<meta charset="utf-8">' . PHP_EOL;
10+
11+
echo 'Begin test IntlDateFormatter();.'."<br>\n";
12+
echo 'time(); = '.time()."<br>\n";
13+
echo 'Current date/time use date(); = '.date('Y-m-d H:i:s')."<br>\n";
14+
if (class_exists('\IntlDateFormatter')) {
15+
$IntlDateFormatter = new \IntlDateFormatter(null, \IntlDateFormatter::FULL, \IntlDateFormatter::FULL);
16+
echo 'Current date/time use IntlDateFormatter(); = ' . $IntlDateFormatter->format(time()) . "<br>\n";
17+
} else {
18+
echo 'Class IntlDateFormatter() is not exits.<br>' . "\n";
19+
}
20+
echo '----------------------------------'."<br>\n";
21+
22+
$Thaidate = new Rundiz\Thaidate\Thaidate();
23+
$Thaidate->buddhist_era = true;
24+
$Thaidate->locale = 'th';
25+
26+
echo 'Thai date test.'."<br>\n";
27+
echo '12 Months'."<br>\n";
28+
for ($i = 1; $i <= 12; $i++) {
29+
echo $Thaidate->intlDate('cccc d MMMM yyyy', strtotime(date('Y').'-'.$i.'-01'))."<br>\n";
30+
}
31+
echo '---------------------'."<br>\n";
32+
echo '12 Months in short'."<br>\n";
33+
for ($i = 1; $i <= 12; $i++) {
34+
echo $Thaidate->intlDate('ccc d MMM yyyy', strtotime(date('Y').'-'.$i.'-01'))."<br>\n";
35+
}
36+
echo '---------------------'."<br>\n";
37+
echo 'Full long date with time'."<br>\n";
38+
echo sprintf($Thaidate->intlDate('cccc\'%1$s\' d MMMM Gyyyy \'%2$s\'H:mm:ss'), 'ที่', 'เวลา')."<br>\n";

tests/via-http/thaistrftime-function.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* This is test file. You can remove this.
44
*/
55

6-
76
require dirname(dirname(__DIR__)).'/Rundiz/Thaidate/Thaidate.php';
87
require dirname(dirname(__DIR__)).'/Rundiz/Thaidate/thaidate-functions.php';
98

@@ -41,4 +40,4 @@
4140
}
4241
echo '---------------------'."<br>\n";
4342
echo 'Full long date with time'."<br>\n";
44-
echo sprintf(thaistrftime('%%s%A%%s %d %B %%s%Y %%s%H:%M:%S'), 'วัน', 'ที่', 'พ.ศ.', 'เวลา')."<br>\n";
43+
echo sprintf(thaistrftime('%%s%A%%s %d %B %%s%Y %%s%H:%M:%S'), 'วัน', 'ที่', 'พ.ศ.', 'เวลา')."<br>\n";

0 commit comments

Comments
 (0)