Skip to content

Commit c033499

Browse files
committed
improve convert_dateTime and convert_date function
1 parent 983883b commit c033499

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

src/functions.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,17 @@ class functions extends JDF
2626
/**
2727
* Will Change yyyy-mm-dd hh:ii:ss from gregorian to shmsi(jalali)
2828
* @param $in_datetime
29+
* @param string $delimiter
30+
* @param string $target jalali | gregory
2931
* @return string will return string date and if DateTime was =='0000-00-00 00:00:00' will return '---'
3032
* @todo debug delimiter
3133
*/
32-
public function convert_dateTime($in_datetime, $delimiter = null)
34+
public function convert_dateTime($in_datetime,$target = 'jalali', $delimiter = '-')
3335
{
3436
if ($in_datetime && $in_datetime != '0000-00-00 00:00:00') {
35-
36-
if ($delimiter) {
37-
$datetime = explode($delimiter, $in_datetime);
38-
39-
40-
} else {
41-
$datetime = explode(' ', $in_datetime);
42-
43-
}
44-
45-
return self::jdate('Y/m/d', strtotime($datetime[0])).' - '.$datetime[1];
37+
$dateExplited = explode(' ', $in_datetime);
38+
$out= $this->convert_date($dateExplited[0], $target,$delimiter).(!empty($dateExplited[1])?' '.$dateExplited[1]:'');
39+
return $out;
4640
}
4741

4842
return '---';
@@ -117,8 +111,9 @@ public function convert_date($in_date, string $target = 'jalali', string $delimi
117111
if ($in_date == '0000-00-00') {
118112
return null;
119113
}
120-
121-
return str_replace('/', $delimiter, self::jdate('Y/m/d', strtotime($in_date)));
114+
$converted=self::jdate('Y/m/d', strtotime($in_date));
115+
$out= str_replace('/', $delimiter,$converted );
116+
return $out;
122117
}
123118

124119
return null;

test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
define('N', "\n\n\n");
1010
$f = new functions();
1111
echo 'convert '.date('Y-m-d ').'00:00:00'.' to date (clear_zerotime method):'.$f->clear_zerotime(date('Y-m-d '.'00:00:00')).N;
12-
echo 'convert '.date('Y-m-d H:i:s').' to jalali (convert_dateTime method):'.$f->convert_dateTime(date('Y-m-d H:i:s')).N;
13-
echo 'convert '.date('Y-m-d').' to jalali (convert_date method):'.$f->convert_date(date('Y-m-d')).N;
12+
echo 'convert '.date('Y-m-d H:i:s').' to jalali (convert_dateTime method):'.$f->convert_dateTime(date('Y-m-d H:i:s'),'jalali','-').N;
13+
echo 'convert '.date('Y-m-d').' to jalali (convert_date method):'.$f->convert_date(date('Y-m-d'),'jalali','/').N;
1414
echo 'convert `2017-2018` to jalali (yearsToShamsi method):'.$f->yearsToShamsi('2017-2018').N;
1515
echo 'convert `2017` to jalali (YearToShamsi method):'.$f->YearToShamsi('2017').N;
1616
echo 'convert `1396` to gregory (YearToGregorian method):'.$f->YearToGregorian('1396').N;

0 commit comments

Comments
 (0)