1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Rundiz\Thaidate;
12:
13: 14: 15:
16: class Thaidate
17: {
18:
19:
20: 21: 22: 23:
24: public $buddhist_era = true;
25:
26: 27: 28: 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: 41: 42: 43: 44: 45:
46: public function date($format, $timestamp = '')
47: {
48: if ($timestamp == null) {
49: $timestamp = time();
50: }
51:
52:
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:
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:
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: }
91:
92:
93: 94: 95: 96: 97: 98: 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:
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: }
124:
125:
126: }