diff --git a/README.md b/README.md index 81f4675..938914e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ 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 and add `'Verta' => Hekmatinasser\Verta\Verta::class,` to the end of the `alias` array. +In your `config/app.php` add `Hekmatinasser\Verta\VertaServiceProvider::class,` to the `providers` array and add `'Verta' => Hekmatinasser\Verta\Verta::class,` to the `alias` array. ```php 'providers' => [ @@ -33,26 +33,27 @@ In your `config/app.php` add `Hekmatinasser\Verta\VertaServiceProvider::class,` ### Verta You can use verta in project ```php -use \Verta as Verta ;` +use Verta; ``` #### Constructors ```php // default timestamp is now -$dt = new Verta(); +$v = new Verta(); // OR -$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 +$v = verta(); //1396-02-02 15:32:08 +$v = Verta::now(); //1396-02-02 15:32:08 +$v = Verta::today(); //1396-03-02 00:00:00 +$v = Verta::tomorrow(); // 1396-03-03 00:00:00 +$v = Verta::yesterday(); // 1396-03-01 00:00:00 // pass string datetime -$dt = new Verta('2016-12-27 14:12:32'); +$v = new Verta('2016-12-27 14:12:32'); // OR -$dt = Verta::instance('2016-12-25 11:12:36'); +$v = Verta::instance('2016-12-25 11:12:36'); // pass timestamps -$dt = new Verta(1333857600); +$v = new Verta(1333857600); //pass datetime object $dt = new \Datetime(); @@ -60,10 +61,10 @@ 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 +return verta($c); // 1395-12-09 15:05:56 // pass strings persian date -$dt = Verta::parse('1395-10-07 14:12:32'); +$v = 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() @@ -85,28 +86,26 @@ return Verta::createJalaliTime(15,51,5); // 1395-12-14 15:51:05 set date now #### 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 -return $dt->format('%B %d، %Y'); // دی 07، 1395 -return $dt; //1395-10-07 14:12:32 +$v = verta(); +return $v->format('Y-n-j H:i'); // 1395-10-7 14:12 +return $v->format('%B %d، %Y'); // دی 07، 1395 +return $v; //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 +return $v->format('datetime'); // 1395-12-10 23:25:12 +return $v->format('date'); // 1395-12-10 +return $v->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->formatJalaliDatetime(); // 1395/12/10 23:46:09 -return $dt->formatJalaliDate(); // 1395/12/10 +return $v->formatDatetime(); // 1395-12-10 23:37:26 +return $v->formatDate(); // 1395-12-10 +return $v->formatTime(); // 23:26:35 +return $v->formatJalaliDatetime(); // 1395/12/10 23:46:09 +return $v->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 +return $v->DateTime()->format('Y-m-d H:i:s'); // 2017-05-23 23:21:02 +return $v->formatGregorian('Y-m-d H:i:s'); // 2017-05-23 23:21:02 // set default format Verta::setStringformat('Y/n/j H:i:s'); @@ -117,8 +116,8 @@ Verta::resetStringFormat(); return new Verta(); // 1395-12-12 00:18:04 // change english number to persian -$dt = Verta::parse('1396-10-07 14:12:32'); -return Verta::persianNumbers($dt); // ۱۳۹۶-۱۰-۰۷ ۱۴:۱۲:۳۲ +$v = Verta::parse('1396-10-07 14:12:32'); +return Verta::persianNumbers($v); // ۱۳۹۶-۱۰-۰۷ ۱۴:۱۲:۳۲ ``` For help in building your formats, checkout the [PHP strftime() docs](http://php.net/manual/en/function.strftime.php). @@ -126,41 +125,41 @@ For help in building your formats, checkout the [PHP strftime() docs](http://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 +$v = verta(); +return $v->addYear(); // 1396-10-07 14:12:32 +return $v->addYears(4); // 1399-10-07 14:12:32 +return $v->subYear(); // 1394-10-07 14:12:32 +return $v->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 $v->addMonth(); // 1395-11-07 14:12:32 +return $v->addMonths(5); // 1396-03-07 14:12:32 +return $v->subMonth(); // 1395-09-07 14:12:32 +return $v->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 $v->addWeek(); // 1395-10-12 14:12:32 +return $v->addWeeks(3); // 1395-10-26 14:12:32 +return $v->subWeek(); // 1395-09-30 14:12:32 +return $v->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 $v->addDay(); // 1395-10-08 14:12:32 +return $v->addDays(3); // 1395-10-11 14:12:32 +return $v->subDay(); // 1395-10-06 14:12:32 +return $v->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 $v->addHour(); // 1395-10-07 15:12:32 +return $v->addHours(5); // 1395-10-07 19:12:32 +return $v->subHour(); // 1395-10-07 13:12:32 +return $v->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 $v->addMinute(); // 1395-10-07 14:13:32 +return $v->addMinutes(3); // 1395-10-07 14:15:32 +return $v->subMinute(); // 1395-10-07 14:11:32 +return $v->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 +return $v->addSecond(); // 1395-10-07 14:12:33 +return $v->addSeconds(3); // 1395-10-07 14:12:35 +return $v->subSecond(); // 1395-10-07 14:12:31 +return $v->subSeconds(2); // 1395-10-07 14:12:30 ``` --- @@ -179,11 +178,11 @@ 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 ماه پیش +$v = Verta::parse('1395-10-07 14:12:32'); +return $v->diffNow(); // 2 ماه پیش -$dt = Verta::parse('1395/12/12 14:13:50'); -return $dt->diffNow(); // 5 ثانیه بعد +$v = Verta::parse('1395/12/12 14:13:50'); +return $v->diffNow(); // 5 ثانیه بعد ``` --- @@ -196,11 +195,11 @@ return Verta::getJalali(2015,12,25); // [1394,10,4] return Verta::getGregorian(1394,10,4); // [2015,12,25] // export to datetime object -$dt = Verta::parse('1395/01/05 23:50:25'); +$v = Verta::parse('1395/01/05 23:50:25'); $dt = $v->DateTime(); // export to carbon object -$dt = Verta::parse('1395/01/05 23:50:25'); +$v = Verta::parse('1395/01/05 23:50:25'); $c = Carbon::instance($v->DateTime()); ``` diff --git a/src/Verta.php b/src/Verta.php index 931e36d..7cd71ee 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -10,7 +10,7 @@ namespace Hekmatinasser\Verta; * 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; @@ -31,58 +31,58 @@ class Verta extends DateTime { /** * The day constants */ - const SATURDAY = 0; - const SUNDAY = 1; - const MONDAY = 2; - const TUESDAY = 3; - const WEDNESDAY = 4; - const THURSDAY = 5; - const FRIDAY = 6; + 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; + 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_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', ); /** * 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 = 'بعد'; + const AM = 'ق.ظ'; + const PM = 'ب.ظ'; + const ANTE_MERIDIEM = 'قبل از ظهر'; + const POST_MERIDIEM = 'بعد از ظهر'; + const NUMBER_TH = ' ام'; + const PRE = 'پیش'; + const POST = 'بعد'; /** * number day in months gregorian @@ -268,41 +268,42 @@ class Verta extends DateTime { public static function now() { return new static(); } + /** - * Create a Carbon instance for today. + * Create a Verta instance for today. * - * @param \DateTimeZone|string|null $tz + * @param \DateTimeZone|string|null $timezone * * @return static */ - public static function today($tz = null) + public static function today($timezone = null) { - return static::now($tz)->startOfDay(); + return static::now($timezone)->startOfDay(); } /** - * Create a Carbon instance for tomorrow. + * Create a Verta instance for tomorrow. * - * @param \DateTimeZone|string|null $tz + * @param \DateTimeZone|string|null $timezone * * @return static */ - public static function tomorrow($tz = null) + public static function tomorrow($timezone = null) { - return static::today($tz)->addDay(); + return static::today($timezone)->addDay(); } /** - * Create a Carbon instance for yesterday. + * Create a Verta instance for yesterday. * - * @param \DateTimeZone|string|null $tz + * @param \DateTimeZone|string|null $timezone * * @return static */ - public static function yesterday($tz = null) + public static function yesterday($timezone = null) { - return static::today($tz)->subDay(); + return static::today($timezone)->subDay(); } /**