1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the EasyBanglaDate package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 Roni Saha |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace EasyBanglaDate\Types; |
14
|
|
|
|
15
|
|
|
use EasyBanglaDate\Common\BaseDateTime; |
16
|
|
|
use EasyBanglaDate\Tools\Converter; |
17
|
|
|
|
18
|
|
|
class BnDateTime extends BaseDateTime |
19
|
|
|
{ |
20
|
|
|
/** @var DateTime */ |
21
|
|
|
private $_dateTime; |
22
|
|
|
|
23
|
|
|
/** @var \DateTime */ |
24
|
|
|
private $_phpDateTime; |
25
|
|
|
|
26
|
|
|
protected static $bnMonths = array( |
27
|
|
|
'F' => array('বৈশাখ','জ্যৈষ্ঠ','আষাঢ়','শ্রাবণ','ভাদ্র','আশ্বিন','কার্তিক','অগ্রহায়ণ','পৌষ','মাঘ','ফাল্গুন','চৈত্র'), |
28
|
|
|
'M' => array('বৈশাখ','জ্যৈষ্ঠ','আষাঢ়','শ্রাবণ','ভাদ্র','আশ্বিন','কার্তিক','অগ্র','পৌষ','মাঘ','ফাল্গুন','চৈত্র') |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
protected static $enMonths = array( |
32
|
|
|
'F' => array('Boishakh','Joishtha','Ashar','Srabon','Bhadra','Ashwin','Kartik','Ogrohayon','Poush','Magh','Falgun','Choitra'), |
33
|
|
|
'M' => array('Boi','Joi','Ash','Sra','Bha','Ash','Kar','Ogr','Pou','Mag','Fal','Cho') |
34
|
|
|
); |
35
|
|
|
protected static $daysInMonth = array('', 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30); |
36
|
|
|
protected static $enSuffix = array('th', 'st', 'nd', 'rd'); |
37
|
|
|
|
38
|
|
|
protected static $parameterList = array('a', 'A', 'l', 'D'); |
39
|
|
|
|
40
|
|
|
private $morning = 6; |
41
|
|
|
|
42
|
|
|
public function __construct($time = 'now', \DateTimeZone $timezone = null) |
43
|
|
|
{ |
44
|
|
|
parent::__construct($time, $timezone); |
45
|
|
|
$this->_dateTime = new DateTime(); |
46
|
|
|
$this->_phpDateTime = new \DateTime(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param DateTime|\DateTime|BnDateTime|string $time |
51
|
|
|
* |
52
|
|
|
* @return null|BnDateTime |
53
|
|
|
*/ |
54
|
|
|
public static function create($time = null) |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
if (is_string($time)) { |
58
|
|
|
$time = new self($time); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (self::isNullOrBnDateTimeObject($time)) { |
62
|
|
|
return $time; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$dateTime = new self(); |
66
|
|
|
|
67
|
|
|
if ($time instanceof \DateTime) { |
68
|
|
|
$dateTime->setTimezone($time->getTimezone()); |
69
|
|
|
$time = $time->getTimestamp(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$dateTime->setTimestamp($time); |
73
|
|
|
return $dateTime; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $time |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
private static function isNullOrBnDateTimeObject($time): bool |
82
|
|
|
{ |
83
|
|
|
return null === $time || $time instanceof BnDateTime; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function format($format) |
87
|
|
|
{ |
88
|
|
|
$bnDate = $this->getBengaliDateMonthYear(); |
89
|
|
|
|
90
|
|
|
$out = $this->replaceTimes($format); |
91
|
|
|
$out = $this->replaceTimePrefix($out); |
92
|
|
|
$out = $this->replaceBnSuffix($out, $bnDate); |
93
|
|
|
$out = $this->replaceDateNumbers($out, $bnDate); |
94
|
|
|
$out = $this->replaceMonthString($out, $bnDate, self::$bnMonths, '%s'); |
95
|
|
|
$out = $this->replaceDays($out); |
96
|
|
|
$out = $this->replaceMeridian($out); |
97
|
|
|
|
98
|
|
|
return $this->translateNumbers($out); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function enFormat($format) |
102
|
|
|
{ |
103
|
|
|
$bnDate = $this->getBengaliDateMonthYear(); |
104
|
|
|
|
105
|
|
|
$out = $this->replaceTimes($format); |
106
|
|
|
$out = $this->replaceDateNumbers($out, $bnDate); |
107
|
|
|
$out = $this->replaceToPlaceHolders($out); |
108
|
|
|
$out = $this->replaceMonthString($out, $bnDate, self::$enMonths, '{%s}'); |
109
|
|
|
$out = str_replace('{S}', $this->getEnSuffix($bnDate['day']), $out); |
110
|
|
|
|
111
|
|
|
return $this->replacePlaceHolders($out); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $morning |
116
|
|
|
*/ |
117
|
|
|
public function setMorning($morning) |
118
|
|
|
{ |
119
|
|
|
$this->morning = $morning; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setDate($year, $month, $day) |
123
|
|
|
{ |
124
|
|
|
$engTime = Converter::getEnglishTimeFromBanglaDate( |
125
|
|
|
$this->getNativeDateTimeObject(), |
126
|
|
|
array('day' => $day, 'month' => $month, 'year' => $year), |
127
|
|
|
$this->morning |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->setTimestamp($engTime->getTimestamp()); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return DateTime |
137
|
|
|
*/ |
138
|
|
|
public function getDateTime() |
139
|
|
|
{ |
140
|
|
|
return $this->_dateTime |
141
|
|
|
->setTimestamp($this->getTimestamp()) |
142
|
|
|
->setTimezone($this->getTimezone()) |
143
|
|
|
; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function replaceToPlaceHolders($out) { |
147
|
|
|
$paramList = array_merge(self::$parameterList , array("S", "M", "F")); |
148
|
|
|
|
149
|
|
|
foreach($paramList as $item) { |
150
|
|
|
$out = str_replace($item, '{' . $item .'}', $out); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $out; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function replacePlaceHolders($out) { |
157
|
|
|
|
158
|
|
|
foreach(self::$parameterList as $item) { |
159
|
|
|
$out = str_replace('{' . $item .'}', $this->_format($item), $out); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $out; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $format |
167
|
|
|
* @param $bnDate |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
protected function replaceBnSuffix($format, $bnDate) |
171
|
|
|
{ |
172
|
|
|
return str_replace('S', $this->getBnSuffix((int)$bnDate['day']), $format); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function replaceDateNumbers($format, $bnDate = array()) |
176
|
|
|
{ |
177
|
|
|
$format = str_replace('d', str_pad($bnDate['day'], 2, 0, STR_PAD_LEFT), $format); |
178
|
|
|
$format = str_replace('j', $bnDate['day'], $format); |
179
|
|
|
$format = str_replace('t', $this->getDayInMonth($bnDate['month']), $format); |
180
|
|
|
$format = str_replace('m', str_pad($bnDate['month'], 2, 0, STR_PAD_LEFT), $format); |
181
|
|
|
$format = str_replace('n', $bnDate['month'], $format); |
182
|
|
|
$format = str_replace('Y', $bnDate['year'], $format); |
183
|
|
|
$format = str_replace('y', substr($bnDate['year'], -2), $format); |
184
|
|
|
|
185
|
|
|
return $format; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return \DateTime |
190
|
|
|
*/ |
191
|
|
|
private function getNativeDateTimeObject() |
192
|
|
|
{ |
193
|
|
|
return $this->_phpDateTime |
194
|
|
|
->setTimestamp($this->getTimestamp()) |
195
|
|
|
->setTimezone($this->getTimezone()) |
196
|
|
|
; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function getBengaliDateMonthYear() |
200
|
|
|
{ |
201
|
|
|
return Converter::getBengaliDateMonthYear($this->getNativeDateTimeObject(), $this->morning); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private function getDayInMonth($month) |
205
|
|
|
{ |
206
|
|
|
if($month == 11 && $this->_format('L')) { |
207
|
|
|
return 31; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return self::$daysInMonth[$month]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function getEnSuffix($num) { |
214
|
|
|
|
215
|
|
|
$index = $this->getSuffixArrayIndexFromNumber($num); |
216
|
|
|
|
217
|
|
|
if($index > 3) { |
218
|
|
|
$index = 0; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return self::$enSuffix[$index]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param $template |
226
|
|
|
* @param $bnDate |
227
|
|
|
* @param $monthArray |
228
|
|
|
* @param $keyTemplate |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
protected function replaceMonthString($template, $bnDate, $monthArray, $keyTemplate) |
232
|
|
|
{ |
233
|
|
|
foreach(array('F','M') as $key){ |
234
|
|
|
$template = str_replace(sprintf($keyTemplate, $key), $monthArray[$key][$bnDate['month'] - 1], $template); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $template; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $num |
242
|
|
|
* @return int |
243
|
|
|
*/ |
244
|
|
|
protected function getSuffixArrayIndexFromNumber($num) |
245
|
|
|
{ |
246
|
|
|
if (in_array($num, array(11, 12, 13))) { |
247
|
|
|
return 0; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return ($num % 10); |
251
|
|
|
} |
252
|
|
|
} |