diff --git a/README.md b/README.md index 93bf2e0..e824152 100644 --- a/README.md +++ b/README.md @@ -18,19 +18,16 @@ In your `config/app.php` add `Hekmatinasser\Verta\VertaServiceProvider::class,` ```php 'providers' => [ - ... Hekmatinasser\Verta\VertaServiceProvider::class, ], 'alias' => [ - ... 'Verta' => Hekmatinasser\Verta\Verta::class, ] ``` - ## Basic Usage @@ -41,69 +38,87 @@ use Verta; ``` #### Constructors +create object with datetime now ```php -// default timestamp is now -$v = new Verta(); -// OR +$v = new Verta(); //1396-02-02 15:32:08 $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 +create object with pass string +```php $v = new Verta('2016-12-27 14:12:32'); -// OR $v = Verta::instance('2016-12-25 11:12:36'); +``` -// pass timestamps +create object with timestamp +```php $v = new Verta(1333857600); +``` -//pass datetime object +create object with Datetime +```php $dt = new \Datetime(); return new Verta($dt); // 1395-12-09 15:05:56 +``` -// pass carbon object +create object with Carbon +```php $c = \Carbon::now(); return verta($c); // 1395-12-09 15:05:56 +``` -// pass strings persian date +create object string persian datetime +```php $v = Verta::parse('1395-10-07 14:12:32'); +``` -// create from a specific date and time +create object specific datetime +```php +// get Gregorian datetime return Verta::create(); // 1395-12-14 11:17:01 equal 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 +// alias create functions 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 +// get Jalali datetime 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 ``` --- #### Geter +get part of datetime ```php -//get each part of the time -$v = verta(); +$v = verta(); // 1396-03-14 14:18:23 return $v->year; // 1396 +return $v->quarter // 1 return $v->month; // 3 return $v->day; // 14 return $v->hour; // 14 return $v->minute; // 18 return $v->second; // 23 +return $v->micro; // 0 +return $v->dayOfWeek; // 1 +return $v->dayOfYear; // 107 +return $v->weekOfYear; // 16 +return $v->daysInMonth; // 31 return $v->timestamp; // 1496557661 return $v->timezone; // Asia/Tehran - ``` --- #### Setter +set each part of the time ```php -//set each part of the time $v = verta(); $v->year = 1395; $v->month = 4; // set 13 for next year first month @@ -113,8 +128,9 @@ $v->minute = 50; $v->second = 42; $v->timestamp = 1496557661; $v->timezone = 'Asia/Baku'; - -//set each part of the time with method +``` +set each part of the time with method +```php $v = $v->year(1395); $v = $v->month(4); // set 13 for next year first month $v = $v->day(25); @@ -123,16 +139,18 @@ $v = $v->minute(50); $v = $v->second(42); $v = $v->timestamp(1496557661); $v = $v->timezone('Asia/Baku'); - -//set date and time +``` +set datetime together +```php +// $v = $v->setDateTime(1395, 4, 25, 16, 50, 42); $v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856); $v = $v->setDate(1395, 4, 25); $v = $v->setTimeString('12:25:48'); - ``` --- #### Isset +check set each part of time ```php $v = verta(); echo isset($v->year); // true @@ -143,51 +161,63 @@ echo empty($v->minute); // false echo empty($v->second); // false echo isset($v->timestamp); // true echo isset($v->timezone); // true - ``` --- #### Formating +format with php standard ```php -// format the timestamp $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 +``` +use predefined format +```php 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 +``` +use predefined format method +```php 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 +``` +get Gregorian format +```php 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 +```php // set default format Verta::setStringformat('Y/n/j H:i:s'); -return new Verta(); // 1395/12/12 00:11:35 +return verta(); // 1395/12/12 00:11:35 // reset default format Verta::resetStringFormat(); -return new Verta(); // 1395-12-12 00:18:04 - -// change english number to persian -$v = Verta::parse('1396-10-07 14:12:32'); +return verta(); // 1395-12-12 00:18:04 +``` +difference format +```php +return $v1->diffFormat($v2); // 12 ماه بعد +return $v1->diffFormat($v3); // 1 سال قبل +return $v1->addDays(25)->diffFormat(); // 4 هفته بعد compare with now +return $v1->subDays(6)->diffFormat(); // 6 روز قبل +``` +change number +```php +$v = verta(); return Verta::persianNumbers($v); // ۱۳۹۶-۱۰-۰۷ ۱۴:۱۲:۳۲ ``` For help in building your formats, checkout the [PHP strftime() docs](http://php.net/manual/en/function.strftime.php). --- -#### Calculation +#### Modify +add and sub each part of datetime ```php -// add and sub unit datetime $v = verta(); return $v->addYear(); // 1396-10-07 14:12:32 return $v->addYears(4); // 1399-10-07 14:12:32 @@ -223,10 +253,32 @@ 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 - +``` +change start or end each part of date +```php +$v = verta(); // 1396-04-29 14:25:48 +return $v->startDay(); // 1396-04-29 00:00:00 +return $v->endDay(); // 1396-04-29 23:59:59 +return $v->startWeek(); // 1396 1396-04-24 00:00:00 +return $v->endWeek(); // 1396-04-30 23:59:59 +return $v->startMonth(); // 1396-04-01 00:00:00 +return $v->endMonth(); // 1396-04-31 00:00:00 +return $v->endQuarter(); // 1396-09-30 23:59:59 +``` +--- +#### Comparisons +validation +```php +echo Verta::isLeapYear(1394); // false +echo Verta::isValideDate(1394, 12, 30); // false +echo Verta::isValideTime(15, 12, 30); // true +``` +difference objects together +```php +// diff objects together $v1 = verta(); // 1396-03-31 22:21:40 -$v2 = verta('2018-06-21 01:21:40'); // 1397-03-31 01:21:40 -$v3 = verta('2016-06-20'); // 1395-03-30 15:24:53 +$v2 = verta('2017-06-21 01:21:40'); // 1396-03-31 01:21:40 +$v3 = verta('2017-06-20'); // 1396-03-30 15:24:53 return $v1->diffYears($v3); // -1 return $v1->diffMonths($v2); // 11 @@ -235,34 +287,9 @@ return $v1->diffWeeks($v2); // 51 return $v1->diffDays($v3); // -372 return $v3->diffMinutes(); // 536548 return $v3->diffMinutes(); // 536548 - -return $v1->diffFormat($v2); // 12 ماه بعد -return $v1->diffFormat($v3); // 1 سال قبل -return $v1->addDays(25)->diffFormat(); // 4 هفته بعد compare with now -return $v1->subDays(6)->diffFormat(); // 6 روز قبل - ``` - ---- -#### Comparisons +compare objects together ```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 - -// Comparisons objects together -$v1 = verta(); // 1396-03-31 22:21:40 -$v2 = verta('2017-06-21 01:21:40'); // 1396-03-31 01:21:40 -$v3 = verta('2017-06-20'); // 1396-03-30 15:24:53 - echo $v1->eq($v2); // false equalTo() echo $v1->ne($v2); // true notEqualTo() echo $v1->gt($v2); // true greaterThan() @@ -275,7 +302,9 @@ echo $v1->closest($v2, $v3); // return $v2 object echo $v1->farthest($v2, $v3); // return $v3 object echo $v1->min($v2); // return $v2 object minimum() echo $v1->max($v2); // return $v1 object maximum() - +``` +compare object with is +```php echo $v1->isWeekday(); // true echo $v1->isWeekend(); // false echo $v1->isYesterday(); // false @@ -306,24 +335,24 @@ echo $v1->isTuesday(); // false echo $v1->isWednesday(); // false echo $v1->isThursday(); // true echo $v1->isFriday(); // false - ``` - --- #### Transformations +Gregorian to Jalali date ```php -// get jalali a gregorian date return Verta::getJalali(2015,12,25); // [1394,10,4] - -// get gregorian a jalali date +``` +Jalali to Gregorian date +```php return Verta::getGregorian(1394,10,4); // [2015,12,25] - -// export to datetime object -$v = Verta::parse('1395/01/05 23:50:25'); +``` +Verta to Datatime PHP +```php +$v = verta(); $dt = $v->DateTime(); - -// export to carbon object -$v = Verta::parse('1395/01/05 23:50:25'); +``` +Verta to Carbon +```php $c = Carbon::instance($v->DateTime()); ``` diff --git a/src/Verta.php b/src/Verta.php index 694a41e..f4a2fc2 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -38,6 +38,7 @@ class Verta extends DateTime { const YEARS_PER_CENTURY = 100; const YEARS_PER_DECADE = 10; const MONTHS_PER_YEAR = 12; + const MONTHS_PER_QUARTER = 3; const WEEKS_PER_YEAR = 52; const WEEKS_PER_MONTH = 4.35; const DAYS_PER_WEEK = 7; @@ -614,6 +615,9 @@ class Verta extends DateTime { )): return (int) $this->format($formats[$name]); + case $name === 'quarter': + return (int) ceil($this->month / static::MONTHS_PER_QUARTER); + case $name === 'timezone': return $this->getTimezone()->getName(); @@ -1218,6 +1222,31 @@ class Verta extends DateTime { return $this->format('Y/n/j'); } + /** + * get difference in all + * + * @param Verta|null $v + * + * @return string + */ + public function diffFormat(Verta $v = null) + { + $difference = $this->diffSeconds($v); + $absolute = $difference < 0 ? self::POST : self::PRE; + $difference = abs($difference); + + for ($j = 0; $difference >= static::$unitNumber[$j] and $j < count(static::$unitNumber) - 1; $j++) { + $difference /= static::$unitNumber[$j]; + } + $difference = intval(round($difference)); + + if($difference === 0) { + return self::NOW; + } + + return sprintf('%s %s %s', $difference, static::$unitName[$j], $absolute); + } + /** * Convert english numbers to persian numbers * @@ -1274,6 +1303,98 @@ class Verta extends DateTime { && $second >= 0 && $second <= 59; } + /** + * Get the difference in years + * + * @param Verta|null $v + * + * @return int + */ + public function diffYears(Verta $v = null) + { + $v = $v ?: static::now($this->getTimezone()); + + return (int) $this->diff($v)->format('%r%y'); + } + + /** + * Get the difference in months + * + * @param Verta|null $v + * + * @return int + */ + public function diffMonths(Verta $v = null) + { + $v = $v ?: static::now($this->getTimezone()); + + return $this->diffYears($v) * static::MONTHS_PER_YEAR + (int) $this->diff($v)->format('%r%m'); + } + + /** + * Get the difference in weeks + * + * @param Verta|null $v + * + * @return int + */ + public function diffWeeks(Verta $v = null) + { + return (int) ($this->diffDays($v) / static::DAYS_PER_WEEK); + } + + /** + * Get the difference in days + * + * @param Verta|null $v + * + * @return int + */ + public function diffDays(Verta $v = null) + { + $v = $v ?: static::now($this->getTimezone()); + + return (int) $this->diff($v)->format('%r%a'); + } + + /** + * Get the difference in hours + * + * @param Verta|null $v + * + * @return int + */ + public function diffHours(Verta $v = null) + { + return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); + } + + /** + * Get the difference in minutes + * + * @param Verta|null $v + * + * @return int + */ + public function diffMinutes(Verta $v = null) + { + return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE); + } + + /** + * Get the difference in seconds + * + * @param Verta|null $v + * + * @return int + */ + public function diffSeconds(Verta $v = null) + { + $v = $v ?: static::now($this->getTimezone()); + + return $v->getTimestamp() - $this->getTimestamp(); + } + /** * Determines if the instance is equal to another * @@ -1822,7 +1943,7 @@ class Verta extends DateTime { return $this->dayOfWeek === static::FRIDAY; } - /***************************** CALCULATION ****************************/ + /***************************** MODIFY ****************************/ /** * Add years to the instance. Positive $value travel forward while @@ -2195,132 +2316,12 @@ class Verta extends DateTime { return $this->addSeconds(-1 * $value); } - /** - * Get the difference in years - * - * @param Verta|null $v - * - * @return int - */ - public function diffYears(Verta $v = null) - { - $v = $v ?: static::now($this->getTimezone()); - - return (int) $this->diff($v)->format('%r%y'); - } - - /** - * Get the difference in months - * - * @param Verta|null $v - * - * @return int - */ - public function diffMonths(Verta $v = null) - { - $v = $v ?: static::now($this->getTimezone()); - - return $this->diffYears($v) * static::MONTHS_PER_YEAR + (int) $this->diff($v)->format('%r%m'); - } - - /** - * Get the difference in weeks - * - * @param Verta|null $v - * - * @return int - */ - public function diffWeeks(Verta $v = null) - { - return (int) ($this->diffDays($v) / static::DAYS_PER_WEEK); - } - - - /** - * Get the difference in days - * - * @param Verta|null $v - * - * @return int - */ - public function diffDays(Verta $v = null) - { - $v = $v ?: static::now($this->getTimezone()); - - return (int) $this->diff($v)->format('%r%a'); - } - - /** - * Get the difference in hours - * - * @param Verta|null $v - * - * @return int - */ - public function diffHours(Verta $v = null) - { - return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); - } - - /** - * Get the difference in minutes - * - * @param Verta|null $v - * - * @return int - */ - public function diffMinutes(Verta $v = null) - { - return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE); - } - - /** - * Get the difference in seconds - * - * @param Verta|null $v - * - * @return int - */ - public function diffSeconds(Verta $v = null) - { - $v = $v ?: static::now($this->getTimezone()); - - return $v->getTimestamp() - $this->getTimestamp(); - } - - /** - * get difference in all - * - * @param Verta|null $v - * - * @return string - */ - public function diffFormat(Verta $v = null) - { - $difference = $this->diffSeconds($v); - $absolute = $difference < 0 ? self::POST : self::PRE; - $difference = abs($difference); - - for ($j = 0; $difference >= static::$unitNumber[$j] and $j < count(static::$unitNumber) - 1; $j++) { - $difference /= static::$unitNumber[$j]; - } - $difference = intval(round($difference)); - - if($difference === 0) { - return self::NOW; - } - - return sprintf('%s %s %s', $difference, static::$unitName[$j], $absolute); - } - - /***************************** MODIFY ****************************/ - /** * Resets the time to 00:00:00 * * @return static */ - public function startOfDay() + public function startDay() { return $this->setTime(0, 0, 0); } @@ -2330,11 +2331,103 @@ class Verta extends DateTime { * * @return static */ - public function endOfDay() + public function endDay() { return $this->setTime(23, 59, 59); } + /** + * Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00 + * + * @return static + */ + public function startWeek() + { + while ($this->dayOfWeek !== static::$weekStartsAt) { + $this->subDay(); + } + + return $this->startDay(); + } + + /** + * Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59 + * + * @return static + */ + public function endWeek() + { + while ($this->dayOfWeek !== static::$weekEndsAt) { + $this->addDay(); + } + + return $this->endDay(); + } + + /** + * Resets the date to the first day of the month and the time to 00:00:00 + * + * @return static + */ + public function startMonth() + { + return $this->setDateTime($this->year, $this->month, 1, 0, 0, 0); + } + + /** + * Resets the date to end of the month and time to 23:59:59 + * + * @return static + */ + public function endMonth() + { + return $this->setDateTime($this->year, $this->month, $this->daysInMonth, 23, 59, 59); + } + + /** + * Resets the date to the first day of the quarter and the time to 00:00:00 + * + * @return static + */ + public function startQuarter() + { + $month = ($this->quarter - 1) * static::MONTHS_PER_QUARTER + 1; + + return $this->setDateTime($this->year, $month, 1, 0, 0, 0); + } + + /** + * Resets the date to end of the quarter and time to 23:59:59 + * + * @return static + */ + public function endQuarter() + { + return $this->startQuarter()->addMonths(static::MONTHS_PER_QUARTER - 1)->endMonth(); + } + + /** + * Resets the date to the first day of the year and the time to 00:00:00 + * + * @return static + */ + public function startYear() + { + return $this->setDateTime($this->year, 1, 1, 0, 0, 0); + } + + /** + * Resets the date to end of the year and time to 23:59:59 + * + * @return static + */ + public function endYear() + { + $day = $this->format('L') ? 30 : 29; + + return $this->setDateTime($this->year, 12, $day, 23, 59, 59); + } + /***************************** TRANSFORMATION ****************************/ /** @@ -2370,6 +2463,7 @@ class Verta extends DateTime { } return array($jy, $i + 1, $j_day_no + 1); } + /** * jalali to gregorian convertion * @@ -2413,6 +2507,7 @@ class Verta extends DateTime { } return array($gy, $i + 1, $g_day_no + 1); } + /** * integer division *