Skip to content

Commit b24b3d0

Browse files
committed
moved to rundiz/thai-date
moved from okvee/thai-date to rundiz/thai-date add phpunit test
1 parent b499ddb commit b24b3d0

File tree

10 files changed

+550
-0
lines changed

10 files changed

+550
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Thaidate Component
2+
3+
Display date in Thai by using same PHP `date()` and `strftime()` function attributes.
4+
5+
[![Latest Stable Version](https://poser.pugx.org/rundiz/thai-date/v/stable)](https://packagist.org/packages/rundiz/thai-date)
6+
[![License](https://poser.pugx.org/rundiz/thai-date/license)](https://packagist.org/packages/rundiz/thai-date)
7+
[![Total Downloads](https://poser.pugx.org/rundiz/thai-date/downloads)](https://packagist.org/packages/rundiz/thai-date)
8+
9+
```php
10+
echo thaidate('วันlที่ j F พ.ศ.Y เวลาH:i:s');
11+
// results: วันพฤหัสบดีที่ 12 พฤศจิกายน พ.ศ.2558 เวลา18:55:29
12+
```
13+
14+
```php
15+
echo sprintf(thaistrftime('%%s%A%%s %d %B %%s%Y %%s%H:%M:%S'), 'วัน', 'ที่', 'พ.ศ.', 'เวลา');
16+
// results: วันพฤหัสบดีที่ 12 พฤศจิกายน พ.ศ.2558 เวลา18:56:06
17+
```
18+
19+
For more details, please look in tests folder

Rundiz/Thaidate/Thaidate.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/*
3+
*
4+
* @author Vee W.
5+
* @license http://opensource.org/licenses/MIT
6+
*
7+
*/
8+
9+
10+
/**
11+
* Thai date use date() function.
12+
*
13+
* @param string $format The format as same as PHP date function format. See http://php.net/manual/en/function.date.php
14+
* @param integer $timestamp The optional timestamp is an integer Unix timestamp.
15+
* @param boolean $buddhist_era Use Buddhist era? set to true to use that or false not to use.
16+
* @return string Return the formatted date/time string.
17+
*/
18+
function thaidate($format, $timestamp = '', $buddhist_era = true)
19+
{
20+
if (!is_bool($buddhist_era)) {
21+
$buddhist_era = true;
22+
}
23+
24+
$thaidate = new \Rundiz\Thaidate\Thaidate();
25+
$thaidate->buddhist_era = $buddhist_era;
26+
return $thaidate->date($format, $timestamp);
27+
}// thaidate
28+
29+
30+
/**
31+
* Thai date use strftime() function.
32+
*
33+
* @param string $format The format as same as PHP date function format. See http://php.net/manual/en/function.strftime.php
34+
* @param integer $timestamp The optional timestamp is an integer Unix timestamp.
35+
* @param boolean $buddhist_era Use Buddhist era? set to true to use that or false not to use.
36+
* @param string $locale The locale that will be use in setlocale() function. See http://php.net/setlocale
37+
* @return string Return the formatted date/time string.
38+
*/
39+
function thaistrftime($format, $timestamp = '', $buddhist_era = true, $locale = 'th')
40+
{
41+
if ($locale == null) {
42+
$locale = 'th';
43+
}
44+
45+
if (!is_bool($buddhist_era)) {
46+
$buddhist_era = true;
47+
}
48+
49+
$thaidate = new \Rundiz\Thaidate\Thaidate();
50+
$thaidate->buddhist_era = $buddhist_era;
51+
$thaidate->locale = $locale;
52+
return $thaidate->strftime($format, $timestamp);
53+
}// thaistrftime

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "rundiz/thai-date",
3+
"type": "library",
4+
"description": "display date in Thai language use PHP date() and strftime()",
5+
"keywords": ["date", "Thai date", "thaidate", "date thai", "datethai", "date translation"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Vee W.",
10+
"email": "[email protected]",
11+
"homepage": "http://rundiz.com",
12+
"role": "Developer"
13+
}
14+
],
15+
"require": {
16+
"php": ">=5.3.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Rundiz\\Thaidate\\" : "Rundiz/Thaidate"
21+
},
22+
"files": ["Rundiz/Thaidate/thaidate-functions.php"]
23+
}
24+
}

phpunit.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="./tests/phpunit/phpunit.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="true"
12+
verbose="true"
13+
>
14+
<testsuites>
15+
<testsuite name="tests/phpunit">
16+
<directory suffix=".php">./tests/phpunit/</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">./Rundiz/Thaidate/</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

0 commit comments

Comments
 (0)