Skip to content

Commit

Permalink
add format word
Browse files Browse the repository at this point in the history
  • Loading branch information
hekmatinasser committed Sep 21, 2017
1 parent 52f1bd9 commit ea2cc45
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 10 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
======

Expand Down Expand Up @@ -203,6 +207,14 @@ return $v1->addDays(25)->formatDifference(); // 4 هفته بعد compare with n
return $v1->subDays(6)->formatDifference(); // 6 روز قبل
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
```php
$v = verta();
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"keywords": ["Laravel","Date","Datetime","Jalali", "Shamsi", "Verta"],
"require": {
"php": ">=5.5",
"illuminate/support": "^5.0"
"illuminate/support": "^5.0",
"hekmatinasser/notowo": "^1.0"
},
"require-dev" : {},
"autoload": {
Expand Down
171 changes: 162 additions & 9 deletions src/Verta.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

use DateTime;
use DateTimeZone;
use DatePeriod;
use InvalidArgumentException;
use Exception;
use Hekmatinasser\Notowo\Notowo;

class Verta extends DateTime {

Expand Down Expand Up @@ -1019,6 +1018,160 @@ protected function date($format){
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
*
Expand All @@ -1035,7 +1188,6 @@ protected function daysYear($month, $day) {
return ($days + $day);
}


/**
* The format of the outputted date string (jalali equivalent of php strftime() function)
*
Expand Down Expand Up @@ -1130,11 +1282,7 @@ protected function strftime($format)
"%",
);

//Change Strftime format to Date format
$format = str_replace($str_format_code, $date_format_code, $format);

//Convert to date
return $this->date($format);
return str_replace($str_format_code, $date_format_code, $format);
}

/**
Expand All @@ -1144,7 +1292,7 @@ protected function strftime($format)
* @return string
*/
public function format($format) {
return $this->strftime($format);
return $this->date($this->strftime($format));
}

/**
Expand Down Expand Up @@ -1233,6 +1381,11 @@ public function formatDifference(Verta $v = null)
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
*
Expand Down

0 comments on commit ea2cc45

Please sign in to comment.