From 71126a7a8dd0e431b106f2ec45ee7610b3d33a3b Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Mon, 27 Feb 2017 12:32:15 +0330 Subject: [PATCH 01/22] init commit --- LICENSE | 20 + LICENSE~ | 20 + composer.json | 17 + composer.json~ | 21 + src/Verta.php | 833 +++++++++++++++++++++++++++++++++++ src/VertaServiceProvider.php | 42 ++ 6 files changed, 953 insertions(+) create mode 100755 LICENSE create mode 100755 LICENSE~ create mode 100755 composer.json create mode 100755 composer.json~ create mode 100755 src/Verta.php create mode 100755 src/VertaServiceProvider.php diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..a541286 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2016 Nasser Hekmati + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/LICENSE~ b/LICENSE~ new file mode 100755 index 0000000..8d2fb98 --- /dev/null +++ b/LICENSE~ @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2016 Morilog + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..dc1e381 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "hekmatinasser/verta", + "description": "This Package helps developers to work with Jalali Date and Time class.", + "license": "MIT", + "authors": [ + { + "name": "Nasser Hekmati", + "email": "hekmati.nasser@gmail.com" + } + ], + "keywords": ["Laravel","Date","Datetime","Jalali", "Shamsi", "Verta"], + "require": { + "php": ">=5.5" + }, + "require-dev" : {}, + "minimum-stability": "dev" +} diff --git a/composer.json~ b/composer.json~ new file mode 100755 index 0000000..3f283cd --- /dev/null +++ b/composer.json~ @@ -0,0 +1,21 @@ +{ + "name": "hekmatinasser/jalali", + "description": "This Package helps developers to work with Jalali Date and Time class.", + "license": "MIT", + "authors": [ + { + "name": "Nasser Hekmati", + "email": "hekmati.nasser@gmail.com" + } + ], + "keywords": ["Laravel","Date","Datetime","Jalali", "Shamsi"], + "require": { + "php": ">=5.5", + "illuminate/support": "^5.0", + "nesbot/carbon": "^1.21" + }, + "require-dev" : { + "phpunit/phpunit": "~4.0" + }, + "minimum-stability": "dev" +} diff --git a/src/Verta.php b/src/Verta.php new file mode 100755 index 0000000..a8aab5d --- /dev/null +++ b/src/Verta.php @@ -0,0 +1,833 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +use Closure; +use DateTime; +use DateTimeZone; +use DatePeriod; +use InvalidArgumentException; +use Exception; + + + /********************************* CONFIG *********************************/ + /** + * set Default timezone + */ + date_default_timezone_set('Asia/Tehran'); + +class Verta extends DateTime { + + /***************************** STATCT VARIABLE ****************************/ + + /** + * The day constants + */ + const SATURDAY = 0; + const SUNDAY = 1; + const MONDAY = 2; + const TUESDAY = 3; + const WEDNESDAY = 4; + const THURSDAY = 5; + const FRIDAY = 6; + + /** + * Number unit in date + */ + const YEARS_PER_CENTURY = 100; + const YEARS_PER_DECADE = 10; + const MONTHS_PER_YEAR = 12; + const WEEKS_PER_YEAR = 52; + const WEEKS_PER_MONTH = 4.35; + const DAYS_PER_WEEK = 7; + const HOURS_PER_DAY = 24; + const MINUTES_PER_HOUR = 60; + const SECONDS_PER_MINUTE = 60; + + /** + * Word use in format datetime. + */ + const DEFAULT_TO_STRING_FORMAT = 'Y-m-d H:i:s'; + + /** + * Default format to use for __toString method when type juggling occurs. + */ + const AM = 'ق.ظ'; + const PM = 'ب.ظ'; + const ANTE_MERIDIEM = 'قبل از ظهر'; + const POST_MERIDIEM = 'بعد از ظهر'; + const NUMBER_TH = ' ام'; + const PRE = 'پیش'; + const POST = 'بعد'; + + /** + * Number persian in convertion + */ + const PERSIAN_ZERO = '۰'; + const PERSIAN_ONE = '۱'; + const PERSIAN_TOW = '۲'; + const PERSIAN_THREE = '۳'; + const PERSIAN_FOUR = '۴'; + const PERSIAN_FIVE = '۵'; + const PERSIAN_SIX = '۶'; + const PERSIAN_SEVEN = '۷'; + const PERSIAN_EIGHT = '۸'; + const PERSIAN_NINE = '۹'; + + /** + * Number english in convertion + */ + const ENGLISH_ZERO = '0'; + const ENGLISH_ONE = '1'; + const ENGLISH_TOW = '2'; + const ENGLISH_THREE = '3'; + const ENGLISH_FOUR = '4'; + const ENGLISH_FIVE = '5'; + const ENGLISH_SIX = '6'; + const ENGLISH_SEVEN = '7'; + const ENGLISH_EIGHT = '8'; + const ENGLISH_NINE = '9'; + + /** + * number day in months gregorian + * + * @var array + */ + protected static $daysMonthGregorian = array( + 31, // January + 28, // February + 31, // March + 30, // April + 31, // May + 30, // June + 31, // July + 31, // August + 30, // September + 31, // October + 30, // November + 31, // December + ); + + + /** + * number day in month jalali + * + * @var array + */ + protected static $daysMonthJalali = array( + 31, // فرودین + 31, // اردیبهشت + 31, // خرداد + 31, // تیر + 31, // مرداد + 31, // شهریور + 30, // مهر + 30, // آبان + 30, // آذر + 30, // دی + 30, // بهمن + 29, // اسفند + ); + + /** + * month name jalali + * + * @var array + */ + protected static $monthYear = array( + 'فرودین', // 1 + 'اردیبهشت', // 2 + 'خرداد', // 3 + 'تیر', // 4 + 'مرداد', // 5 + 'شهریور', // 6 + 'مهر', // 7 + 'آبان', // 8 + 'آذر', // 9 + 'دی', // 10 + 'بهمن', // 11 + 'اسفند', // 12 + ); + + /** + * Names of days of the week. + * + * @var array + */ + protected static $dayWeek = array( + 'شنبه', + 'یکشنبه', + 'دوشنبه', + 'سه شنبه', + 'چهارشنبه', + 'پنج شنبه', + 'جمعه', + ); + + /** + * unit date name. + * + * @var array + */ + protected static $unitName = array( + 'ثانیه', + 'دقیقه', + 'ساعت', + 'روز', + 'هفته', + 'ماه', + 'سال', + 'قرن' + ); + + /** + * unit date number. + * + * @var array + */ + protected static $unitNumber = array( + self::SECONDS_PER_MINUTE, + self::MINUTES_PER_HOUR, + self::HOURS_PER_DAY, + self::DAYS_PER_WEEK, + self::WEEKS_PER_MONTH, + self::MONTHS_PER_YEAR, + self::YEARS_PER_DECADE, + ); + + /** + * persian number. + * + * @var array + */ + protected static $persianNumber = array( + self::PERSIAN_ZERO, + self::PERSIAN_ONE, + self::PERSIAN_TOW, + self::PERSIAN_THREE, + self::PERSIAN_FOUR, + self::PERSIAN_FIVE, + self::PERSIAN_SIX, + self::PERSIAN_SEVEN, + self::PERSIAN_EIGHT, + self::PERSIAN_NINE, + ); + + /** + * english number. + * + * @var array + */ + protected static $englishNumber = array( + self::ENGLISH_ZERO, + self::ENGLISH_ONE, + self::ENGLISH_TOW, + self::ENGLISH_THREE, + self::ENGLISH_FOUR, + self::ENGLISH_FIVE, + self::ENGLISH_SIX, + self::ENGLISH_SEVEN, + self::ENGLISH_EIGHT, + self::ENGLISH_NINE, + ); + + /** + * Format to use for __toString method when type juggling occurs. + * + * @var string + */ + protected static $stringFormat = self::DEFAULT_TO_STRING_FORMAT; + + + + /***************************** CONSTRUCT ****************************/ + + /** + * create object of Jalali + * + * @param timestamp $timestamp [optional] + * @param bool $timezone [optional] + */ + public function __construct($datetime = null, $timezone = null) { + + if ($datetime === null) { + $instance = time(); + } + elseif (is_string($datetime)){ + $object = date_create($datetime); + if ($object === false) { + throw new InvalidArgumentException('Unknown datetime ('.$datetime.')'); + } + $instance = $object->getTimestamp(); + } + elseif ($datetime instanceof DateTime || $datetime instanceof Verta) { + $instance = $datetime->getTimestamp(); + } + elseif (is_numeric($datetime)) { + $instance = $datetime; + } + else { + throw new \InvalidArgumentException('Unknown datetime ('.$datetime.')'); + } + $timezone = static::createTimeZone($timezone); + parent::__construct(date('Y-m-d H:i:s.u', $instance), $timezone); + } + + /** + * Create a Jalali instance from a DateTime one + * + * @param DateTime $datetime + * + * @return static + */ + public static function instance(DateTime $datetime) { + return new static($datetime, $datetime->getTimeZone()); + } + + /** + * Create a DateTime instance from Verta + * + * @return DateTime $datetime + */ + public function DateTime() { + return new DateTime(date('Y-m-d H:i:s.u', $this->getTimestamp()), $this->getTimeZone()); + } + + public static function parse($datetime, $timezone = null) { + $parse = date_parse($datetime); + if($parse['error_count'] == 0){ + list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']); + list( $hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']); + $timezone = self::createTimeZone($timezone); + $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone); + return new static($datetime); + } + else{ + throw new \InvalidArgumentException('Unknown datetime ('.$datetime.')'); + } + } + + /** + * @param null $timezone + * @return DateTimeZone|null + */ + protected static function createTimeZone($timezone = null) { + if ($timezone === null) { + return new DateTimeZone('Asia/Tehran'); + } + if ($timezone instanceof DateTimeZone) { + return $timezone; + } + $object = @timezone_open(strval($timezone)); + if ($object === false) { + throw new InvalidArgumentException('Unknown timezone ('.$timezone.')'); + } + + return $object; + } + + /***************************** STRING FORMATED ****************************/ + + /** + * Format the instance as a string using the set format + * + * @return string + */ + public function __toString() + { + return $this->format(static::$stringFormat); + } + + + /** + * 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 date($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 = ''; + + while ($i < $lenghFormat) { + $par = $format{$i}; + if ($par == '\\') { + $result .= $format{ ++$i}; + $i ++; + continue; + } + switch ($par) { + # Day + case 'd': + $result .= sprintf('%02s', $pDay); + break; + + case 'D': + $result .= substr(static::$dayWeek[$pWeek], 0, 2); + break; + + case 'j': + $result .= $pDay; + break; + + case 'l': + $result .= static::$dayWeek[$pWeek]; + break; + + case 'N': + $result .= $pWeek + 1; + break; + + case 'w': + $result .= $pWeek; + break; + + case 'z': + $result .= $this->daysYear($pMonth, $pDay); + break; + + case 'S': + $result .= self::NUMBER_TH; + break; + + # Week + case 'W': + $result .= ceil($this->daysYear($pMonth, $pDay) / 7); + break; + + # Month + case 'F': + $result .= static::$monthYear[$pMonth-1]; + break; + + case 'm': + $result .= sprintf('%02s', $pMonth); + break; + + case 'M': + $result .= substr(static::$monthYear[$pMonth-1], 0, 6); + break; + + case 'n': + $result .= $pMonth; + break; + + case 't': + $result .= $this->daysMonthJalali($pYear,$pMonth); + break; + + # Years + case 'L': + $result .= intval($this->isLeapYear($pYear)); + break; + + case 'Y': + case 'o': + $result .= $pYear; + break; + + case 'y': + $result .= 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': + # Timezone + case 'e': + case 'I': + case 'O': + case 'P': + case 'T': + case 'Z': + $result .= date($par, $timestamp); + break; + + # Full Date/Time + case 'c': + $result .= ($pYear . '-' . $pMonth . '-' . $pDay . ' ' . date('H:i:s P', $timestamp)); + break; + + case 'r': + $result .= (substr(static::$dayWeek[$pWeek], 0, 2) . '، ' . $pDay . ' ' . substr(static::$monthYear[$pMonth], 0, 6) . ' ' . $pYear . ' ' . date('H:i:s P', $timestamp)); + break; + + case 'U': + $result .= $timestamp; + break; + + default: + $result .= $par; + } + $i ++; + } + + return $result; + } + + + /** + * The format of the outputted date string (jalali equivalent of php strftime() function) + * + * @param $format + * @return string + */ + protected function strftime($format) + { + $str_format_code = array( + "%a", + "%A", + "%d", + "%e", + "%j", + "%u", + "%w", + "%U", + "%V", + "%W", + "%b", + "%B", + "%h", + "%m", + "%C", + "%g", + "%G", + "%y", + "%Y", + "%H", + "%I", + "%l", + "%M", + "%p", + "%P", + "%r", + "%R", + "%S", + "%T", + "%X", + "%z", + "%Z", + "%c", + "%D", + "%F", + "%s", + "%x", + "%n", + "%t", + "%%", + ); + + $date_format_code = array( + "D", + "l", + "d", + "j", + "z", + "N", + "w", + "W", + "W", + "W", + "M", + "F", + "M", + "m", + "y", + "y", + "y", + "y", + "Y", + "H", + "h", + "g", + "i", + "A", + "a", + "h:i:s A", + "H:i", + "s", + "H:i:s", + "h:i:s", + "H", + "H", + "D j M H:i:s", + "d/m/y", + "Y-m-d", + "U", + "d/m/y", + "\n", + "\t", + "%", + ); + + //Change Strftime format to Date format + $format = str_replace($str_format_code, $date_format_code, $format); + + //Convert to date + return $this->date($format); + } + + /** + * The format of the outputted date string (jalali equivalent of php strftime() function) + * + * @param $format + * @return string + */ + public function format($format) { + + return $this->strftime($format); + } + + + /** + * get difference to now + * + * @return string + */ + public function diffrentNow() + { + $now = time(); + + // get difference + $difference = $now - $this->getTimestamp(); + + $negative = true; + // set descriptor + if ($difference < 0) { + $difference = abs($difference); // absolute value + $negative = false; + } + + // do math + for ($j = 0; $difference >= static::$unitNumber[$j] and $j < count(static::$unitNumber) - 1; $j++) { + $difference /= static::$unitNumber[$j]; + } + + // round difference + $difference = intval(round($difference)); + $timebound = ($negative ? self::PRE : self::POST); + + return sprintf('%s %s %s', $difference, static::$unitName[$j], $timebound); + } + + /** + * Convert english numbers to persian numbers + * + * @param string $string + * @return string + */ + public static function persianNumbers($string) + { + return str_replace(self::$englishNumber, self::$persianNumber, $string); + } + + /***************************** COMPERTION ****************************/ + + + + /** + * check jalali the instance is a leap year + * + * @param int $year + * @return bool + */ + public static function isLeapYear($year) { + $mod = ($year % 33); + if (($mod == 1) or ( $mod == 5) or ( $mod == 9) or ( $mod == 13) or ( $mod == 17) or ( $mod == 22) or ( $mod == 26) or ( $mod == 30)) { + return true; + } + return false; + } + + + /** + * validate a jalali date (jalali equivalent of php checkdate() function) + * + * @param int $month + * @param int $day + * @param int $year + * @return bool + */ + public static function isValideDate($year, $month, $day) { + return $year >= 1 && $year <= 32766 + && $month >= 1 && $month <= 12 + && $day >= 1 && $day <= static::daysMonthJalali($year, $month); + } + + + /***************************** CONVERTION ****************************/ + + /** + * gregorian to jalali convertion + * + * @param int $g_y + * @param int $g_m + * @param int $g_d + * @return array + */ + public static function getJalali($g_y, $g_m, $g_d) { + $gy = $g_y - 1600; + $gm = $g_m - 1; + $g_day_no = (365 * $gy + static::div($gy + 3, 4) - static::div($gy + 99, 100) + static::div($gy + 399, 400)); + + for ($i = 0; $i < $gm; ++$i) { + $g_day_no += static::$daysMonthGregorian[$i]; + } + + if ($gm > 1 && (($gy % 4 == 0 && $gy % 100 != 0) || ($gy % 400 == 0))) + # leap and after Feb + $g_day_no ++; + $g_day_no += $g_d - 1; + $j_day_no = $g_day_no - 79; + $j_np = static::div($j_day_no, 12053); # 12053 = (365 * 33 + 32 / 4) + $j_day_no = $j_day_no % 12053; + $jy = (979 + 33 * $j_np + 4 * static::div($j_day_no, 1461)); # 1461 = (365 * 4 + 4 / 4) + $j_day_no %= 1461; + + if ($j_day_no >= 366) { + $jy += static::div($j_day_no - 1, 365); + $j_day_no = ($j_day_no - 1) % 365; + } + + for ($i = 0; ($i < 11 && $j_day_no >= static::$daysMonthJalali[$i]); ++$i) { + $j_day_no -= static::$daysMonthJalali[$i]; + } + + return array($jy, $i + 1, $j_day_no + 1); + } + + /** + * jalali to gregorian convertion + * + * @param int $j_y + * @param int $j_m + * @param int $j_d + * @return array + */ + public static function getGregorian($j_y, $j_m, $j_d) { + $jy = $j_y - 979; + $jm = $j_m - 1; + $j_day_no = (365 * $jy + static::div($jy, 33) * 8 + static::div($jy % 33 + 3, 4)); + + for ($i = 0; $i < $jm; ++$i) { + $j_day_no += static::$daysMonthJalali[$i]; + } + + $j_day_no += $j_d - 1; + $g_day_no = $j_day_no + 79; + $gy = (1600 + 400 * static::div($g_day_no, 146097)); # 146097 = (365 * 400 + 400 / 4 - 400 / 100 + 400 / 400) + $g_day_no = $g_day_no % 146097; + $leap = 1; + + if ($g_day_no >= 36525) { # 36525 = (365 * 100 + 100 / 4) + $g_day_no --; + $gy += (100 * static::div($g_day_no, 36524)); # 36524 = (365 * 100 + 100 / 4 - 100 / 100) + $g_day_no = $g_day_no % 36524; + if ($g_day_no >= 365) { + $g_day_no ++; + } else { + $leap = 0; + } + } + + $gy += (4 * static::div($g_day_no, 1461)); # 1461 = (365 * 4 + 4 / 4) + $g_day_no %= 1461; + + if ($g_day_no >= 366) { + $leap = 0; + $g_day_no --; + $gy += static::div($g_day_no, 365); + $g_day_no = ($g_day_no % 365); + } + + for ($i = 0; $g_day_no >= (static::$daysMonthGregorian[$i] + ($i == 1 && $leap)); $i ++) { + $g_day_no -= (static::$daysMonthGregorian[$i] + ($i == 1 && $leap)); + } + + return array($gy, $i + 1, $g_day_no + 1); + } + + /***************************** HELPER ****************************/ + + + /** + * integer division + * + * @param int $a + * @param int $b + * @return int + */ + private static function div($a, $b) { + return ~~($a / $b); + } + + /** + * return day number from first day of year + * + * @param int $month + * @param int $day + * @return type + * @since 5.0.0 + */ + public function daysYear($month, $day) { + $days = 0; + for ($i = 0; $i < $month; $i ++) { + $days += static::$daysMonthJalali[$i]; + } + return ($days + $day); + } + + /** + * return last day of month + * + * @param int $year + * @param int $month + * @return int + */ + public static function daysMonthJalali($year,$month) { + if(static::isLeapYear($year) && ($month == 12)) + return 30; + $month = intval($month); + return static::$daysMonthJalali[$month-1]; + } + +} \ No newline at end of file diff --git a/src/VertaServiceProvider.php b/src/VertaServiceProvider.php new file mode 100755 index 0000000..67b451c --- /dev/null +++ b/src/VertaServiceProvider.php @@ -0,0 +1,42 @@ +app->bind('verta', function ($app) { + return new Verta; + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('verta'); + } +} From 0b6984e64bb8af5675888eee39036462bf907ce3 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Mon, 27 Feb 2017 15:49:00 +0330 Subject: [PATCH 02/22] add readme file --- README.md | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..9e3651e --- /dev/null +++ b/README.md @@ -0,0 +1,115 @@ + +hekmatinasser/verta +====== +- This package compatible with Laravel `>=5` & `< 6.0` + +- Verta is a jalali calendar that was used in persian datetime + +- Calendar conversion is based on the [algorithm provided by Morteza Parvini, Vahid Sohrablou, Roozbeh Pournader and Mohammad Tou'si](http://IranPHP.org). + +Run the Composer update comand + + $ composer require hekmatinasser/verta + +In your `config/app.php` add `'Hekmatinasser\Verta\VertaServiceProvider::class'` to the end of the `$providers` array + +```php +'providers' => [ + + Illuminate\Foundation\Providers\ArtisanServiceProvider::class, + Illuminate\Auth\AuthServiceProvider::class, + ... + Hekmatinasser\Verta\VertaServiceProvider::class, + +], + + +'alias' => [ + ... + Verta' => Hekmatinasser\Verta\Verta::class, +] +``` + + +## Basic Usage + +### Verta +You can use verta `use \Verta as Verta ;` +#### `$dt = new Verta();` +``` php +// default timestamp is now +$dt = new Verta(); + +// pass timestamps +$dt = new Verta(1333857600); +// OR +$dt = new Verta('2016-12-27 14:12:32'); + +//pass datetime object +$dt = new \Datetime(); +return new Verta($dt); // 1395-12-09 15:05:56 + +// pass carbon object +$c = Carbon::now(); +return new Verta($c); // 1395-12-09 15:05:56 + +// pass strings persian date +$dt = Verta::parse('1395-10-07 14:12:32'); + +// format the timestamp +$dt = new Verta('2016-12-27 14:12:32'); +return $dt->format('Y-n-j H:i'); //1395-10-7 14:12 +return $dt->format('%B %d، %Y'); // دی 07، 1395 +return $dt; //1395-10-07 14:12:32 + +// get a diffrent time with now +$dt = Verta::parse('1395-10-07 14:12:32'); +return $dt->diffrentNow(); // 2 ماه پیش + +$dt = Verta::parse('1396-10-07 14:12:32'); +return $dt->diffrentNow(); // 10 ماه بعد +``` + +--- +#### `getJalali($gYear, $gMonth, $gDay)` +```php +return Verta::getJalali(2015,12,25); // [1394,10,4] +``` +--- +#### `getGregorian($jYear, $jMonth, $jDay)` +```php +return Verta::getGregorian(1394,10,4);; // [2015,12,25] +``` + +--- +#### `DateTime()` +```php +$dt = Verta::parse('1395/01/05 23:50:25'); +return Carbon::instance($dt->DateTime()); + +``` +--- +#### `isLeapYear($int)` +```php +echo Verta::isLeapYear(1394); // false +echo Verta::isLeapYear(1395); // true +``` +--- +#### `isValideDate($jYear, $jMonth, $jDay)` +```php +echo Verta::isValideDate(1394, 12, 30); // false +echo Verta::isValideDate(1395, 12, 30); // true +``` +--- +#### `persianNumbers($string)` +```php +$dt = Verta::parse('1396-10-07 14:12:32'); +return Verta::persianNumbers($dt); // ۱۳۹۶-۱۰-۰۷ ۱۴:۱۲:۳۲ +``` +--- +## Formatting ## + +For help in building your formats, checkout the [PHP strftime() docs](http://php.net/manual/en/function.strftime.php). + +## License ## +- This package was created and modified by [Nasser Hekmati](https://github.com/hekmatinasser) for Laravel >= 5 and is released under the MIT License. From fe25a6ad85c71c556a8bf2684cefa512e968defa Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Mon, 27 Feb 2017 23:39:36 +0330 Subject: [PATCH 03/22] fix bug in use define package --- LICENSE~ | 20 -------------------- composer.json~ | 21 --------------------- src/VertaServiceProvider.php | 3 +-- 3 files changed, 1 insertion(+), 43 deletions(-) delete mode 100755 LICENSE~ delete mode 100755 composer.json~ mode change 100755 => 100644 src/VertaServiceProvider.php diff --git a/LICENSE~ b/LICENSE~ deleted file mode 100755 index 8d2fb98..0000000 --- a/LICENSE~ +++ /dev/null @@ -1,20 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2016 Morilog - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/composer.json~ b/composer.json~ deleted file mode 100755 index 3f283cd..0000000 --- a/composer.json~ +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "hekmatinasser/jalali", - "description": "This Package helps developers to work with Jalali Date and Time class.", - "license": "MIT", - "authors": [ - { - "name": "Nasser Hekmati", - "email": "hekmati.nasser@gmail.com" - } - ], - "keywords": ["Laravel","Date","Datetime","Jalali", "Shamsi"], - "require": { - "php": ">=5.5", - "illuminate/support": "^5.0", - "nesbot/carbon": "^1.21" - }, - "require-dev" : { - "phpunit/phpunit": "~4.0" - }, - "minimum-stability": "dev" -} diff --git a/src/VertaServiceProvider.php b/src/VertaServiceProvider.php old mode 100755 new mode 100644 index 67b451c..02057e3 --- a/src/VertaServiceProvider.php +++ b/src/VertaServiceProvider.php @@ -6,8 +6,6 @@ use Illuminate\Support\ServiceProvider; class VertaServiceProvider extends ServiceProvider { - - protected $defer = false; /** * Bootstrap the application services. * @@ -30,6 +28,7 @@ class VertaServiceProvider extends ServiceProvider }); } + /** * Get the services provided by the provider. * From 0b84f1a82431bb4e7ae347f35e26428c060ec890 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Tue, 28 Feb 2017 01:23:24 +0330 Subject: [PATCH 04/22] fix bug composer.json --- composer.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dc1e381..2bd5835 100755 --- a/composer.json +++ b/composer.json @@ -12,6 +12,11 @@ "require": { "php": ">=5.5" }, - "require-dev" : {}, + "require-dev" : {}, + "autoload": { + "psr-4": { + "Hekmatinasser\\Verta\\": "src" + } + }, "minimum-stability": "dev" } From 3a5a7d1ab87fcdbe1a0c55d176dac97b9aec6716 Mon Sep 17 00:00:00 2001 From: hekmatinasser Date: Tue, 28 Feb 2017 01:29:03 +0330 Subject: [PATCH 05/22] fix error alias array --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e3651e..0c27630 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ In your `config/app.php` add `'Hekmatinasser\Verta\VertaServiceProvider::class'` 'alias' => [ ... - Verta' => Hekmatinasser\Verta\Verta::class, + 'Verta' => Hekmatinasser\Verta\Verta::class, ] ``` From 55ba79a83fac1530b81a52191ddcafe0da6e9616 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Tue, 28 Feb 2017 22:09:19 +0330 Subject: [PATCH 06/22] add instance method for access static --- README.md | 13 +++++++++---- src/Verta.php | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9e3651e..ffacfdf 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ hekmatinasser/verta - Verta is a jalali calendar that was used in persian datetime -- Calendar conversion is based on the [algorithm provided by Morteza Parvini, Vahid Sohrablou, Roozbeh Pournader and Mohammad Tou'si](http://IranPHP.org). +- Calendar conversion is based on the algorithm provided by Morteza Parvini, Vahid Sohrablou, Roozbeh Pournader and Mohammad Tou'si. Run the Composer update comand @@ -34,7 +34,8 @@ In your `config/app.php` add `'Hekmatinasser\Verta\VertaServiceProvider::class'` ## Basic Usage ### Verta -You can use verta `use \Verta as Verta ;` +You can use verta +`use \Verta as Verta ;` #### `$dt = new Verta();` ``` php // default timestamp is now @@ -42,7 +43,8 @@ $dt = new Verta(); // pass timestamps $dt = new Verta(1333857600); -// OR + +// pass string datetime $dt = new Verta('2016-12-27 14:12:32'); //pass datetime object @@ -50,9 +52,12 @@ $dt = new \Datetime(); return new Verta($dt); // 1395-12-09 15:05:56 // pass carbon object -$c = Carbon::now(); +$c = \Carbon::now(); return new Verta($c); // 1395-12-09 15:05:56 +// access static +return Verta::instance('2016-12-25 11:12:36'); //1395-10-05 11:12:36 + // pass strings persian date $dt = Verta::parse('1395-10-07 14:12:32'); diff --git a/src/Verta.php b/src/Verta.php index a8aab5d..726d8c8 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -288,8 +288,8 @@ class Verta extends DateTime { * * @return static */ - public static function instance(DateTime $datetime) { - return new static($datetime, $datetime->getTimeZone()); + public static function instance($datetime = null, $timezone = null) { + return new static($datetime, $timezone); } /** From 9b07e845055765e3a2563ebf32b3f02da796274c Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Wed, 1 Mar 2017 00:05:56 +0330 Subject: [PATCH 07/22] add predefine format and now method --- README.md | 24 ++++++++++++++--- src/Verta.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4b5bac3..61e3e24 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ You can use verta ``` php // default timestamp is now $dt = new Verta(); +//OR +$dt = Verta::now(); +//OR +$dt = Verta::instance('2016-12-25 11:12:36'); // pass timestamps $dt = new Verta(1333857600); @@ -55,18 +59,30 @@ return new Verta($dt); // 1395-12-09 15:05:56 $c = \Carbon::now(); return new Verta($c); // 1395-12-09 15:05:56 -// access static -return Verta::instance('2016-12-25 11:12:36'); //1395-10-05 11:12:36 - // pass strings persian date $dt = Verta::parse('1395-10-07 14:12:32'); // format the timestamp $dt = new Verta('2016-12-27 14:12:32'); -return $dt->format('Y-n-j H:i'); //1395-10-7 14:12 +return $dt->format('Y-n-j H:i'); // 1395-10-7 14:12 return $dt->format('%B %d، %Y'); // دی 07، 1395 return $dt; //1395-10-07 14:12:32 +// use predefined format +$dt = new Verta(); +return $dt->format('datetime'); // 1395-12-10 23:25:12 +return $dt->format('date'); // 1395-12-10 +return $dt->format('time'); // 23:26:35 + +// use predefined format method +$dt = new Verta(); +return $dt->formatDatetime(); // 1395-12-10 23:37:26 +return $dt->formatDate(); // 1395-12-10 +return $dt->formatTime(); // 23:26:35 +return $dt->formatPersianDatetime(); // 1395/12/10 23:46:09 +return $dt->formatPersianDate(); // 1395/12/10 + + // get a diffrent time with now $dt = Verta::parse('1395-10-07 14:12:32'); return $dt->diffrentNow(); // 2 ماه پیش diff --git a/src/Verta.php b/src/Verta.php index 726d8c8..26be5e6 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -239,6 +239,15 @@ class Verta extends DateTime { self::ENGLISH_NINE, ); + /** + * @var array + */ + protected static $formats = array( + 'datetime' => 'Y-m-d H:i:s', + 'date' => 'Y-m-d', + 'time' => 'H:i:s', + ); + /** * Format to use for __toString method when type juggling occurs. * @@ -281,10 +290,21 @@ class Verta extends DateTime { parent::__construct(date('Y-m-d H:i:s.u', $instance), $timezone); } + /** + * Create a Jalali now Datetime + * + * + * @return static + */ + public static function now() { + return new static(); + } + /** * Create a Jalali instance from a DateTime one * - * @param DateTime $datetime + * @param timestamp $timestamp [optional] + * @param bool $timezone [optional] * * @return static */ @@ -614,9 +634,62 @@ class Verta extends DateTime { */ public function format($format) { + if (in_array($format, array_keys(self::$formats))) { + $format = self::$formats[$format]; + } return $this->strftime($format); } + /** + * Format the instance as date + * + * @return string + */ + public function formatDatetime() + { + return $this->format('Y-m-d H:i:s'); + } + + /** + * Format the instance as date + * + * @return string + */ + public function formatDate() + { + return $this->format('Y-m-d'); + } + + /** + * Format the instance as time + * + * @return string + */ + public function formatTime() + { + return $this->format('H:i:s'); + } + + /** + * Format the instance as date + * + * @return string + */ + public function formatPersianDatetime() + { + return $this->format('Y/n/j H:i:s'); + } + + /** + * Format the instance as date + * + * @return string + */ + public function formatPersianDate() + { + return $this->format('Y/n/j'); + } + /** * get difference to now From d9ea2e2edff94b761cdcd9164801a1b795bfd4e2 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Wed, 1 Mar 2017 00:34:55 +0330 Subject: [PATCH 08/22] add travis file --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..03d149b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: php + +php: + - 5.5 + - 5.6 + +before_script: + - curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer + From 65e7c1fd5d65548a5a3ba440b89ce35b80cc2440 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Thu, 2 Mar 2017 00:19:58 +0330 Subject: [PATCH 09/22] add set and reset default format --- README.md | 7 +++++++ src/Verta.php | 55 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 61e3e24..afb0d78 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,13 @@ return $dt->formatTime(); // 23:26:35 return $dt->formatPersianDatetime(); // 1395/12/10 23:46:09 return $dt->formatPersianDate(); // 1395/12/10 +// set default format +Verta::setStringformat('Y/n/j H:i:s'); +return new Verta(); // 1395/12/12 00:11:35 + +// reset default format +Verta::resetStringFormat(); +return new Verta(); // 1395-12-12 00:18:04 // get a diffrent time with now $dt = Verta::parse('1395-10-07 14:12:32'); diff --git a/src/Verta.php b/src/Verta.php index 26be5e6..5daca48 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -55,7 +55,23 @@ class Verta extends DateTime { /** * Word use in format datetime. */ - const DEFAULT_TO_STRING_FORMAT = 'Y-m-d H:i:s'; + const DEFAULT_STRING_FORMAT = 'Y-m-d H:i:s'; + + /** + * Format to use for __toString. + * + * @var string + */ + protected static $stringFormat = self::DEFAULT_STRING_FORMAT; + + /** + * @var array + */ + protected static $formats = array( + 'datetime' => 'Y-m-d H:i:s', + 'date' => 'Y-m-d', + 'time' => 'H:i:s', + ); /** * Default format to use for __toString method when type juggling occurs. @@ -239,24 +255,6 @@ class Verta extends DateTime { self::ENGLISH_NINE, ); - /** - * @var array - */ - protected static $formats = array( - 'datetime' => 'Y-m-d H:i:s', - 'date' => 'Y-m-d', - 'time' => 'H:i:s', - ); - - /** - * Format to use for __toString method when type juggling occurs. - * - * @var string - */ - protected static $stringFormat = self::DEFAULT_TO_STRING_FORMAT; - - - /***************************** CONSTRUCT ****************************/ /** @@ -356,6 +354,25 @@ class Verta extends DateTime { /***************************** STRING FORMATED ****************************/ + + /** + * Reset the format used to the default when type juggling a Carbon instance to a string + */ + public static function resetStringFormat() + { + static::setStringFormat(static::DEFAULT_STRING_FORMAT); + } + + /** + * Set the default format used when type juggling a Carbon instance to a string + * + * @param string $format + */ + public static function setStringFormat($format) + { + static::$stringFormat = $format; + } + /** * Format the instance as a string using the set format * From 8f7bc03f34d8b1791286edb442070116f5ff3531 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Thu, 2 Mar 2017 14:22:41 +0330 Subject: [PATCH 10/22] change diffNow method --- README.md | 119 ++++++++++++++++++++++++++------------------------ src/Verta.php | 2 +- 2 files changed, 62 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index afb0d78..a2cb773 100644 --- a/README.md +++ b/README.md @@ -11,20 +11,18 @@ Run the Composer update comand $ composer require hekmatinasser/verta -In your `config/app.php` add `'Hekmatinasser\Verta\VertaServiceProvider::class'` to the end of the `$providers` array +In your `config/app.php` add `Hekmatinasser\Verta\VertaServiceProvider::class,` to the end of the `providers` array and add `'Verta' => Hekmatinasser\Verta\Verta::class,` to the end of the `alias` array. ```php 'providers' => [ - Illuminate\Foundation\Providers\ArtisanServiceProvider::class, - Illuminate\Auth\AuthServiceProvider::class, ... Hekmatinasser\Verta\VertaServiceProvider::class, ], - 'alias' => [ + ... 'Verta' => Hekmatinasser\Verta\Verta::class, ] @@ -34,23 +32,26 @@ In your `config/app.php` add `'Hekmatinasser\Verta\VertaServiceProvider::class'` ## Basic Usage ### Verta -You can use verta -`use \Verta as Verta ;` -#### `$dt = new Verta();` -``` php +You can use verta in project +```php +use \Verta as Verta ;` +``` + +#### Constructors +```php // default timestamp is now $dt = new Verta(); -//OR +// OR $dt = Verta::now(); -//OR + +// pass string datetime +$dt = new Verta('2016-12-27 14:12:32'); +// OR $dt = Verta::instance('2016-12-25 11:12:36'); // pass timestamps $dt = new Verta(1333857600); -// pass string datetime -$dt = new Verta('2016-12-27 14:12:32'); - //pass datetime object $dt = new \Datetime(); return new Verta($dt); // 1395-12-09 15:05:56 @@ -61,7 +62,10 @@ return new Verta($c); // 1395-12-09 15:05:56 // pass strings persian date $dt = Verta::parse('1395-10-07 14:12:32'); - +``` +--- +#### Formating +```php // format the timestamp $dt = new Verta('2016-12-27 14:12:32'); return $dt->format('Y-n-j H:i'); // 1395-10-7 14:12 @@ -90,54 +94,53 @@ return new Verta(); // 1395/12/12 00:11:35 Verta::resetStringFormat(); return new Verta(); // 1395-12-12 00:18:04 -// get a diffrent time with now -$dt = Verta::parse('1395-10-07 14:12:32'); -return $dt->diffrentNow(); // 2 ماه پیش - -$dt = Verta::parse('1396-10-07 14:12:32'); -return $dt->diffrentNow(); // 10 ماه بعد -``` - ---- -#### `getJalali($gYear, $gMonth, $gDay)` -```php -return Verta::getJalali(2015,12,25); // [1394,10,4] -``` ---- -#### `getGregorian($jYear, $jMonth, $jDay)` -```php -return Verta::getGregorian(1394,10,4);; // [2015,12,25] -``` - ---- -#### `DateTime()` -```php -$dt = Verta::parse('1395/01/05 23:50:25'); -return Carbon::instance($dt->DateTime()); - -``` ---- -#### `isLeapYear($int)` -```php -echo Verta::isLeapYear(1394); // false -echo Verta::isLeapYear(1395); // true -``` ---- -#### `isValideDate($jYear, $jMonth, $jDay)` -```php -echo Verta::isValideDate(1394, 12, 30); // false -echo Verta::isValideDate(1395, 12, 30); // true -``` ---- -#### `persianNumbers($string)` -```php +// change english number to persian $dt = Verta::parse('1396-10-07 14:12:32'); return Verta::persianNumbers($dt); // ۱۳۹۶-۱۰-۰۷ ۱۴:۱۲:۳۲ ``` ---- -## Formatting ## - For help in building your formats, checkout the [PHP strftime() docs](http://php.net/manual/en/function.strftime.php). +--- +#### Differences +```php +// get a diffrent time with now +$dt = Verta::parse('1395-10-07 14:12:32'); +return $dt->diffNow(); // 2 ماه پیش + +$dt = Verta::parse('1395/12/12 14:13:50'); +return $dt->diffNow(); // 5 ثانیه بعد +``` + +--- +#### Transformations +```php +// get jalali a gregorian date +return Verta::getJalali(2015,12,25); // [1394,10,4] + +// get gregorian a jalali date +return Verta::getGregorian(1394,10,4); // [2015,12,25] + +// export to datetime object +$dt = Verta::parse('1395/01/05 23:50:25'); +$dt = $v->DateTime(); + +// export to carbon object +$dt = Verta::parse('1395/01/05 23:50:25'); +$c = Carbon::instance($v->DateTime()); +``` + +--- +#### Comparisons +```php +// is leap year +echo Verta::isLeapYear(1394); // false +echo Verta::isLeapYear(1395); // true + +// is valid date +echo Verta::isValideDate(1394, 12, 30); // false +echo Verta::isValideDate(1395, 12, 30); // true +``` + +--- ## License ## - This package was created and modified by [Nasser Hekmati](https://github.com/hekmatinasser) for Laravel >= 5 and is released under the MIT License. diff --git a/src/Verta.php b/src/Verta.php index 5daca48..3727b3a 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -713,7 +713,7 @@ class Verta extends DateTime { * * @return string */ - public function diffrentNow() + public function diffNow() { $now = time(); From fcb6e978d943b223872928e9d581f0a12ef267fd Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Sat, 4 Mar 2017 11:31:23 +0330 Subject: [PATCH 11/22] add create methods --- README.md | 11 +++++ src/Verta.php | 119 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 123 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a2cb773..d1cfbe4 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,13 @@ return new Verta($c); // 1395-12-09 15:05:56 // pass strings persian date $dt = Verta::parse('1395-10-07 14:12:32'); + +// create from a specific date and time +return Verta::create(); // 1395-12-14 11:17:01 equal now() +return Verta::create(1394,12,29,15,51,5); // 1394-12-29 15:51:05 +return Verta::createDate(1394,12,29); // 1394-12-29 11:18:29 set time now +return Verta::createTime(15,51,5); // 1395-12-14 15:51:05 set date now +return Verta::createTimestamp(1488614023); // 1395-12-14 11:23:43 ``` --- #### Formating @@ -139,6 +146,10 @@ echo Verta::isLeapYear(1395); // true // is valid date echo Verta::isValideDate(1394, 12, 30); // false echo Verta::isValideDate(1395, 12, 30); // true + +// is valid time +echo Verta::isValideTime(15, 62, 50); // false +echo Verta::isValideTime(15, 12, 30); // true ``` --- diff --git a/src/Verta.php b/src/Verta.php index 3727b3a..5fdf8dc 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -289,7 +289,7 @@ class Verta extends DateTime { } /** - * Create a Jalali now Datetime + * Create a Verta now datetime * * * @return static @@ -299,9 +299,9 @@ class Verta extends DateTime { } /** - * Create a Jalali instance from a DateTime one + * Create a Verta instance from a DateTime one * - * @param timestamp $timestamp [optional] + * @param timestamp $datetime [optional] * @param bool $timezone [optional] * * @return static @@ -319,20 +319,110 @@ class Verta extends DateTime { return new DateTime(date('Y-m-d H:i:s.u', $this->getTimestamp()), $this->getTimeZone()); } + + /** + * Create a Verta instance from a DateTime one + * + * @param timestamp $datetime [optional] + * @param bool $timezone [optional] + * + * @return static + */ public static function parse($datetime, $timezone = null) { $parse = date_parse($datetime); + if($parse['error_count'] == 0){ list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']); - list( $hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']); - $timezone = self::createTimeZone($timezone); - $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone); - return new static($datetime); + list( $hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']); + + $timezone = self::createTimeZone($timezone); + $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone); + return new static($datetime); } else{ throw new \InvalidArgumentException('Unknown datetime ('.$datetime.')'); } } + /** + * Create a new Verta instance from a specific date and time. + * + * If any of feild are set to null their now() values will + * be used. + * + * @param int|null $year + * @param int|null $month + * @param int|null $day + * @param int|null $hour + * @param int|null $minute + * @param int|null $second + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + + public static function create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) + { + $now = static::instance(null, $timezone)->format('Y-n-j-G-i-s'); + $defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now)); + + $year = $year === null ? intval($defaults['year']) : $year; + $month = $month === null ? intval($defaults['month']) : $month; + $day = $day === null ? intval($defaults['day']) : $day; + $hour = $hour === null ? intval($defaults['hour']) : $hour; + $minute = $minute === null ? intval($defaults['minute']) : $minute; + $second = $second === null ? intval($defaults['second']) : $second; + + if (!static::isValideDate($year, $month, $day) || !static::isValideTime($hour, $minute, $second)) { + throw new \InvalidArgumentException('Unknown datetime'); + } + + return static::parse(sprintf('%s-%s-%s %s:%s:%s', $year, $month, $day, $hour, $minute, $second)); + } + + /** + * Create a Verta from just a date. + * + * @param int|null $year + * @param int|null $month + * @param int|null $day + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createDate($year = null, $month = null, $day = null, $timezone = null) + { + return static::create($year, $month, $day, null, null, null, $timezone); + } + + /** + * Create a Verta instance from just a time. + * + * @param int|null $hour + * @param int|null $minute + * @param int|null $second + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createTime($hour = null, $minute = null, $second = null, $timezone = null) + { + return static::create(null, null, null, $hour, $minute, $second, $timezone); + } + + /** + * Create a Verta instance from a timestamp. + * + * @param int $timestamp + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createTimestamp($timestamp, $timezone = null) + { + return static::instance($timestamp, $timezone); + } + /** * @param null $timezone * @return DateTimeZone|null @@ -783,6 +873,21 @@ class Verta extends DateTime { && $day >= 1 && $day <= static::daysMonthJalali($year, $month); } + + /** + * validate a time + * + * @param int $hour + * @param int $minute + * @param int $second + * @return bool + */ + public static function isValideTime($hour, $minute, $second) { + return $hour >= 0 && $hour <= 24 + && $minute >= 0 && $minute <= 59 + && $second >= 0 && $second <= 59; + } + /***************************** CONVERTION ****************************/ From 6175fba77a428afc3bc6709b847bda472ef19d93 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Sat, 4 Mar 2017 12:07:07 +0330 Subject: [PATCH 12/22] edit travis file --- .travis.yml | 9 --------- README.md | 1 - 2 files changed, 10 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 03d149b..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: php - -php: - - 5.5 - - 5.6 - -before_script: - - curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer - diff --git a/README.md b/README.md index d1cfbe4..b2fa655 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - hekmatinasser/verta ====== - This package compatible with Laravel `>=5` & `< 6.0` From eea78fe25a44487fda20fddfb199565f038616d4 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Tue, 2 May 2017 23:15:08 +0430 Subject: [PATCH 13/22] add calculation year --- src/Verta.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Verta.php b/src/Verta.php index 5fdf8dc..c4f510c 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -888,8 +888,62 @@ class Verta extends DateTime { && $second >= 0 && $second <= 59; } - - /***************************** CONVERTION ****************************/ + + /***************************** CALCULATION ****************************/ + + /** + * Add years to the instance. Positive $value travel forward while + * negative $value travel into the past. + * + * @param int $value + * + * @return static + */ + public function addYears($value) + { + return $this->modify((int) $value.' year'); + } + + /** + * Add a year to the instance + * + * @param int $value + * + * @return static + */ + public function addYear($value = 1) + { + return $this->addYears($value); + } + + /** + * Remove years from the instance. + * + * @param int $value + * + * @return static + */ + public function subYears($value) + { + return $this->addYears(-1 * $value)->modify('-1 day'); + } + + /** + * Remove a year from the instance + * + * @param int $value + * + * @return static + */ + public function subYear($value = 1) + { + return $this->subYears($value); + } + + + + + /***************************** CONVERTION ****************************/ /** * gregorian to jalali convertion From 04993d769ba158da01dd39c653d690e54ec5289d Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Fri, 5 May 2017 23:56:16 +0430 Subject: [PATCH 14/22] calculation day, week, hour, minute, second --- src/Verta.php | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) diff --git a/src/Verta.php b/src/Verta.php index c4f510c..c548def 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -940,8 +940,260 @@ class Verta extends DateTime { return $this->subYears($value); } + /** + * Add days to the instance. Positive $value travels forward while + * negative $value travels into the past. + * + * @param int $value + * + * @return static + */ + public function addDays($value) + { + return $this->modify((int) $value.' day'); + } + + /** + * Add a day to the instance + * + * @param int $value + * + * @return static + */ + public function addDay($value = 1) + { + return $this->addDays($value); + } + + /** + * Remove a day from the instance + * + * @param int $value + * + * @return static + */ + public function subDay($value = 1) + { + return $this->subDays($value); + } + + /** + * Remove days from the instance + * + * @param int $value + * + * @return static + */ + public function subDays($value) + { + return $this->addDays(-1 * $value); + } + + /** + * Add weekdays to the instance. Positive $value travels forward while + * negative $value travels into the past. + * + * @param int $value + * + * @return static + */ + /** + * Add weeks to the instance. Positive $value travels forward while + * negative $value travels into the past. + * + * @param int $value + * + * @return static + */ + public function addWeeks($value) + { + return $this->modify((int) $value.' week'); + } + + /** + * Add a week to the instance + * + * @param int $value + * + * @return static + */ + public function addWeek($value = 1) + { + return $this->addWeeks($value); + } + + /** + * Remove a week from the instance + * + * @param int $value + * + * @return static + */ + public function subWeek($value = 1) + { + return $this->subWeeks($value); + } + + /** + * Remove weeks to the instance + * + * @param int $value + * + * @return static + */ + public function subWeeks($value) + { + return $this->addWeeks(-1 * $value); + } + + /** + * Add hours to the instance. Positive $value travels forward while + * negative $value travels into the past. + * + * @param int $value + * + * @return static + */ + public function addHours($value) + { + return $this->modify((int) $value.' hour'); + } + + /** + * Add an hour to the instance + * + * @param int $value + * + * @return static + */ + public function addHour($value = 1) + { + return $this->addHours($value); + } + + /** + * Remove an hour from the instance + * + * @param int $value + * + * @return static + */ + public function subHour($value = 1) + { + return $this->subHours($value); + } + + /** + * Remove hours from the instance + * + * @param int $value + * + * @return static + */ + public function subHours($value) + { + return $this->addHours(-1 * $value); + } + + /** + * Add minutes to the instance. Positive $value travels forward while + * negative $value travels into the past. + * + * @param int $value + * + * @return static + */ + public function addMinutes($value) + { + return $this->modify((int) $value.' minute'); + } + + /** + * Add a minute to the instance + * + * @param int $value + * + * @return static + */ + public function addMinute($value = 1) + { + return $this->addMinutes($value); + } + + /** + * Remove a minute from the instance + * + * @param int $value + * + * @return static + */ + public function subMinute($value = 1) + { + return $this->subMinutes($value); + } + + /** + * Remove minutes from the instance + * + * @param int $value + * + * @return static + */ + public function subMinutes($value) + { + return $this->addMinutes(-1 * $value); + } + + /** + * Add seconds to the instance. Positive $value travels forward while + * negative $value travels into the past. + * + * @param int $value + * + * @return static + */ + public function addSeconds($value) + { + return $this->modify((int) $value.' second'); + } + + /** + * Add a second to the instance + * + * @param int $value + * + * @return static + */ + public function addSecond($value = 1) + { + return $this->addSeconds($value); + } + + /** + * Remove a second from the instance + * + * @param int $value + * + * @return static + */ + public function subSecond($value = 1) + { + return $this->subSeconds($value); + } + + /** + * Remove seconds from the instance + * + * @param int $value + * + * @return static + */ + public function subSeconds($value) + { + return $this->addSeconds(-1 * $value); + } /***************************** CONVERTION ****************************/ From 46b80250271181e875eee2f7f70ee16d68a2cb19 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Mon, 8 May 2017 23:43:37 +0430 Subject: [PATCH 15/22] add and sub month --- src/Verta.php | 86 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/src/Verta.php b/src/Verta.php index c548def..1846ce6 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -940,6 +940,83 @@ class Verta extends DateTime { return $this->subYears($value); } + /** + * Add months to the instance. Positive $value travels forward while + * negative $value travels into the past. + * + * @param int $value + * + * @return static + */ + public function addMonths($value) + { + list($year, $month, $day, $hour, $minute, $second) = explode('-', $this->format('Y-m-d-H-i-s')); + $month += $value; + if ($month > 12) { + $year += intval($month / 12); + $month = $month % 12; + } + if ($month < 1) { + $year += intval($month / 12) - 1; + $month = 12 + ($month % 12); + } + if ($value > 0) { + if ($month > 6 && $day == 31) { + $day--; + } + if ($month == 12 && !self::isLeapYear($year)) { + $day--; + } + } + else { + if ($month == 12 && $day == 31) { + if (self::isLeapYear($year)) { + $day = 30; + } + else { + $day = 29; + } + } + } + return self::create($year, $month, $day, $hour, $minute, $second, $this->getTimeZone()); + } + + /** + * Add a month to the instance + * + * @param int $value + * + * @return static + */ + public function addMonth($value = 1) + { + return $this->addMonths($value); + } + + /** + * Remove a month from the instance + * + * @param int $value + * + * @return static + */ + public function subMonth($value = 1) + { + return $this->subMonths($value); + } + + /** + * Remove months from the instance + * + * @param int $value + * + * @return static + */ + public function subMonths($value) + { + return $this->addMonths(-1 * $value); + } + /** * Add days to the instance. Positive $value travels forward while * negative $value travels into the past. @@ -989,15 +1066,6 @@ class Verta extends DateTime { return $this->addDays(-1 * $value); } - /** - * Add weekdays to the instance. Positive $value travels forward while - * negative $value travels into the past. - * - * @param int $value - * - * @return static - */ - /** * Add weeks to the instance. Positive $value travels forward while From 9dde35ba81c795aa8dd45c019a781c747d23f7b8 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Tue, 9 May 2017 00:08:38 +0430 Subject: [PATCH 16/22] change readme file --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index b2fa655..69e112d 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,48 @@ return Verta::persianNumbers($dt); // ۱۳۹۶-۱۰-۰۷ ۱۴:۱۲:۳۲ ``` For help in building your formats, checkout the [PHP strftime() docs](http://php.net/manual/en/function.strftime.php). +--- +#### Calculation +```php +// add and sub unit datetime +$dt = Verta::parse('1395-10-07 14:12:32'); +return $dt->addYear(); // 1396-10-07 14:12:32 +return $dt->addYears(4); // 1399-10-07 14:12:32 +return $dt->subYear(); // 1394-10-07 14:12:32 +return $dt->subYears(2); // 1393-10-07 14:12:32 + +return $dt->addMonth(); // 1395-11-07 14:12:32 +return $dt->addMonths(5); // 1396-03-07 14:12:32 +return $dt->subMonth(); // 1395-09-07 14:12:32 +return $dt->subMonths(2); // 1395-08-07 14:12:32 + +return $dt->addWeek(); // 1395-10-12 14:12:32 +return $dt->addWeeks(3); // 1395-10-26 14:12:32 +return $dt->subWeek(); // 1395-09-30 14:12:32 +return $dt->subWeeks(2); // 1395-09-27 14:12:32 + +return $dt->addDay(); // 1395-10-08 14:12:32 +return $dt->addDays(3); // 1395-10-11 14:12:32 +return $dt->subDay(); // 1395-10-06 14:12:32 +return $dt->subDays(2); // 1395-09-05 14:12:32 + +return $dt->addHour(); // 1395-10-07 15:12:32 +return $dt->addHours(5); // 1395-10-07 19:12:32 +return $dt->subHour(); // 1395-10-07 13:12:32 +return $dt->subHours(2); // 1395-10-07 12:12:32 + +return $dt->addMinute(); // 1395-10-07 14:13:32 +return $dt->addMinutes(3); // 1395-10-07 14:15:32 +return $dt->subMinute(); // 1395-10-07 14:11:32 +return $dt->subMinutes(2); // 1395-10-07 14:10:32 + +return $dt->addSecond(); // 1395-10-07 14:12:33 +return $dt->addSeconds(3); // 1395-10-07 14:12:35 +return $dt->subSecond(); // 1395-10-07 14:12:31 +return $dt->subSeconds(2); // 1395-10-07 14:12:30 + +``` + --- #### Differences ```php From 7a882fe0aa5c1717ecf1805bcf27b160ac63e079 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Fri, 19 May 2017 23:11:05 +0430 Subject: [PATCH 17/22] add number to word method --- README.md | 17 ++ src/Verta.php | 492 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 407 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 69e112d..6c7f1cc 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,23 @@ echo Verta::isValideTime(15, 62, 50); // false echo Verta::isValideTime(15, 12, 30); // true ``` +--- +#### Exchange n2w +```php +// get word of number +// max number size 39 digit +echo Verta::getWords('455000000000000000000000000000000000000'); // چهارصد و پنجاه و پنج دسیلیون +echo Verta::getWords('4550000'); // چهار میلیون و پانصد و پنجاه هزار +echo Verta::getWords('4550000', 'fa'); // چهار میلیون و پانصد و پنجاه هزار +echo Verta::getPersianWords('4550000'); // چهار میلیون و پانصد و پنجاه هزار +echo Verta::getWords('4550000', 'en'); // four million and five hundred and fifty thousand +echo Verta::getEnglishWords('4550000'); // four million and five hundred and fifty thousand + +// add currency persian +echo Verta::getRial('4550000'); // چهار میلیون و پانصد و پنجاه هزار ریال +echo Verta::getToman('4550000'); // چهار میلیون و پانصد و پنجاه هزار تومان +``` + --- ## License ## - This package was created and modified by [Nasser Hekmati](https://github.com/hekmatinasser) for Laravel >= 5 and is released under the MIT License. diff --git a/src/Verta.php b/src/Verta.php index 1846ce6..27447e5 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -55,22 +55,22 @@ class Verta extends DateTime { /** * Word use in format datetime. */ - const DEFAULT_STRING_FORMAT = 'Y-m-d H:i:s'; + const DEFAULT_STRING_FORMAT = 'Y-m-d H:i:s'; /** * Format to use for __toString. * * @var string */ - protected static $stringFormat = self::DEFAULT_STRING_FORMAT; + protected static $stringFormat = self::DEFAULT_STRING_FORMAT; /** * @var array */ protected static $formats = array( - 'datetime' => 'Y-m-d H:i:s', - 'date' => 'Y-m-d', - 'time' => 'H:i:s', + 'datetime' => 'Y-m-d H:i:s', + 'date' => 'Y-m-d', + 'time' => 'H:i:s', ); /** @@ -85,32 +85,10 @@ class Verta extends DateTime { const POST = 'بعد'; /** - * Number persian in convertion - */ - const PERSIAN_ZERO = '۰'; - const PERSIAN_ONE = '۱'; - const PERSIAN_TOW = '۲'; - const PERSIAN_THREE = '۳'; - const PERSIAN_FOUR = '۴'; - const PERSIAN_FIVE = '۵'; - const PERSIAN_SIX = '۶'; - const PERSIAN_SEVEN = '۷'; - const PERSIAN_EIGHT = '۸'; - const PERSIAN_NINE = '۹'; - - /** - * Number english in convertion - */ - const ENGLISH_ZERO = '0'; - const ENGLISH_ONE = '1'; - const ENGLISH_TOW = '2'; - const ENGLISH_THREE = '3'; - const ENGLISH_FOUR = '4'; - const ENGLISH_FIVE = '5'; - const ENGLISH_SIX = '6'; - const ENGLISH_SEVEN = '7'; - const ENGLISH_EIGHT = '8'; - const ENGLISH_NINE = '9'; + * Curency use in number to word. + */ + const RIAL = 'ریال'; + const TOMAN = 'تومان'; /** * number day in months gregorian @@ -118,20 +96,19 @@ class Verta extends DateTime { * @var array */ protected static $daysMonthGregorian = array( - 31, // January - 28, // February - 31, // March - 30, // April - 31, // May - 30, // June - 31, // July - 31, // August - 30, // September - 31, // October - 30, // November - 31, // December - ); - + 31, + 28, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31, + ); /** * number day in month jalali @@ -139,18 +116,18 @@ class Verta extends DateTime { * @var array */ protected static $daysMonthJalali = array( - 31, // فرودین - 31, // اردیبهشت - 31, // خرداد - 31, // تیر - 31, // مرداد - 31, // شهریور - 30, // مهر - 30, // آبان - 30, // آذر - 30, // دی - 30, // بهمن - 29, // اسفند + 31, + 31, + 31, + 31, + 31, + 31, + 30, + 30, + 30, + 30, + 30, + 29, ); /** @@ -159,19 +136,19 @@ class Verta extends DateTime { * @var array */ protected static $monthYear = array( - 'فرودین', // 1 - 'اردیبهشت', // 2 - 'خرداد', // 3 - 'تیر', // 4 - 'مرداد', // 5 - 'شهریور', // 6 - 'مهر', // 7 - 'آبان', // 8 - 'آذر', // 9 - 'دی', // 10 - 'بهمن', // 11 - 'اسفند', // 12 - ); + 'فرودین', + 'اردیبهشت', + 'خرداد', + 'تیر', + 'مرداد', + 'شهریور', + 'مهر', + 'آبان', + 'آذر', + 'دی', + 'بهمن', + 'اسفند', + ); /** * Names of days of the week. @@ -225,16 +202,16 @@ class Verta extends DateTime { * @var array */ protected static $persianNumber = array( - self::PERSIAN_ZERO, - self::PERSIAN_ONE, - self::PERSIAN_TOW, - self::PERSIAN_THREE, - self::PERSIAN_FOUR, - self::PERSIAN_FIVE, - self::PERSIAN_SIX, - self::PERSIAN_SEVEN, - self::PERSIAN_EIGHT, - self::PERSIAN_NINE, + '۰', + '۱', + '۲', + '۳', + '۴', + '۵', + '۶', + '۷', + '۸', + '۹', ); /** @@ -243,18 +220,205 @@ class Verta extends DateTime { * @var array */ protected static $englishNumber = array( - self::ENGLISH_ZERO, - self::ENGLISH_ONE, - self::ENGLISH_TOW, - self::ENGLISH_THREE, - self::ENGLISH_FOUR, - self::ENGLISH_FIVE, - self::ENGLISH_SIX, - self::ENGLISH_SEVEN, - self::ENGLISH_EIGHT, - self::ENGLISH_NINE, + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', ); + protected static $number_word = array( + 'fa' => array( + 'd1' => array( + 0 => 'صفر', + 1 => 'یک', + 2 => 'دو', + 3 => 'سه', + 4 => 'چهار', + 5 => 'پنج', + 6 => 'شش', + 7 => 'هفت', + 8 => 'هشت', + 9 => 'نه', + ), + 'd2-1' => array( + 1 => 'یازده', + 2 => 'دوازده', + 3 => 'سیزده', + 4 => 'چهارده', + 5 => 'پانزده', + 6 => 'شانزده', + 7 => 'هفده', + 8 => 'هجده', + 9 => 'نوزده', + ), + 'd2-2' => array( + 1 => 'ده', + 2 => 'بیست', + 3 => 'سی', + 4 => 'چهل', + 5 => 'پنجاه', + 6 => 'شصت', + 7 => 'هفتاد', + 8 => 'هشتاد', + 9 => 'نود' + ), + 'd3' => array( + 1 => 'صد', + 2 => 'دویست', + 3 => 'سیصد', + 4 => 'چهارصد', + 5 => 'پانصد', + 6 => 'ششصد', + 7 => 'هفتصد', + 8 => 'هشتصد', + 9 => 'نهصد', + ), + 'step' => array( + 1 => 'هزار', + 2 => 'میلیون', + 3 => 'میلیارد', + 4 => 'بیلیون', + 5 => 'تریلیون', + 6 => 'کادریلیون', + 7 => 'کوینتریلیون', + 8 => 'سکستریلیون', + 9 => 'سپتریلیون', + 10 => 'اکتریلیون', + 11 => 'نونیلیون', + 12 => 'دسیلیون', + ), + 'and' => 'و', + ), + 'en' => array( + 'd1' => array( + 0 => 'zero', + 1 => 'one', + 2 => 'two', + 3 => 'three', + 4 => 'four', + 5 => 'five', + 6 => 'six', + 7 => 'seven', + 8 => 'eight', + 9 => 'nine', + ), + 'd2-1' => array( + 1 => 'eleven', + 2 => 'twelve', + 3 => 'thirteen', + 4 => 'fourteen', + 5 => 'fifteen', + 6 => 'sixteen', + 7 => 'seventeen', + 8 => 'eighteen', + 9 => 'nineteen', + ), + 'd2-2' => array( + 1 => 'ten', + 2 => 'twenty', + 3 => 'thirty', + 4 => 'forty', + 5 => 'fifty', + 6 => 'sixty', + 7 => 'seventy', + 8 => 'eighty', + 9 => 'ninety' + ), + 'd3' => array( + 1 => 'one hundred', + 2 => 'two hundred', + 3 => 'three hundred', + 4 => 'four hundred', + 5 => 'five hundred', + 6 => 'six hundred', + 7 => 'seven hundred', + 8 => 'eight hundred', + 9 => 'nine hundred', + ), + 'step' => array( + 1 => 'thousand', + 2 => 'million', + 3 => 'milliard', + 4 => 'billion', + 5 => 'trillion', + 6 => 'quadrillion', + 7 => 'quintillion', + 8 => 'sextillion', + 9 => 'septillion', + 10 => 'octillion', + 11 => 'nonillion', + 12 => 'decillion', + ), + 'and' => 'and', + ), + ); + protected static $digit1 = array( + 0 => 'صفر', + 1 => 'یک', + 2 => 'دو', + 3 => 'سه', + 4 => 'چهار', + 5 => 'پنج', + 6 => 'شش', + 7 => 'هفت', + 8 => 'هشت', + 9 => 'نه', + ); + protected static $digit1_5 = array( + 1 => 'یازده', + 2 => 'دوازده', + 3 => 'سیزده', + 4 => 'چهارده', + 5 => 'پانزده', + 6 => 'شانزده', + 7 => 'هفده', + 8 => 'هجده', + 9 => 'نوزده', + ); + protected static $digit2 = array( + 1 => 'ده', + 2 => 'بیست', + 3 => 'سی', + 4 => 'چهل', + 5 => 'پنجاه', + 6 => 'شصت', + 7 => 'هفتاد', + 8 => 'هشتاد', + 9 => 'نود' + ); + protected static $digit3 = array( + 1 => 'صد', + 2 => 'دویست', + 3 => 'سیصد', + 4 => 'چهارصد', + 5 => 'پانصد', + 6 => 'ششصد', + 7 => 'هفتصد', + 8 => 'هشتصد', + 9 => 'نهصد', + ); + protected static $steps = array( + 1 => 'هزار', + 2 => 'میلیون', + 3 => 'میلیارد', + 4 => 'بیلیون', + 5 => 'تریلیون', + 6 => 'کادریلیون', + 7 => 'کوینتریلیون', + 8 => 'سکستریلیون', + 9 => 'سپتریلیون', + 10 => 'اکتریلیون', + 11 => 'نونیلیون', + 12 => 'دسیلیون', + ); + + /***************************** CONSTRUCT ****************************/ /** @@ -375,7 +539,7 @@ class Verta extends DateTime { if (!static::isValideDate($year, $month, $day) || !static::isValideTime($hour, $minute, $second)) { throw new \InvalidArgumentException('Unknown datetime'); - } + } return static::parse(sprintf('%s-%s-%s %s:%s:%s', $year, $month, $day, $hour, $minute, $second)); } @@ -1355,20 +1519,20 @@ class Verta extends DateTime { return array($gy, $i + 1, $g_day_no + 1); } + /** + * integer division + * + * @param int $a + * @param int $b + * @return int + */ + private static function div($a, $b) { + return ~~($a / $b); + } + /***************************** HELPER ****************************/ - /** - * integer division - * - * @param int $a - * @param int $b - * @return int - */ - private static function div($a, $b) { - return ~~($a / $b); - } - /** * return day number from first day of year * @@ -1399,4 +1563,128 @@ class Verta extends DateTime { return static::$daysMonthJalali[$month-1]; } + /** + * format number for change to word + * + * @param string $number + * @param int $decimal_precision + * @param string $decimals_separator + * @param string $thousands_separator + * @return string + */ + static function number_format($number, $decimal_precision = 0, $decimals_separator = '.', $thousands_separator = ',') { + $number = explode('.', str_replace(' ', '', $number)); + $number[0] = str_split(strrev($number[0]), 3); + $total_segments = count($number[0]); + for ($i = 0; $i < $total_segments; $i++) { + $number[0][$i] = strrev($number[0][$i]); + } + $number[0] = implode($thousands_separator, array_reverse($number[0])); + if (!empty($number[1])) { + $number[1] = round($number[1], $decimal_precision); + } + return implode($decimals_separator, $number); + } + + /** + * group number to word + * + * @param array $group + * @param string $lang + * @return array + */ + protected static function groupToWords($group, $lang) { + $d3 = floor($group / 100); + $d2 = floor(($group - $d3 * 100) / 10); + $d1 = $group - $d3 * 100 - $d2 * 10; + + $group_array = array(); + + if ($d3 != 0) { + $group_array[] = static::$number_word[$lang]['d3'][$d3]; + } + + if ($d2 == 1 && $d1 != 0) { // 11-19 + $group_array[] = static::$number_word[$lang]['d2-1'][$d1]; + } else if ($d2 != 0 && $d1 == 0) { // 10-20-...-90 + $group_array[] = static::$number_word[$lang]['d2-2'][$d2]; + } else if ($d2 == 0 && $d1 == 0) { // 00 + } else if ($d2 == 0 && $d1 != 0) { // 1-9 + $group_array[] = static::$number_word[$lang]['d1'][$d1]; + } else { // Others + $group_array[] = static::$number_word[$lang]['d2-2'][$d2]; + $group_array[] = static::$number_word[$lang]['d1'][$d1]; + } + if (!count($group_array)) { + return FALSE; + } + return $group_array; + } + + /** + * return string of number + * + * @param string $number + * @param string $lang + * @return string + */ + public static function getWords($number, $lang = 'fa') { + $formated = self::number_format(strval($number), 0, '.', ','); + $groups = explode(',', $formated); + + $steps = count($groups); + + $parts = array(); + foreach ($groups as $step => $group) { + $group_words = self::groupToWords($group, $lang); + if ($group_words) { + $part = implode(' ' . static::$number_word[$lang]['and'] . ' ', $group_words); + if (isset(static::$number_word[$lang]['step'][$steps - $step - 1])) { + $part .= ' ' . static::$number_word[$lang]['step'][$steps - $step - 1]; + } + $parts[] = $part; + } + } + return implode(' ' . static::$number_word[$lang]['and'] . ' ', $parts); + } + + /** + * return persian string of number + * + * @param string $number + * @return int + */ + public static function getPersianWords ($number) { + return self::getWords($number); + } + + /** + * return english string of number + * + * @param string $number + * @return string + */ + public static function getEnglishWords ($number) { + return self::getWords($number, 'en'); + } + + /** + * return rial string of number + * + * @param string $number + * @return string + */ + public static function getRial($number) { + return self::getPersianWords($number) . ' ' . self::RIAL; + } + + /** + * return toman string of number + * + * @param string $number + * @return string + */ + public static function getToman($number) { + return self::getPersianWords($number) . ' ' . self::TOMAN; + } } \ No newline at end of file From 8ba7ebe7cad8333e51b433f389a89030dd833fb2 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Sun, 21 May 2017 21:50:41 +0430 Subject: [PATCH 18/22] add create gregorian and jalali --- README.md | 18 ++-- src/Verta.php | 224 +++++++++++++++++++++++++++++++------------------- 2 files changed, 154 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index 6c7f1cc..5f6c8a8 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,18 @@ $dt = Verta::parse('1395-10-07 14:12:32'); // create from a specific date and time return Verta::create(); // 1395-12-14 11:17:01 equal now() -return Verta::create(1394,12,29,15,51,5); // 1394-12-29 15:51:05 -return Verta::createDate(1394,12,29); // 1394-12-29 11:18:29 set time now -return Verta::createTime(15,51,5); // 1395-12-14 15:51:05 set date now +return Verta::create(2016,12,25,15,20,15); // 1395-10-05 15:20:15 +return Verta::createDate(2016,12,25); // 1395-10-05 21:35:49 set time now +return Verta::createTime(15,51,5); // 1396-02-31 15:51:05 set date now return Verta::createTimestamp(1488614023); // 1395-12-14 11:23:43 + +return Verta::createGregorian(2016,12,25,15,20,15); // 1395-10-05 15:20:15 +return Verta::createGregorianDate(2016,12,25); // 1395-10-05 21:35:49 set time now +return Verta::createGregorianTime(15,51,5); // 1396-02-31 15:51:05 set date now + +return Verta::createJalali(1394,12,29,15,51,5); // 1394-12-29 15:51:05 +return Verta::createJalaliDate(1394,12,29); // 1394-12-29 11:18:29 set time now +return Verta::createJalaliTime(15,51,5); // 1395-12-14 15:51:05 set date now ``` --- #### Formating @@ -89,8 +97,8 @@ $dt = new Verta(); return $dt->formatDatetime(); // 1395-12-10 23:37:26 return $dt->formatDate(); // 1395-12-10 return $dt->formatTime(); // 23:26:35 -return $dt->formatPersianDatetime(); // 1395/12/10 23:46:09 -return $dt->formatPersianDate(); // 1395/12/10 +return $dt->formatJalaliDatetime(); // 1395/12/10 23:46:09 +return $dt->formatJalaliDate(); // 1395/12/10 // set default format Verta::setStringformat('Y/n/j H:i:s'); diff --git a/src/Verta.php b/src/Verta.php index 27447e5..4f96838 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -358,66 +358,6 @@ class Verta extends DateTime { 'and' => 'and', ), ); - protected static $digit1 = array( - 0 => 'صفر', - 1 => 'یک', - 2 => 'دو', - 3 => 'سه', - 4 => 'چهار', - 5 => 'پنج', - 6 => 'شش', - 7 => 'هفت', - 8 => 'هشت', - 9 => 'نه', - ); - protected static $digit1_5 = array( - 1 => 'یازده', - 2 => 'دوازده', - 3 => 'سیزده', - 4 => 'چهارده', - 5 => 'پانزده', - 6 => 'شانزده', - 7 => 'هفده', - 8 => 'هجده', - 9 => 'نوزده', - ); - protected static $digit2 = array( - 1 => 'ده', - 2 => 'بیست', - 3 => 'سی', - 4 => 'چهل', - 5 => 'پنجاه', - 6 => 'شصت', - 7 => 'هفتاد', - 8 => 'هشتاد', - 9 => 'نود' - ); - protected static $digit3 = array( - 1 => 'صد', - 2 => 'دویست', - 3 => 'سیصد', - 4 => 'چهارصد', - 5 => 'پانصد', - 6 => 'ششصد', - 7 => 'هفتصد', - 8 => 'هشتصد', - 9 => 'نهصد', - ); - protected static $steps = array( - 1 => 'هزار', - 2 => 'میلیون', - 3 => 'میلیارد', - 4 => 'بیلیون', - 5 => 'تریلیون', - 6 => 'کادریلیون', - 7 => 'کوینتریلیون', - 8 => 'سکستریلیون', - 9 => 'سپتریلیون', - 10 => 'اکتریلیون', - 11 => 'نونیلیون', - 12 => 'دسیلیون', - ); - /***************************** CONSTRUCT ****************************/ @@ -509,7 +449,7 @@ class Verta extends DateTime { } /** - * Create a new Verta instance from a specific date and time. + * Create a new Verta instance from a specific date and time gregorain. * * If any of feild are set to null their now() values will * be used. @@ -524,28 +464,13 @@ class Verta extends DateTime { * * @return static */ - public static function create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) { - $now = static::instance(null, $timezone)->format('Y-n-j-G-i-s'); - $defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now)); - - $year = $year === null ? intval($defaults['year']) : $year; - $month = $month === null ? intval($defaults['month']) : $month; - $day = $day === null ? intval($defaults['day']) : $day; - $hour = $hour === null ? intval($defaults['hour']) : $hour; - $minute = $minute === null ? intval($defaults['minute']) : $minute; - $second = $second === null ? intval($defaults['second']) : $second; - - if (!static::isValideDate($year, $month, $day) || !static::isValideTime($hour, $minute, $second)) { - throw new \InvalidArgumentException('Unknown datetime'); - } - - return static::parse(sprintf('%s-%s-%s %s:%s:%s', $year, $month, $day, $hour, $minute, $second)); + return static::createGregorian($year, $month, $day, $hour, $minute, $second, $timezone); } /** - * Create a Verta from just a date. + * Create a Verta from just a date gregorian. * * @param int|null $year * @param int|null $month @@ -560,7 +485,7 @@ class Verta extends DateTime { } /** - * Create a Verta instance from just a time. + * Create a Verta instance from just a time gregorian. * * @param int|null $hour * @param int|null $minute @@ -573,7 +498,7 @@ class Verta extends DateTime { { return static::create(null, null, null, $hour, $minute, $second, $timezone); } - + /** * Create a Verta instance from a timestamp. * @@ -606,6 +531,139 @@ class Verta extends DateTime { return $object; } + /** + * Create a new Verta instance from a specific date and time gregorain. + * + * If any of feild are set to null their now() values will + * be used. + * + * @param int|null $year + * @param int|null $month + * @param int|null $day + * @param int|null $hour + * @param int|null $minute + * @param int|null $second + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createGregorian($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) + { + + $now = date('Y-n-j-G-i-s', time()); + $defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now)); + + $year = $year === null ? intval($defaults['year']) : $year; + $month = $month === null ? intval($defaults['month']) : $month; + $day = $day === null ? intval($defaults['day']) : $day; + $hour = $hour === null ? intval($defaults['hour']) : $hour; + $minute = $minute === null ? intval($defaults['minute']) : $minute; + $second = $second === null ? intval($defaults['second']) : $second; + + if (!static::isValideDate($year, $month, $day) || !static::isValideTime($hour, $minute, $second)) { + throw new \InvalidArgumentException('Unknown datetime'); + } + + return static::instance(sprintf('%s-%s-%s %s:%s:%s', $year, $month, $day, $hour, $minute, $second), $timezone); + } + + /** + * Create a Verta from just a date gregorian. + * + * @param int|null $year + * @param int|null $month + * @param int|null $day + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createGregorianDate($year = null, $month = null, $day = null, $timezone = null) + { + return static::createGregorian($year, $month, $day, null, null, null, $timezone); + } + + /** + * Create a Verta instance from just a time gregorian. + * + * @param int|null $hour + * @param int|null $minute + * @param int|null $second + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createGregorianTime($hour = null, $minute = null, $second = null, $timezone = null) + { + return static::createGregorian(null, null, null, $hour, $minute, $second, $timezone); + } + + /** + * Create a new Verta instance from a specific date and time. + * + * If any of feild are set to null their now() values will + * be used. + * + * @param int|null $year + * @param int|null $month + * @param int|null $day + * @param int|null $hour + * @param int|null $minute + * @param int|null $second + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + + public static function createJalali($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) + { + $now = static::instance(null, $timezone)->format('Y-n-j-G-i-s'); + $defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now)); + + $year = $year === null ? intval($defaults['year']) : $year; + $month = $month === null ? intval($defaults['month']) : $month; + $day = $day === null ? intval($defaults['day']) : $day; + $hour = $hour === null ? intval($defaults['hour']) : $hour; + $minute = $minute === null ? intval($defaults['minute']) : $minute; + $second = $second === null ? intval($defaults['second']) : $second; + + if (!static::isValideDate($year, $month, $day) || !static::isValideTime($hour, $minute, $second)) { + throw new \InvalidArgumentException('Unknown datetime'); + } + + return static::parse(sprintf('%s-%s-%s %s:%s:%s', $year, $month, $day, $hour, $minute, $second)); + } + + /** + * Create a Verta from just a date. + * + * @param int|null $year + * @param int|null $month + * @param int|null $day + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createJalaliDate($year = null, $month = null, $day = null, $timezone = null) + { + return static::createJalali($year, $month, $day, null, null, null, $timezone); + } + + /** + * Create a Verta instance from just a time. + * + * @param int|null $hour + * @param int|null $minute + * @param int|null $second + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function createJalaliTime($hour = null, $minute = null, $second = null, $timezone = null) + { + return static::createJalali(null, null, null, $hour, $minute, $second, $timezone); + } + + /***************************** STRING FORMATED ****************************/ @@ -946,7 +1004,7 @@ class Verta extends DateTime { * * @return string */ - public function formatPersianDatetime() + public function formatJalaliDatetime() { return $this->format('Y/n/j H:i:s'); } @@ -956,7 +1014,7 @@ class Verta extends DateTime { * * @return string */ - public function formatPersianDate() + public function formatJalaliDate() { return $this->format('Y/n/j'); } @@ -1142,7 +1200,7 @@ class Verta extends DateTime { } } } - return self::create($year, $month, $day, $hour, $minute, $second, $this->getTimeZone()); + return self::createJalali($year, $month, $day, $hour, $minute, $second, $this->getTimeZone()); } /** From d9b52963469057b248629ff4f2755190a6b21531 Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Tue, 23 May 2017 23:40:56 +0430 Subject: [PATCH 19/22] add today, tomorrow, yesterday --- README.md | 9 ++++++- src/Verta.php | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5f6c8a8..6d5429b 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,10 @@ use \Verta as Verta ;` // default timestamp is now $dt = new Verta(); // OR -$dt = Verta::now(); +$dt = Verta::now(); +return Verta::today(); //1396-03-02 00:00:00 +return Verta::tomorrow(); // 1396-03-03 00:00:00 +return Verta::yesterday(); // 1396-03-01 00:00:00 // pass string datetime $dt = new Verta('2016-12-27 14:12:32'); @@ -100,6 +103,10 @@ return $dt->formatTime(); // 23:26:35 return $dt->formatJalaliDatetime(); // 1395/12/10 23:46:09 return $dt->formatJalaliDate(); // 1395/12/10 +// get gregorain datetime +return $dt->DateTime()->format('Y-m-d H:i:s'); // 2017-05-23 23:21:02 +return $dt->formatGregorian('Y-m-d H:i:s'); // 2017-05-23 23:21:02 + // set default format Verta::setStringformat('Y/n/j H:i:s'); return new Verta(); // 1395/12/12 00:11:35 diff --git a/src/Verta.php b/src/Verta.php index 4f96838..ac0acd3 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -401,6 +401,42 @@ class Verta extends DateTime { public static function now() { return new static(); } + /** + * Create a Carbon instance for today. + * + * @param \DateTimeZone|string|null $tz + * + * @return static + */ + + public static function today($tz = null) + { + return static::now($tz)->startOfDay(); + } + + /** + * Create a Carbon instance for tomorrow. + * + * @param \DateTimeZone|string|null $tz + * + * @return static + */ + public static function tomorrow($tz = null) + { + return static::today($tz)->addDay(); + } + + /** + * Create a Carbon instance for yesterday. + * + * @param \DateTimeZone|string|null $tz + * + * @return static + */ + public static function yesterday($tz = null) + { + return static::today($tz)->subDay(); + } /** * Create a Verta instance from a DateTime one @@ -969,6 +1005,17 @@ class Verta extends DateTime { return $this->strftime($format); } + /** + * The format of the outputted date string (gregorian) + * + * @param $format + * @return string + */ + public function formatGregorian($format) { + + return $this->DateTime()->format($format); + } + /** * Format the instance as date * @@ -1485,7 +1532,29 @@ class Verta extends DateTime { return $this->addSeconds(-1 * $value); } - /***************************** CONVERTION ****************************/ + /***************************** MODIFY ****************************/ + + /** + * Resets the time to 00:00:00 + * + * @return static + */ + public function startOfDay() + { + return $this->setTime(0, 0, 0); + } + + /** + * Resets the time to 23:59:59 + * + * @return static + */ + public function endOfDay() + { + return $this->setTime(23, 59, 59); + } + + /***************************** CONVERT ****************************/ /** * gregorian to jalali convertion From 36d7e6f56eb10e3639b69a0b9e0e429c1fa61d3b Mon Sep 17 00:00:00 2001 From: hekmatinasser Date: Wed, 24 May 2017 08:47:05 +0430 Subject: [PATCH 20/22] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6d5429b..d2afa87 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,10 @@ use \Verta as Verta ;` // default timestamp is now $dt = new Verta(); // OR -$dt = Verta::now(); -return Verta::today(); //1396-03-02 00:00:00 -return Verta::tomorrow(); // 1396-03-03 00:00:00 -return Verta::yesterday(); // 1396-03-01 00:00:00 +$dt = Verta::now(); //1396-02-02 15:32:08 +$dt = Verta::today(); //1396-03-02 00:00:00 +$dt = Verta::tomorrow(); // 1396-03-03 00:00:00 +$dt = Verta::yesterday(); // 1396-03-01 00:00:00 // pass string datetime $dt = new Verta('2016-12-27 14:12:32'); From 9f08f7a4a27eebe75b9dc770f59dd1c12d1f0373 Mon Sep 17 00:00:00 2001 From: hekmatinasser Date: Wed, 24 May 2017 23:20:57 +0430 Subject: [PATCH 21/22] Set theme jekyll-theme-cayman --- _config.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 _config.yml diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..c419263 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-cayman \ No newline at end of file From cd78544b71719a1bcf9649058182ef3f59e14a7e Mon Sep 17 00:00:00 2001 From: Nasser Hekmati Date: Fri, 26 May 2017 19:40:58 +0430 Subject: [PATCH 22/22] add helper and remove n2w --- README.md | 49 ++---- composer.json | 7 +- src/Verta.php | 398 ++++++++---------------------------------------- src/helpers.php | 11 ++ 4 files changed, 94 insertions(+), 371 deletions(-) create mode 100644 src/helpers.php diff --git a/README.md b/README.md index 6d5429b..1ae6cc0 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ return Verta::createJalali(1394,12,29,15,51,5); // 1394-12-29 15:51:05 return Verta::createJalaliDate(1394,12,29); // 1394-12-29 11:18:29 set time now return Verta::createJalaliTime(15,51,5); // 1395-12-14 15:51:05 set date now ``` + --- #### Formating ```php @@ -160,12 +161,23 @@ return $dt->addSecond(); // 1395-10-07 14:12:33 return $dt->addSeconds(3); // 1395-10-07 14:12:35 return $dt->subSecond(); // 1395-10-07 14:12:31 return $dt->subSeconds(2); // 1395-10-07 14:12:30 - ``` --- -#### Differences +#### Comparisons ```php +// is leap year +echo Verta::isLeapYear(1394); // false +echo Verta::isLeapYear(1395); // true + +// is valid date +echo Verta::isValideDate(1394, 12, 30); // false +echo Verta::isValideDate(1395, 12, 30); // true + +// is valid time +echo Verta::isValideTime(15, 62, 50); // false +echo Verta::isValideTime(15, 12, 30); // true + // get a diffrent time with now $dt = Verta::parse('1395-10-07 14:12:32'); return $dt->diffNow(); // 2 ماه پیش @@ -192,39 +204,6 @@ $dt = Verta::parse('1395/01/05 23:50:25'); $c = Carbon::instance($v->DateTime()); ``` ---- -#### Comparisons -```php -// is leap year -echo Verta::isLeapYear(1394); // false -echo Verta::isLeapYear(1395); // true - -// is valid date -echo Verta::isValideDate(1394, 12, 30); // false -echo Verta::isValideDate(1395, 12, 30); // true - -// is valid time -echo Verta::isValideTime(15, 62, 50); // false -echo Verta::isValideTime(15, 12, 30); // true -``` - ---- -#### Exchange n2w -```php -// get word of number -// max number size 39 digit -echo Verta::getWords('455000000000000000000000000000000000000'); // چهارصد و پنجاه و پنج دسیلیون -echo Verta::getWords('4550000'); // چهار میلیون و پانصد و پنجاه هزار -echo Verta::getWords('4550000', 'fa'); // چهار میلیون و پانصد و پنجاه هزار -echo Verta::getPersianWords('4550000'); // چهار میلیون و پانصد و پنجاه هزار -echo Verta::getWords('4550000', 'en'); // four million and five hundred and fifty thousand -echo Verta::getEnglishWords('4550000'); // four million and five hundred and fifty thousand - -// add currency persian -echo Verta::getRial('4550000'); // چهار میلیون و پانصد و پنجاه هزار ریال -echo Verta::getToman('4550000'); // چهار میلیون و پانصد و پنجاه هزار تومان -``` - --- ## License ## - This package was created and modified by [Nasser Hekmati](https://github.com/hekmatinasser) for Laravel >= 5 and is released under the MIT License. diff --git a/composer.json b/composer.json index 2bd5835..01da00d 100755 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "hekmatinasser/verta", - "description": "This Package helps developers to work with Jalali Date and Time class.", + "description": "This Package helps developers to work with Jalali Date and Time class for Laravel freamwork PHP", "license": "MIT", "authors": [ { @@ -16,7 +16,10 @@ "autoload": { "psr-4": { "Hekmatinasser\\Verta\\": "src" - } + }, + "files": [ + "src/helpers.php" + ] }, "minimum-stability": "dev" } diff --git a/src/Verta.php b/src/Verta.php index ac0acd3..931e36d 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -18,11 +18,11 @@ use InvalidArgumentException; use Exception; - /********************************* CONFIG *********************************/ - /** - * set Default timezone - */ - date_default_timezone_set('Asia/Tehran'); +/********************************* CONFIG *********************************/ +/** + * set Default timezone + */ +date_default_timezone_set('Asia/Tehran'); class Verta extends DateTime { @@ -84,12 +84,6 @@ class Verta extends DateTime { const PRE = 'پیش'; const POST = 'بعد'; - /** - * Curency use in number to word. - */ - const RIAL = 'ریال'; - const TOMAN = 'تومان'; - /** * number day in months gregorian * @@ -232,133 +226,6 @@ class Verta extends DateTime { '9', ); - protected static $number_word = array( - 'fa' => array( - 'd1' => array( - 0 => 'صفر', - 1 => 'یک', - 2 => 'دو', - 3 => 'سه', - 4 => 'چهار', - 5 => 'پنج', - 6 => 'شش', - 7 => 'هفت', - 8 => 'هشت', - 9 => 'نه', - ), - 'd2-1' => array( - 1 => 'یازده', - 2 => 'دوازده', - 3 => 'سیزده', - 4 => 'چهارده', - 5 => 'پانزده', - 6 => 'شانزده', - 7 => 'هفده', - 8 => 'هجده', - 9 => 'نوزده', - ), - 'd2-2' => array( - 1 => 'ده', - 2 => 'بیست', - 3 => 'سی', - 4 => 'چهل', - 5 => 'پنجاه', - 6 => 'شصت', - 7 => 'هفتاد', - 8 => 'هشتاد', - 9 => 'نود' - ), - 'd3' => array( - 1 => 'صد', - 2 => 'دویست', - 3 => 'سیصد', - 4 => 'چهارصد', - 5 => 'پانصد', - 6 => 'ششصد', - 7 => 'هفتصد', - 8 => 'هشتصد', - 9 => 'نهصد', - ), - 'step' => array( - 1 => 'هزار', - 2 => 'میلیون', - 3 => 'میلیارد', - 4 => 'بیلیون', - 5 => 'تریلیون', - 6 => 'کادریلیون', - 7 => 'کوینتریلیون', - 8 => 'سکستریلیون', - 9 => 'سپتریلیون', - 10 => 'اکتریلیون', - 11 => 'نونیلیون', - 12 => 'دسیلیون', - ), - 'and' => 'و', - ), - 'en' => array( - 'd1' => array( - 0 => 'zero', - 1 => 'one', - 2 => 'two', - 3 => 'three', - 4 => 'four', - 5 => 'five', - 6 => 'six', - 7 => 'seven', - 8 => 'eight', - 9 => 'nine', - ), - 'd2-1' => array( - 1 => 'eleven', - 2 => 'twelve', - 3 => 'thirteen', - 4 => 'fourteen', - 5 => 'fifteen', - 6 => 'sixteen', - 7 => 'seventeen', - 8 => 'eighteen', - 9 => 'nineteen', - ), - 'd2-2' => array( - 1 => 'ten', - 2 => 'twenty', - 3 => 'thirty', - 4 => 'forty', - 5 => 'fifty', - 6 => 'sixty', - 7 => 'seventy', - 8 => 'eighty', - 9 => 'ninety' - ), - 'd3' => array( - 1 => 'one hundred', - 2 => 'two hundred', - 3 => 'three hundred', - 4 => 'four hundred', - 5 => 'five hundred', - 6 => 'six hundred', - 7 => 'seven hundred', - 8 => 'eight hundred', - 9 => 'nine hundred', - ), - 'step' => array( - 1 => 'thousand', - 2 => 'million', - 3 => 'milliard', - 4 => 'billion', - 5 => 'trillion', - 6 => 'quadrillion', - 7 => 'quintillion', - 8 => 'sextillion', - 9 => 'septillion', - 10 => 'octillion', - 11 => 'nonillion', - 12 => 'decillion', - ), - 'and' => 'and', - ), - ); - /***************************** CONSTRUCT ****************************/ /** @@ -459,8 +326,7 @@ class Verta extends DateTime { return new DateTime(date('Y-m-d H:i:s.u', $this->getTimestamp()), $this->getTimeZone()); } - - /** + /** * Create a Verta instance from a DateTime one * * @param timestamp $datetime [optional] @@ -596,7 +462,7 @@ class Verta extends DateTime { $minute = $minute === null ? intval($defaults['minute']) : $minute; $second = $second === null ? intval($defaults['second']) : $second; - if (!static::isValideDate($year, $month, $day) || !static::isValideTime($hour, $minute, $second)) { + if (!checkdate($year, $month, $day) || !static::isValideTime($hour, $minute, $second)) { throw new \InvalidArgumentException('Unknown datetime'); } @@ -731,8 +597,7 @@ class Verta extends DateTime { return $this->format(static::$stringFormat); } - - /** + /** * The format of the outputted date string (jalali equivalent of php date() function) * * @param string $format for example 'Y-m-d H:i:s' @@ -1066,6 +931,60 @@ class Verta extends DateTime { return $this->format('Y/n/j'); } + /** + * Convert english numbers to persian numbers + * + * @param string $string + * @return string + */ + public static function persianNumbers($string) + { + return str_replace(self::$englishNumber, self::$persianNumber, $string); + } + + /***************************** COMPERTION ****************************/ + + /** + * check jalali the instance is a leap year + * + * @param int $year + * @return bool + */ + public static function isLeapYear($year) { + $mod = ($year % 33); + if (($mod == 1) or ( $mod == 5) or ( $mod == 9) or ( $mod == 13) or ( $mod == 17) or ( $mod == 22) or ( $mod == 26) or ( $mod == 30)) { + return true; + } + return false; + } + + /** + * validate a jalali date (jalali equivalent of php checkdate() function) + * + * @param int $month + * @param int $day + * @param int $year + * @return bool + */ + public static function isValideDate($year, $month, $day) { + return $year >= 1 && $year <= 32766 + && $month >= 1 && $month <= 12 + && $day >= 1 && $day <= static::daysMonthJalali($year, $month); + } + + /** + * validate a time + * + * @param int $hour + * @param int $minute + * @param int $second + * @return bool + */ + public static function isValideTime($hour, $minute, $second) { + return $hour >= 0 && $hour <= 24 + && $minute >= 0 && $minute <= 59 + && $second >= 0 && $second <= 59; + } /** * get difference to now @@ -1098,66 +1017,6 @@ class Verta extends DateTime { return sprintf('%s %s %s', $difference, static::$unitName[$j], $timebound); } - /** - * Convert english numbers to persian numbers - * - * @param string $string - * @return string - */ - public static function persianNumbers($string) - { - return str_replace(self::$englishNumber, self::$persianNumber, $string); - } - - /***************************** COMPERTION ****************************/ - - - - /** - * check jalali the instance is a leap year - * - * @param int $year - * @return bool - */ - public static function isLeapYear($year) { - $mod = ($year % 33); - if (($mod == 1) or ( $mod == 5) or ( $mod == 9) or ( $mod == 13) or ( $mod == 17) or ( $mod == 22) or ( $mod == 26) or ( $mod == 30)) { - return true; - } - return false; - } - - - /** - * validate a jalali date (jalali equivalent of php checkdate() function) - * - * @param int $month - * @param int $day - * @param int $year - * @return bool - */ - public static function isValideDate($year, $month, $day) { - return $year >= 1 && $year <= 32766 - && $month >= 1 && $month <= 12 - && $day >= 1 && $day <= static::daysMonthJalali($year, $month); - } - - - /** - * validate a time - * - * @param int $hour - * @param int $minute - * @param int $second - * @return bool - */ - public static function isValideTime($hour, $minute, $second) { - return $hour >= 0 && $hour <= 24 - && $minute >= 0 && $minute <= 59 - && $second >= 0 && $second <= 59; - } - - /***************************** CALCULATION ****************************/ /** @@ -1335,8 +1194,7 @@ class Verta extends DateTime { return $this->addDays(-1 * $value); } - - /** + /** * Add weeks to the instance. Positive $value travels forward while * negative $value travels into the past. * @@ -1554,7 +1412,7 @@ class Verta extends DateTime { return $this->setTime(23, 59, 59); } - /***************************** CONVERT ****************************/ + /***************************** TRANSFORMATION ****************************/ /** * gregorian to jalali convertion @@ -1657,9 +1515,6 @@ class Verta extends DateTime { return ~~($a / $b); } - /***************************** HELPER ****************************/ - - /** * return day number from first day of year * @@ -1689,129 +1544,4 @@ class Verta extends DateTime { $month = intval($month); return static::$daysMonthJalali[$month-1]; } - - /** - * format number for change to word - * - * @param string $number - * @param int $decimal_precision - * @param string $decimals_separator - * @param string $thousands_separator - * @return string - */ - static function number_format($number, $decimal_precision = 0, $decimals_separator = '.', $thousands_separator = ',') { - $number = explode('.', str_replace(' ', '', $number)); - $number[0] = str_split(strrev($number[0]), 3); - $total_segments = count($number[0]); - for ($i = 0; $i < $total_segments; $i++) { - $number[0][$i] = strrev($number[0][$i]); - } - $number[0] = implode($thousands_separator, array_reverse($number[0])); - if (!empty($number[1])) { - $number[1] = round($number[1], $decimal_precision); - } - return implode($decimals_separator, $number); - } - - /** - * group number to word - * - * @param array $group - * @param string $lang - * @return array - */ - protected static function groupToWords($group, $lang) { - $d3 = floor($group / 100); - $d2 = floor(($group - $d3 * 100) / 10); - $d1 = $group - $d3 * 100 - $d2 * 10; - - $group_array = array(); - - if ($d3 != 0) { - $group_array[] = static::$number_word[$lang]['d3'][$d3]; - } - - if ($d2 == 1 && $d1 != 0) { // 11-19 - $group_array[] = static::$number_word[$lang]['d2-1'][$d1]; - } else if ($d2 != 0 && $d1 == 0) { // 10-20-...-90 - $group_array[] = static::$number_word[$lang]['d2-2'][$d2]; - } else if ($d2 == 0 && $d1 == 0) { // 00 - } else if ($d2 == 0 && $d1 != 0) { // 1-9 - $group_array[] = static::$number_word[$lang]['d1'][$d1]; - } else { // Others - $group_array[] = static::$number_word[$lang]['d2-2'][$d2]; - $group_array[] = static::$number_word[$lang]['d1'][$d1]; - } - if (!count($group_array)) { - return FALSE; - } - return $group_array; - } - - /** - * return string of number - * - * @param string $number - * @param string $lang - * @return string - */ - public static function getWords($number, $lang = 'fa') { - $formated = self::number_format(strval($number), 0, '.', ','); - $groups = explode(',', $formated); - - $steps = count($groups); - - $parts = array(); - foreach ($groups as $step => $group) { - $group_words = self::groupToWords($group, $lang); - if ($group_words) { - $part = implode(' ' . static::$number_word[$lang]['and'] . ' ', $group_words); - if (isset(static::$number_word[$lang]['step'][$steps - $step - 1])) { - $part .= ' ' . static::$number_word[$lang]['step'][$steps - $step - 1]; - } - $parts[] = $part; - } - } - return implode(' ' . static::$number_word[$lang]['and'] . ' ', $parts); - } - - /** - * return persian string of number - * - * @param string $number - * @return int - */ - public static function getPersianWords ($number) { - return self::getWords($number); - } - - /** - * return english string of number - * - * @param string $number - * @return string - */ - public static function getEnglishWords ($number) { - return self::getWords($number, 'en'); - } - - /** - * return rial string of number - * - * @param string $number - * @return string - */ - public static function getRial($number) { - return self::getPersianWords($number) . ' ' . self::RIAL; - } - - /** - * return toman string of number - * - * @param string $number - * @return string - */ - public static function getToman($number) { - return self::getPersianWords($number) . ' ' . self::TOMAN; - } } \ No newline at end of file diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..d06bd20 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,11 @@ +