Skip to content

Commit c02927c

Browse files
authored
Time identification
1 parent 9b97853 commit c02927c

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

src/PDate.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class PDate extends Shamsi
1010
private $_timestamp = 0;
1111
private string $_format = 'Y-m-d H:i:s';
1212

13+
private array $_time = [0,0,0];
14+
1315
private const day_sec = 86400;
1416
private const week_sec = 604800;
1517

@@ -22,14 +24,14 @@ public static function checkStr(string $date, string $separator='-'):bool{
2224
$arr = explode($separator, $date);
2325

2426
if(count($arr)!=3){
25-
// echo "sdfdsfdsfdsfs";
2627
return false;
2728
}
2829

2930
return self::check($arr[2], $arr[1], $arr[0]);
3031
}
3132

3233

34+
3335
public static function new()
3436
{
3537
return (new PDate);
@@ -131,12 +133,41 @@ public function monthName(): string
131133
return $this->jdate('F', $this->_timestamp);
132134
}
133135

136+
137+
138+
private function detectTimeFromString(&$str){
139+
140+
$ex_date_and_time = explode(' ' , $str);
141+
142+
if(in_array(count($ex_date_and_time),[2,3]) && strpos($ex_date_and_time[1], ':')>0){
143+
$str = $ex_date_and_time[0];
144+
$this->_time =explode(':', $ex_date_and_time[1]);
145+
}
146+
147+
}
148+
149+
private function detectTimeFromArray($array){
150+
$ex_date_and_time = explode(' ' , $array[count($array)-1]);
151+
152+
if(in_array(count($ex_date_and_time),[2,3]) && strpos($ex_date_and_time[1], ':')>0){
153+
$array[count($array)-1] = $ex_date_and_time[0];
154+
$this->_time =explode(':', $ex_date_and_time[1]);
155+
}
156+
}
157+
158+
134159
public function fromGregorian(array|string $gregorian_date, $separator = '-')
135160
{
136161
if(is_string($gregorian_date)){
162+
163+
$this->detectTimeFromString($gregorian_date);
164+
137165
$array = explode($separator, $gregorian_date);
138166
}
139167
else{
168+
169+
$this->detectTimeFromArray($gregorian_date);
170+
140171
$array = $gregorian_date;
141172
}
142173

@@ -145,16 +176,22 @@ public function fromGregorian(array|string $gregorian_date, $separator = '-')
145176
}
146177

147178
$result = $this->gregorian_to_jalali(intval($array[0]), intval($array[1]), intval($array[2]));
148-
$this->_timestamp = $this->jmktime(0, 0, 0, $result[1], $result[2], $result[0]);
179+
$this->_timestamp = $this->jmktime($this->_time[0]??0,$this->_time[1]??0, $this->_time[2]??0, $result[1], $result[2], $result[0]);
149180
return $this;
150181
}
151182

152183
public function fromShamsi(array|string $shamsi_date, $separator = '-')
153184
{
154185
if(is_string($shamsi_date)){
186+
187+
$this->detectTimeFromString($shamsi_date);
188+
155189
$array = explode($separator, $shamsi_date);
156190
}
157191
else{
192+
193+
$this->detectTimeFromArray($shamsi_date);
194+
158195
$array = $shamsi_date;
159196
}
160197

@@ -163,7 +200,7 @@ public function fromShamsi(array|string $shamsi_date, $separator = '-')
163200
}
164201

165202
$result = $this->jalali_to_gregorian(intval($array[0]), intval($array[1]), intval($array[2]));
166-
$this->_timestamp = mktime(0, 0, 0, $result[1], $result[2], $result[0]);
203+
$this->_timestamp = mktime($this->_time[0]??0,$this->_time[1]??0, $this->_time[2]??0, $result[1], $result[2], $result[0]);
167204
return $this;
168205
}
169206

0 commit comments

Comments
 (0)