add format word

This commit is contained in:
nasser committed 2017-09-21 23:25:41 +03:30
1 parent 52f1bd994b
commit ea2cc45952
3 files changed
+176 -10

No files matched your search

+12
View File
@@ -1,3 +1,7 @@
[![Latest Stable Version](https://poser.pugx.org/hekmatinasser/verta/v/stable)](https://packagist.org/packages/hekmatinasser/verta)
[![Total Downloads](https://poser.pugx.org/hekmatinasser/verta/downloads)](https://packagist.org/packages/hekmatinasser/verta)
[![License](https://poser.pugx.org/hekmatinasser/verta/license)](https://packagist.org/packages/hekmatinasser/verta)
hekmatinasser/verta hekmatinasser/verta
====== ======
@@ -203,6 +207,14 @@ return $v1->addDays(25)->formatDifference(); // 4 هفته بعد compare with n
return $v1->subDays(6)->formatDifference(); // 6 روز قبل return $v1->subDays(6)->formatDifference(); // 6 روز قبل
return verta()->formatDifference(); // الان return verta()->formatDifference(); // الان
``` ```
get Word Format
```php
return $v->formatWord('Y'); // یک هزار و سیصد و نود و شش
return $v->formatWord('l dS F'); // چهارشنبه بیست و نه ام شهریور
return $v->formatWord('d F Y'); // بیست و نه شهریور یک هزار و سیصد و نود و شش
return $v->formatWord('r'); // چهارشنبه یک هزار و سیصد و نود و شش, شش, بیست و نه, بیست و دو:چهل و نه:سی و هشت +04:30
return $v->formatWord('d F ') . $v->year; // بیست و نه شهریور 1396
```
change number change number
```php ```php
$v = verta(); $v = verta();
+2 -1
View File
@@ -11,7 +11,8 @@
"keywords": ["Laravel","Date","Datetime","Jalali", "Shamsi", "Verta"], "keywords": ["Laravel","Date","Datetime","Jalali", "Shamsi", "Verta"],
"require": { "require": {
"php": ">=5.5", "php": ">=5.5",
"illuminate/support": "^5.0" "illuminate/support": "^5.0",
"hekmatinasser/notowo": "^1.0"
}, },
"require-dev" : {}, "require-dev" : {},
"autoload": { "autoload": {
+162 -9
View File
@@ -13,9 +13,8 @@ namespace Hekmatinasser\Verta;
use DateTime; use DateTime;
use DateTimeZone; use DateTimeZone;
use DatePeriod;
use InvalidArgumentException; use InvalidArgumentException;
use Exception; use Hekmatinasser\Notowo\Notowo;
class Verta extends DateTime { class Verta extends DateTime {
@@ -1019,6 +1018,160 @@ class Verta extends DateTime {
return $result; return $result;
} }
/**
* The format of the outputted date string (jalali equivalent of php date() function)
*
* @param string $format for example 'Y-m-d H:i:s'
* @return string
*/
protected function dateWord($format){
$timestamp = $this->getTimestamp();
list($gYear, $gMonth, $gDay, $gWeek) = explode('-', date('Y-m-d-w', $timestamp));
list($pYear, $pMonth, $pDay) = static::getJalali($gYear, $gMonth, $gDay);
$pWeek = ($gWeek + 1);
if ($pWeek >= 7) {
$pWeek = 0;
}
if ($format == '\\') {
$format = '//';
}
$lenghFormat = strlen($format);
$i = 0;
$result = '';
$word = new Notowo(0, 'fa');
while ($i < $lenghFormat) {
$par = $format{$i};
if ($par == '\\') {
$result .= $format{ ++$i};
$i ++;
continue;
}
switch ($par) {
# Day
case 'd':
case 'j':
$result .= $word->getWord(strval($pDay));
break;
case 'D':
$result .= substr(static::$dayWeek[$pWeek], 0, 2);
break;
case 'l':
$result .= static::$dayWeek[$pWeek];
break;
case 'N':
$result .= $word->getWord(strval($pWeek + 1));
break;
case 'w':
$result .= $word->getWord(strval($pWeek));
break;
case 'z':
$result .= $word->getWord(strval($this->daysYear($pMonth, $pDay)));
break;
case 'S':
$result .= self::NUMBER_TH;
break;
# Week
case 'W':
$result .= $word->getWord(strval(ceil($this->daysYear($pMonth, $pDay) / 7)));
break;
# Month
case 'F':
$result .= static::$monthYear[$pMonth-1];
break;
case 'm':
case 'n':
$result .= $word->getWord(strval($pMonth));
break;
case 'M':
$result .= substr(static::$monthYear[$pMonth-1], 0, 6);
break;
case 't':
$result .= $word->getWord(strval(static::isLeapYear($pYear) && ($pMonth == 12) ? 30 : static::$daysMonthJalali[intval($pMonth)-1]));
break;
# Years
case 'L':
$result .= intval($this->isLeapYear($pYear));
break;
case 'Y':
case 'o':
$result .= $word->getWord(strval($pYear));
break;
case 'y':
$result .= $word->getWord(strval(substr($pYear, 2)));
break;
# Time
case 'a':
case 'A':
if (date('a', $timestamp) == 'am') {
$result .= (($par == 'a') ? self::AM : self::ANTE_MERIDIEM);
} else {
$result .= (($par == 'a') ? self::PM : self::POST_MERIDIEM);
}
break;
case 'B':
case 'g':
case 'G':
case 'h':
case 'H':
case 's':
case 'u':
case 'i':
$result .= $word->getWord(strval(date($par, $timestamp)));
break;
case 'e':
case 'I':
case 'O':
case 'P':
case 'T':
case 'Z':
$result .= date($par, $timestamp);
break;
# Full Date/Time
case 'c':
$result .= $this->dateWord('Y, m, d, H:i:s P');
break;
case 'r':
$result .= $this->dateWord('l Y, m, d, H:i:s P');
break;
case 'U':
$result .= $word->getWord(strval($timestamp));
break;
default:
$result .= $par;
}
$i ++;
}
return $result;
}
/** /**
* return day number from first day of year * return day number from first day of year
* *
@@ -1035,7 +1188,6 @@ class Verta extends DateTime {
return ($days + $day); return ($days + $day);
} }
/** /**
* The format of the outputted date string (jalali equivalent of php strftime() function) * The format of the outputted date string (jalali equivalent of php strftime() function)
* *
@@ -1130,11 +1282,7 @@ class Verta extends DateTime {
"%", "%",
); );
//Change Strftime format to Date format return str_replace($str_format_code, $date_format_code, $format);
$format = str_replace($str_format_code, $date_format_code, $format);
//Convert to date
return $this->date($format);
} }
/** /**
@@ -1144,7 +1292,7 @@ class Verta extends DateTime {
* @return string * @return string
*/ */
public function format($format) { public function format($format) {
return $this->strftime($format); return $this->date($this->strftime($format));
} }
/** /**
@@ -1233,6 +1381,11 @@ class Verta extends DateTime {
return sprintf('%s %s %s', $difference, static::$unitName[$j], $absolute); return sprintf('%s %s %s', $difference, static::$unitName[$j], $absolute);
} }
public function formatWord($format)
{
return $this->dateWord($this->strftime($format));
}
/** /**
* Convert english numbers to persian numbers * Convert english numbers to persian numbers
* *