From 95d7007964bf496a60fab03082669dc4c588fa70 Mon Sep 17 00:00:00 2001 From: Nasser Date: Fri, 4 Sep 2020 15:45:59 +0430 Subject: [PATCH] add traits --- docs/css/style.css | 352 ++--- docs/css/style.rtl.css | 385 ----- docs/en/index.html | 402 +++-- docs/index.html | 100 +- src/Traits/Accessor.php | 273 ++++ src/Traits/Boundaries.php | 163 ++ src/Traits/Comparison.php | 606 ++++++++ src/Traits/Creator.php | 407 +++++ src/Traits/Date.php | 152 ++ src/Traits/Difference.php | 104 ++ src/Traits/Formatting.php | 599 ++++++++ src/Traits/Modification.php | 376 +++++ src/Traits/Transformation.php | 98 ++ src/Verta.php | 2714 +-------------------------------- src/VertaInterface.php | 50 + tests/AccessTest.php | 4 +- tests/CreatorTest.php | 2 +- 17 files changed, 3156 insertions(+), 3631 deletions(-) delete mode 100644 docs/css/style.rtl.css create mode 100644 src/Traits/Accessor.php create mode 100644 src/Traits/Boundaries.php create mode 100644 src/Traits/Comparison.php create mode 100644 src/Traits/Creator.php create mode 100644 src/Traits/Date.php create mode 100644 src/Traits/Difference.php create mode 100644 src/Traits/Formatting.php create mode 100644 src/Traits/Modification.php create mode 100644 src/Traits/Transformation.php create mode 100644 src/VertaInterface.php diff --git a/docs/css/style.css b/docs/css/style.css index b1ba70f..69db951 100644 --- a/docs/css/style.css +++ b/docs/css/style.css @@ -13,47 +13,23 @@ } article, -aside, -details, -figcaption, -figure, footer, -header, -hgroup, -main, -nav, -section, -summary { +header { display: block } -audio, -canvas, -video { - display: inline-block -} - -audio:not([controls]) { - display: none; - height: 0 -} - -[hidden] { - display: none -} - html { font-size: 100%; -ms-text-size-adjust: 100%; - -webkit-text-size-adjust: 100% -} - -html { - font-family: sans-serif + -webkit-text-size-adjust: 100%; } body { - margin: 0 + margin: 0; + font-family: 'Markazi Text', sans-serif; + font-size: 15px; + line-height: 27px; + color: #000 } a:focus { @@ -65,34 +41,12 @@ a:hover { outline: 0 } -h1 { - font-size: 2em; - margin: .67em 0 -} - -h2 { - font-size: 1.5em; - margin: .83em 0 -} - -h3 { - font-size: 1.17em; - margin: 1em 0 -} -mark { - background: #ff0; - color: #000 -} - p { - font-size: 14px; -} - -pre { - margin: 1em 0 + font-size: 20px; } pre { + margin: 1em 0; font-family: monospace, serif; font-size: 1em; white-space: pre-wrap; @@ -100,8 +54,8 @@ pre { padding: 10px; direction: ltr; text-align: left; - color: white; - background: gray; + color: #090910;; + background: #fbfbfd; } ul { @@ -112,161 +66,12 @@ ul { list-style-image: none } -.container { - width: 100%; - max-width: 1140px; - margin-right: auto; - margin-left: auto; - padding-left: 15px; - padding-right: 15px -} - -.container:after, -.container:before { - display: table; - content: "" -} - -.container:after { - clear: both -} - -.row { - margin-left: -15px; - margin-right: -15px -} - -.row:after, -.row:before { - display: table; - content: "" -} - -.row:after { - clear: both -} - -.col-lg-3, -.col-lg-9, -.col-md-3, -.col-md-9, -.col-sm-3, -.col-sm-9, -.col-xs-3, -.col-xs-9 { - position: relative; - min-height: 1px; - padding-left: 15px; - padding-right: 15px; - padding-bottom: 30px -} - -.col-xs-3, -.col-xs-9 { - float: left -} - -.col-xs-3 { - width: 25% -} - -.col-xs-9 { - width: 75% -} - -@media (min-width:768px) { - .col-sm-3 { - float: left - } - .col-sm-3 { - width: 25% - } - .col-sm-9 { - float: left - } - .col-sm-9 { - width: 75% - } -} - -@media (min-width:992px) { - .col-md-3 { - float: left - } - .col-md-3 { - width: 25% - } - .col-md-9 { - float: left - } - .col-md-9 { - width: 75% - } -} - -@media (min-width:1140px) { - .col-lg-3 { - float: left - } - .col-lg-3 { - width: 25% - } - .col-lg-9 { - float: left - } - .col-lg-9 { - width: 75% - } -} - a, a:visited { color: #666; text-decoration: none } -h1, -h2, -h3 { - font-weight: 400; - line-height: 1.4; - color: #444; - letter-spacing: -.04em -} - -h1 .small, -h1 small, -h2 .small, -h2 small, -h3 .small, -h3 small { - font-weight: 400; - line-height: 1; - color: #b3b3b3 -} - -h1 a, -h2 a, -h3 a { - color: #444 -} - -h1, -h2, -h3 { - margin-top: 20px; - margin-bottom: 10px -} - -h1 .small, -h1 small, -h2 .small, -h2 small, -h3 .small, -h3 small { - font-size: 65% -} - h1 { font-size: 30px; text-transform: uppercase; @@ -276,18 +81,6 @@ h1 { padding: 0 0 20px 0 } -h1::after { - content: ''; - position: absolute; - width: 60px; - height: 3px; - background-color: #444; - left: 50%; - -webkit-transform: translateX(-50%); - transform: translateX(-50%); - bottom: 0 -} - h2 { font-size: 26px; margin-bottom: 20px @@ -297,27 +90,18 @@ h3 { font-size: 24px } -body { - background: #f8f8f8; - font-family: SansSerif; - font-size: 14px; - line-height: 27px; - color: #666 -} - header#header { - background: gray; - color: white; - letter-spacing: -.04em; + background: #fbfbfd; text-align: center; line-height: 60px; padding: 50px 30px; - margin: 0 0 30px 0 + margin: 0 0 30px 0; + border-bottom: 2px solid #ff6f6f; } header#header h1 { - color: white; - font-family: "Sans"; + color: #ff6f6f; + font-family: sans; font-weight: 700; font-size: 70px; line-height: 90px; @@ -331,6 +115,21 @@ header#header h2 { color: lightgray; } +.sidebar { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; +} + +.sticky { + position: -webkit-sticky; + position: sticky; + top: 10px; + padding: 0 15px; +} + footer#footer { background: #ddd; color: #888; @@ -338,35 +137,6 @@ footer#footer { margin-top: 30px } -#go_top { - display: none; - position: fixed; - bottom: 20px; - right: 30px; - z-index: 99; - font-size: 18px; - border: none; - outline: none; - background-color: red; - color: white; - cursor: pointer; - padding: 15px; - border-radius: 4px; - height: 40px; - width: 40px; -} - -footer#footer a, -footer#footer a:visited { - color: #888 -} - -label { - font-weight: 700; - display: block; - margin-bottom: 5px -} - @media (max-width:480px) { header#header { padding: 30px 15px @@ -382,3 +152,63 @@ label { } } +.container { + width: 100%; + padding-left: 15px; + padding-right: 15px; + margin-left: auto; + margin-right: auto; +} + +@media (min-width: 576px) { + .container { + max-width: 540px; + } +} + +@media (min-width: 768px) { + .container { + max-width: 720px; + } +} + +@media (min-width: 992px) { + .container { + max-width: 960px; + } +} + +@media (min-width: 1200px) { + .container { + max-width: 1140px; + } +} + +.row { + display: -ms-flexbox; + display: flex; + -ms-flex-wrap: wrap; + flex-wrap: wrap; + margin-left: -15px; + margin-right: -15px; +} + +.col-sm-3, .col-sm-6 { + position: relative; + width: 100%; + padding-left: 15px; + padding-right: 15px; +} + +@media (min-width: 576px) { + .col-sm-3 { + -ms-flex: 0 0 25%; + flex: 0 0 25%; + max-width: 25%; + } + .col-sm-9 { + -ms-flex: 0 0 75%; + flex: 0 0 75%; + max-width: 75%; + } +} \ No newline at end of file diff --git a/docs/css/style.rtl.css b/docs/css/style.rtl.css deleted file mode 100644 index ba9d402..0000000 --- a/docs/css/style.rtl.css +++ /dev/null @@ -1,385 +0,0 @@ -* { - box-sizing: border-box; - outline: 0 -} - -*, -::after, -::before, -:after, -:before { - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale -} - -article, -aside, -details, -figcaption, -figure, -footer, -header, -hgroup, -main, -nav, -section, -summary { - display: block -} - -audio, -canvas, -video { - display: inline-block -} - -audio:not([controls]) { - display: none; - height: 0 -} - -[hidden] { - display: none -} - -html { - font-size: 100%; - -ms-text-size-adjust: 100%; - -webkit-text-size-adjust: 100% -} - -html { - font-family: sans-serif -} - -body { - margin: 0 -} - -a:focus { - outline: thin dotted -} - -a:active, -a:hover { - outline: 0 -} - -h1 { - font-size: 2em; - margin: .67em 0 -} - -h2 { - font-size: 1.5em; - margin: .83em 0 -} - -h3 { - font-size: 1.17em; - margin: 1em 0 -} -mark { - background: #ff0; - color: #000 -} - -p { - font-size: 20px; -} - -pre { - margin: 1em 0 -} - -pre { - font-family: monospace, serif; - font-size: 1em; - white-space: pre-wrap; - word-wrap: break-word; - padding: 10px; - direction: ltr; - text-align: left; - color: white; - background: gray; -} - -ul { - padding: 0; - font-size: 20px; - margin: 1em 0; - list-style: none; - list-style-image: none -} - -.container { - width: 100%; - max-width: 1140px; - margin-right: auto; - margin-left: auto; - padding-left: 15px; - padding-right: 15px -} - -.container:after, -.container:before { - display: table; - content: "" -} - -.container:after { - clear: both -} - -.row { - margin-left: -15px; - margin-right: -15px -} - -.row:after, -.row:before { - display: table; - content: "" -} - -.row:after { - clear: both -} - -.col-lg-3, -.col-lg-9, -.col-md-3, -.col-md-9, -.col-sm-3, -.col-sm-9, -.col-xs-3, -.col-xs-9 { - position: relative; - min-height: 1px; - padding-left: 15px; - padding-right: 15px; - padding-bottom: 30px -} - -.col-xs-3, -.col-xs-9 { - float: right -} - -.col-xs-3 { - width: 25% -} - -.col-xs-9 { - width: 75% -} - -@media (min-width:768px) { - .col-sm-3 { - float: right - } - .col-sm-3 { - width: 25% - } - .col-sm-9 { - float: right - } - .col-sm-9 { - width: 75% - } -} - -@media (min-width:992px) { - .col-md-3 { - float: right - } - .col-md-3 { - width: 25% - } - .col-md-9 { - float: right - } - .col-md-9 { - width: 75% - } -} - -@media (min-width:1140px) { - .col-lg-3 { - float: right - } - .col-lg-3 { - width: 25% - } - .col-lg-9 { - float: right - } - .col-lg-9 { - width: 75% - } -} - -a, -a:visited { - color: #666; - text-decoration: none -} - -h1, -h2, -h3 { - font-weight: 400; - line-height: 1.4; - color: #444; - letter-spacing: -.04em -} - -h1 .small, -h1 small, -h2 .small, -h2 small, -h3 .small, -h3 small { - font-weight: 400; - line-height: 1; - color: #b3b3b3 -} - -h1 a, -h2 a, -h3 a { - color: #444 -} - -h1, -h2, -h3 { - margin-top: 20px; - margin-bottom: 10px -} - -h1 .small, -h1 small, -h2 .small, -h2 small, -h3 .small, -h3 small { - font-size: 65% -} - -h1 { - font-size: 30px; - text-transform: uppercase; - text-align: center; - margin-bottom: 25px; - position: relative; - padding: 0 0 20px 0 -} - -h1::after { - content: ''; - position: absolute; - width: 60px; - height: 3px; - background-color: #444; - left: 50%; - -webkit-transform: translateX(-50%); - transform: translateX(-50%); - bottom: 0 -} - -h2 { - font-size: 26px; - margin-bottom: 20px -} - -h3 { - font-size: 24px -} - -body { - background: #f8f8f8; - font-family: 'Markazi Text', serif; - font-size: 15px; - line-height: 27px; - color: #666 -} - -header#header { - background: gray; - color: white; - letter-spacing: -.04em; - text-align: center; - line-height: 60px; - padding: 50px 30px; - margin: 0 0 30px 0 -} - -header#header h1 { - color: white; - font-family: sans; - font-weight: 700; - font-size: 70px; - line-height: 90px; - font-weight: 700 -} - -header#header h1::after { - background: white; -} - -header#header h2 { - color: lightgray; -} - -footer#footer { - background: #ddd; - color: #888; - padding: 30px 0; - margin-top: 30px -} - -#go_top { - display: none; - position: fixed; - bottom: 20px; - right: 30px; - z-index: 99; - font-size: 18px; - border: none; - outline: none; - background-color: red; - color: white; - cursor: pointer; - padding: 15px; - border-radius: 4px; - height: 40px; - width: 40px; -} - -footer#footer a, -footer#footer a:visited { - color: #888 -} - -label { - font-weight: 700; - display: block; - margin-bottom: 5px -} - -@media (max-width:480px) { - header#header { - padding: 30px 15px - } - header#header h1 { - font-size: 44px; - line-height: 70px; - padding-bottom: 15px; - margin-bottom: 15px - } - header#header h2 { - font-size: 20px - } -} - diff --git a/docs/en/index.html b/docs/en/index.html index 81e04ce..793fdcf 100644 --- a/docs/en/index.html +++ b/docs/en/index.html @@ -7,7 +7,7 @@ - + Fork me on GitHub @@ -19,44 +19,48 @@
- +
-
-

Installation

-

Enter the following command to install Verta

-
$ composer require hekmatinasser/verta
-

Now you have to add the package to your project by entering the following command

+
+

Installation

+

Enter the following command to install Verta

+
$ composer require hekmatinasser/verta
+

Now you have to add the package to your project by entering the following command

-
// config/app.php
+                
// config/app.php
 'providers' => [
     ...
     Hekmatinasser\Verta\VertaServiceProvider::class,
@@ -66,56 +70,56 @@
     ...
     'Verta' => Hekmatinasser\Verta\Verta::class,
 ]
-

Usage

-

Use the following command at the top of each file of the use Verta class

-
+                

Usage

+

Use the following command at the top of each file of the use Verta class

+
 use Verta;
 // or
 use Hekmatinasser\Verta\Verta;
 
-

Creator

-

To get the current datetime, use the following command

-
+                

Creator

+

To get the current datetime, use the following command

+
 $v = new Verta(); //1396-02-02 15:32:08
 $v = verta(); //1396-02-02 15:32:08
 
-

To get the current date and time, use the following command

-
+                

To get the current date and time, use the following command

+
 $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
 
-

To convert your Gregorian datetime, use the following command

-
+                

To convert your Gregorian datetime, use the following command

+
 $v = new Verta('2016-12-27 14:12:32');
 $v = Verta::instance('2016-12-25 11:12:36');
 $v = Facades\Verta::instance('2016-12-25 11:12:36');
 
-

To use the timestamp number, use the following command

-
+                

To use the timestamp number, use the following command

+
 $v = new Verta(1333857600);
 
-

To use the Datetime class PHP, use the following command

-
+                

To use the Datetime class PHP, use the following command

+
 $dt = new \Datetime();
 return new Verta($dt); // 1395-12-09 15:05:56
 
-

To use the Carbon class, use the following command

-
+                

To use the Carbon class, use the following command

+
 $c = \Carbon::now();
 return verta($c); // 1395-12-09 15:05:56
 
-

To use the jalali datetime as a string, use the following command

-
+                

To use the jalali datetime as a string, use the following command

+
 $v = Verta::parse('1395-10-07 14:12:32');
 $v = Verta::parse('1396 مهر 17');
 $v =  Verta::parseFormat('Y n j','1396 مهر 17');
 
-

If the date values are separate, use the following commands

-
+                

If the date values are separate, use the following commands

+
 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
@@ -126,37 +130,37 @@ 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
 
-

To use the timestamp numeric, use the following command

-
+                

To use the timestamp numeric, use the following command

+
 return Verta::createTimestamp(1488614023); // 1395-12-14 11:23:43
 
-

If the date jalali values are separate, use the following commands

-
+                

If the date jalali values are separate, use the following commands

+
 // 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
 
-

Transformations

-

Gregorian to Jalali date, use the following command

-
+                

Transformations

+

Gregorian to Jalali date, use the following command

+
 return Verta::getJalali(2015,12,25); // [1394,10,4]
 
-

Jalali to Gregorian date, use the following command

-
+                

Jalali to Gregorian date, use the following command

+
 return Verta::getGregorian(1394,10,4); // [2015,12,25]
 
-

Verta to Datatime PHP, use the following command

-
+                

Verta to Datatime PHP, use the following command

+
 $dt = verta()->datetime();
 
-

Verta to Carbon, use the following command

-
+                

Verta to Carbon, use the following command

+
 $c = Carbon::instance($v->datetime());
 
-

Property

-

To get property, use the following command

-
+                

Property

+

To get property, use the following command

+
 $v = verta(); // 1396-03-14 14:18:23
 return $v->year; // 1396
 return $v->quarter // 1
@@ -173,8 +177,8 @@ return $v->daysInMonth; // 31
 return $v->timestamp; // 1496557661
 return $v->timezone; // Asia/Tehran
 
-

To set property, use the following command

-
+                

To set property, use the following command

+
 $v = verta();
 $v->year = 1395;
 $v->month = 4; // set 13 for next year first month
@@ -185,8 +189,8 @@ $v->second = 42;
 $v->timestamp = 1496557661;
 $v->timezone = 'Asia/Baku';
 
-

To set property with method, use the following command

-
+                

To set property with method, use the following command

+
 $v = $v->year(1395);
 $v = $v->month(4); // set 13 for next year first month
 $v = $v->day(25);
@@ -196,141 +200,141 @@ $v = $v->second(42);
 $v = $v->timestamp(1496557661);
 $v = $v->timezone('Asia/Baku');
 
-

To set property together, use the following command

-
+                

To set property together, use the following command

+
 $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');
 
-

Format

-

To output format, use the following command

-
+                

Format

+

To output format, use the following command

+
 $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
 
-

Read the PHP manual to create the format you want

-

- strftime - date -

-

To default output format, use the following command

-
+                

Read the PHP manual to create the format you want

+

+ strftime + date +

+

To default output format, use the following command

+
 Verta::setStringformat('Y/n/j H:i:s');
 return verta(); // 1395/12/12 00:11:35
 
-

To reset output format, use the following command

-
+                

To reset output format, use the following command

+
 Verta::resetStringFormat();
 return verta(); // 1395-12-12 00:18:04
 
-

To display the datetime in standard formats, use the following command

-
+                

To display the datetime in standard formats, use the following command

+
 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
 
-

To output format Gregorian, use the following command

-
+                

To output format Gregorian, use the following command

+
 return $v->formatGregorian('Y-m-d H:i:s'); // 2017-05-23 23:21:02
 
-

To display the date difference as a unit of time, use the following command

-

If you do set parameter, the difference is calculated with the current time

-
+                

To display the date difference as a unit of time, use the following command

+

If you do set parameter, the difference is calculated with the current time

+
 return $v1->formatDifference($v2); // 12 ماه بعد
 return $v1->formatDifference($v3); // 1 سال قبل
 return $v1->addDays(25)->formatDifference(); // 4 هفته بعد
 return $v1->subDays(6)->formatDifference(); // 6 روز قبل
 return verta()->formatDifference(); // الان
 
-

To display numbers in alphabetical order, use the following command

-
+                

To display numbers in alphabetical order, use the following command

+
 return $v->formatWord('Y'); // یک هزار و سیصد و نود و شش
 return $v->formatWord('l dS F'); // چهارشنبه بیست و نه ام شهریور
 return $v->formatWord('d F Y'); // بیست و نه شهریور یک هزار و سیصد و نود و شش
 return $v->formatWord('r'); // چهارشنبه یک هزار و سیصد و نود و شش, شش, بیست و نه, بیست و دو:چهل و نه:سی و هشت +04:30
 return $v->formatWord('d F ') . $v->year; // بیست و نه شهریور 1396
 
-

Modification

-

To add or sub datetime, use the following command

-
+                

Modification

+

To add or sub datetime, use the following command

+
 $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 $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 $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 $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 $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 $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 $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
 
-

To get start or end datetime period, use the following command

-
+                

To get start or end datetime period, use the following command

+
 $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->startQuarter(); // 1396-04-01 00:00:00
 return $v->endQuarter(); // 1396-06-31 23:59:59
 
-
+                
 return $v->startYear(); // 1396-01-01 00:00:00
 return $v->endYear(); // 1396-12-29 23:59:59
 
-

Comparison

-

To validate datetime, use the following command

-
+                

Comparison

+

To validate datetime, use the following command

+
 return Verta::isLeapYear(1394); // false
 return Verta::isValidDate(1394, 12, 30); // false
 return Verta::isValidTime(15, 12, 30); // true
 
-

To difference datetime together, use the following command

-
+                

To difference datetime together, use the following command

+
 // diff objects together
 $v1 = verta(); // 1396-03-31 22:21:40
 $v2 = verta('2017-06-21 01:21:40'); // 1396-03-31 01:21:40
@@ -344,8 +348,8 @@ return $v1->diffDays($v3); // -372
 return $v3->diffMinutes(); // 536548
 return $v3->diffSeconds(); // 12261931
 
-

To compere datetime together, use the following command

-
+                

To compere datetime together, use the following command

+
 return $v1->eq(); // ture alias equalTo()
 return $v1->ne($v2); // true alias notEqualTo()
 return $v1->gt($v2); // true alias greaterThan()
@@ -353,9 +357,9 @@ return $v1->gte($v2); // true alias greaterThanOrEqualTo()
 return $v1->lt($v2); // false alias lessThan()
 return $v1->lte($v2); // false alias lessThanOrEqualTo()
 return $v1->between($v2, $v3); // false    
-
-

To compere datetime for closest, farthest, min or max together, use the following command

-
+
+

To compere datetime for closest, farthest, min or max together, use the following command

+
 return $v1->closest($v2, $v3); // return $v2 object
 return $v1->farthest($v2, $v3); // return $v3 object
 return $v1->minimum($v2); // return $v2
@@ -363,8 +367,8 @@ return $v1->min($v2); // return $v2
 return $v1->maximum($v2); // return $v1
 return $v1->max($v2); // return $v1
 
-

To compere datetime now, use the following command

-
+                

To compere datetime now, use the following command

+
 return $v1->isWeekday(); // true
 return $v1->isWeekend(); // false
 return $v1->isYesterday(); // false
@@ -396,10 +400,10 @@ return $v1->isWednesday(); // false
 return $v1->isThursday(); // true
 return $v1->isFriday(); // false
 
-

Laravel Validator

-

You can validate form with Validator Laravel

-

You need to define error messages for jdate, ... rules in validation.php in lang folders.

-
+                

Laravel Validator

+

You can validate form with Validator Laravel

+

You need to define error messages for jdate, ... rules in validation.php in lang folders.

+
 'jdate' => 'The :attribute is not valid Jalali date',
 'jdate_equal' => 'The :attribute is not equal Jalali date  :date',
 'jdate_not_equal' => 'The :attribute is\'nt not equal Jalali date  :date',
@@ -420,118 +424,118 @@ return $v1->isFriday(); // false
     'expire_datetime' => 'expire datetime',
 ],
 
-

Validation Rules

-

Determines if an input is a valid Jalali date with the specified format

-

The default format is Y/m/d

-
+                

Validation Rules

+

Determines if an input is a valid Jalali date with the specified format

+

The default format is Y/m/d

+
 'start_date' => 'jdate'
 'start_date' => 'jdate:Y/m/d'
 
-

Determines if an input is a valid Jalali date with the specified format and it is equal a given date

-

The default format is Y/m/d

+

Determines if an input is a valid Jalali date with the specified format and it is equal a given date

+

The default format is Y/m/d

-
+                
 'start_date' => 'jdate_equal'
 'start_date' => 'jdate_equal:1388/01/01'
 'start_date' => 'jdate_equal:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali date with the specified format and it is not equal a given date

-

The default format is Y/m/d

+

Determines if an input is a valid Jalali date with the specified format and it is not equal a given date

+

The default format is Y/m/d

-
+                
 'start_date' => 'jdate_not_equal'
 'start_date' => 'jdate_not_equal:1388/01/01'
 'start_date' => 'jdate_not_equal:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali datetime with the specified format

-

The default format is Y/m/d H:i:s

-
+                

Determines if an input is a valid Jalali datetime with the specified format

+

The default format is Y/m/d H:i:s

+
 'start_date' => 'jdatetime'
 'start_date' => 'jdatetime:Y/m/d H:i:s'
 
-

Determines if an input is a valid Jalali datetime with the specified format and it is equal a given datetime

-

The default format is Y/m/d H:i:s

-
+                

Determines if an input is a valid Jalali datetime with the specified format and it is equal a given datetime

+

The default format is Y/m/d H:i:s

+
 'start_date' => 'jdatetime_equal'
 'start_date' => 'jdatetime_equal:1388/01/01 12:56:28'
 'start_date' => 'jdatetime_equal:1388/01/01 12:56:28,Y/m/d H:i:s'
 
-

Determines if an input is a valid Jalali datetime with the specified format and it is not equal a given datetime

-

The default format is Y/m/d H:i:s

-
+                

Determines if an input is a valid Jalali datetime with the specified format and it is not equal a given datetime

+

The default format is Y/m/d H:i:s

+
 'start_date' => 'jdatetime_not_equal'
 'start_date' => 'jdatetime_not_equal:1388/01/01 12:56:28'
 'start_date' => 'jdatetime_not_equal:1388/01/01 12:56:28,Y/m/d H:i:s'
 
-

Determines if an input is a valid Jalali date with the specified format and it is after a given date

-

The default date is now

-

The default format is Y/m/d

-
+                

Determines if an input is a valid Jalali date with the specified format and it is after a given date

+

The default date is now

+

The default format is Y/m/d

+
 'start_date' => 'jdate_after'
 'start_date' => 'jdate_after:1388/01/01'
 'start_date' => 'jdate_after:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali datetime with the specified format and it is after or equal a given datetime

-

The default date is now

-

The default format is Y/m/d

-
+                

Determines if an input is a valid Jalali datetime with the specified format and it is after or equal a given datetime

+

The default date is now

+

The default format is Y/m/d

+
 'start_date' => 'jdate_after_equal'
 'start_date' => 'jdate_after_equal:1388/01/01'
 'start_date' => 'jdate_after_equal:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali datetime with the specified format and it is after a given datetime

-

The default date is now

-

The default format is Y/m/d H:i:s

-
+                

Determines if an input is a valid Jalali datetime with the specified format and it is after a given datetime

+

The default date is now

+

The default format is Y/m/d H:i:s

+
 'start_date' => 'jdatetime_after'
 'start_date' => 'jdatetime_after:1388/01/01'
 'start_date' => 'jdatetime_after:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali datetime with the specified format and it is after or equal a given datetime

-

The default date is now

-

The default format is Y/m/d H:i:s

-
+                

Determines if an input is a valid Jalali datetime with the specified format and it is after or equal a given datetime

+

The default date is now

+

The default format is Y/m/d H:i:s

+
 'start_date' => 'jdatetime_after_equal'
 'start_date' => 'jdatetime_after_equal:1388/01/01'
 'start_date' => 'jdatetime_after_equal:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali date with the specified format and it is before a given date

-

The default date is now

-

The default format is Y/m/d

-
+                

Determines if an input is a valid Jalali date with the specified format and it is before a given date

+

The default date is now

+

The default format is Y/m/d

+
 'start_date' => 'jdate_before'
 'start_date' => 'jdate_before:1388/01/01'
 'start_date' => 'jdate_before:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali date with the specified format and it is before or equal a given date

-

The default date is now

-

The default format is Y/m/d

-
+                

Determines if an input is a valid Jalali date with the specified format and it is before or equal a given date

+

The default date is now

+

The default format is Y/m/d

+
 'start_date' => 'jdate_before_equal'
 'start_date' => 'jdate_before_equal:1388/01/01'
 'start_date' => 'jdate_before_equal:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali datetime with the specified format and it is before a given date-time

-

The default date is now

-

The default format is Y/m/d H:i:s

-
+                

Determines if an input is a valid Jalali datetime with the specified format and it is before a given date-time

+

The default date is now

+

The default format is Y/m/d H:i:s

+
 'start_date' => 'jdatetime_before'
 'start_date' => 'jdatetime_before:1388/01/01'
 'start_date' => 'jdatetime_before:1388/01/01,Y/m/d'
 
-

Determines if an input is a valid Jalali datetime with the specified format and it is before or equal a given datetime

-

The default date is now

-

The default format is Y/m/d H:i:s

-
+                

Determines if an input is a valid Jalali datetime with the specified format and it is before or equal a given datetime

+

The default date is now

+

The default format is Y/m/d H:i:s

+
 'start_date' => 'jdatetime_before_equal'
 'start_date' => 'jdatetime_before_equal:1388/01/01'
 'start_date' => 'jdatetime_before_equal:1388/01/01,Y/m/d'
 
-

Can combine the rules of the between the two boundaries

-
+                

Can combine the rules of the between the two boundaries

+
 $validate = Validator::make([
     'start_date' => '1389/01/31',
     'expire_datetime' => '1397/02/16 12:10:00',
@@ -555,41 +559,15 @@ array:2 [
   ]
 ]
 
-
+
- - diff --git a/docs/index.html b/docs/index.html index ef441cd..4771357 100644 --- a/docs/index.html +++ b/docs/index.html @@ -8,47 +8,52 @@ - + Fork me on GitHub
@@ -353,8 +358,8 @@ return $v1->gt($v2); // true alias greaterThan() return $v1->gte($v2); // true alias greaterThanOrEqualTo() return $v1->lt($v2); // false alias lessThan() return $v1->lte($v2); // false alias lessThanOrEqualTo() -return $v1->between($v2, $v3); // false - +return $v1->between($v2, $v3); // false +

برای بررسی تاریخ از لحاظ نزدیکتر یا دورتر بودن و یا کوچکتر و بزرگتر بودن، از دستور زیر استفاده کنید

 return $v1->closest($v2, $v3); // return $v2 object
@@ -562,35 +567,10 @@ array:2 [
 
- - diff --git a/src/Traits/Accessor.php b/src/Traits/Accessor.php new file mode 100644 index 0000000..68dafc2 --- /dev/null +++ b/src/Traits/Accessor.php @@ -0,0 +1,273 @@ + 'Y', + 'month' => 'n', + 'day' => 'j', + 'hour' => 'G', + 'minute' => 'i', + 'second' => 's', + 'micro' => 'u', + 'dayOfWeek' => 'w', + 'dayOfYear' => 'z', + 'weekOfYear' => 'W', + 'daysInMonth' => 't', + 'timestamp' => 'U', + ); + + switch (true) { + case isset($formats[$name]): + return (int) $this->format($formats[$name]); + + case $name === 'quarter': + return (int) ceil($this->month / VertaInterface::MONTHS_PER_QUARTER); + + case $name === 'timezone': + return $this->getTimezone()->getName(); + + default: + throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); + } + } + + /** + * Check if an attribute exists on the object + * + * @param string $name + * + * @return bool + */ + public function __isset($name) + { + try { + $this->__get($name); + } catch (InvalidArgumentException $e) { + return false; + } + + return true; + } + + /** + * Set a part of the Verta object + * + * @param string $name + * @param string|int|\DateTimeZone $value + * + * @throws \InvalidArgumentException + */ + public function __set($name, $value) + { + switch ($name) { + case 'year': + case 'month': + case 'day': + case 'hour': + case 'minute': + case 'second': + list($year, $month, $day, $hour, $minute, $second) = explode('-', $this->format('Y-n-j-G-i-s')); + $$name = $value; + $this->setDateTime($year, $month, $day, $hour, $minute, $second); + break; + + case 'timestamp': + $this->setTimestamp($value); + break; + + case 'timezone': + case 'tz': + $this->setTimezone(new DateTimeZone($value)); + break; + + default: + throw new InvalidArgumentException(sprintf("Unknown setter '%s'", $name)); + } + } + + /** + * Set the instance's year + * + * @param int $value + * + * @return static + */ + public function year($value) + { + $this->year = $value; + + return $this; + } + + /** + * Set the instance's month + * + * @param int $value + * + * @return static + */ + public function month($value) + { + $this->month = $value; + + return $this; + } + + /** + * Set the instance's day + * + * @param int $value + * + * @return static + */ + public function day($value) + { + $this->day = $value; + + return $this; + } + + /** + * Set the instance's hour + * + * @param int $value + * + * @return static + */ + public function hour($value) + { + $this->hour = $value; + + return $this; + } + + /** + * Set the instance's minute + * + * @param int $value + * + * @return static + */ + public function minute($value) + { + $this->minute = $value; + + return $this; + } + + /** + * Set the instance's second + * + * @param int $value + * + * @return static + */ + public function second($value) + { + $this->second = $value; + + return $this; + } + + /** + * Set the instance's timestamp + * + * @param int $value + * + * @return static + */ + public function timestamp($value) + { + return parent::setTimestamp($value); + } + + /** + * Alias for setTimezone() + * + * @param \DateTimeZone|string $value + * + * @return static + */ + public function timezone($value) + { + return parent::setTimezone(new DateTimeZone($value)); + } + + /** + * Set the date and time all together + * + * @param int $year + * @param int $month + * @param int $day + * @param int $hour + * @param int $minute + * @param int $second + * @param int $microseconds + * + * @return Verta + */ + public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0) + { + return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second, $microseconds); + } + + /** + * Sets the current date of the DateTime object to a different date. + * Calls modify as a workaround for a php bug + * + * @param int $year + * @param int $month + * @param int $day + * + * @return static + */ + public function setDate($year, $month, $day) + { + list($year, $month, $day) = self::getGregorian($year, $month, $day); + + return parent::setDate($year, $month, $day); + } + + /** + * Set the time by time string + * + * @param string $time + * + * @return static + * + * @throws \InvalidArgumentException + */ + public function setTimeString($time) + { + $time = explode(':', $time); + + $hour = $time[0]; + $minute = isset($time[1]) ? $time[1] : 0; + $second = isset($time[2]) ? $time[2] : 0; + + return $this->setTime($hour, $minute, $second, 0); + } + +} \ No newline at end of file diff --git a/src/Traits/Boundaries.php b/src/Traits/Boundaries.php new file mode 100644 index 0000000..e412455 --- /dev/null +++ b/src/Traits/Boundaries.php @@ -0,0 +1,163 @@ +setTime($this->hour, $this->minute, 0); + } + + /** + * Resets the time to 23:59:59 + * + * @return static + */ + public function endMinute() + { + return $this->setTime($this->hour, $this->minute, 59); + } + + /** + * Resets the time to 00:00:00 + * + * @return static + */ + public function startHour() + { + return $this->setTime($this->hour, 0, 0); + } + + /** + * Resets the time to 23:59:59 + * + * @return static + */ + public function endHour() + { + return $this->setTime($this->hour, 59, 59); + } + + /** + * Resets the time to 00:00:00 + * + * @return static + */ + public function startDay() + { + return $this->setTime(0, 0, 0); + } + + /** + * Resets the time to 23:59:59 + * + * @return static + */ + 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() + { + $month = $this->quarter * static::MONTHS_PER_QUARTER; + + return $this->setDate($this->year, $month, 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); + } + +} \ No newline at end of file diff --git a/src/Traits/Comparison.php b/src/Traits/Comparison.php new file mode 100644 index 0000000..7c82912 --- /dev/null +++ b/src/Traits/Comparison.php @@ -0,0 +1,606 @@ + 32766) { + return false; + } + if($month < 1 || $month > 12) { + return false; + } + $dayLastMonthJalali = static::isLeapYear($year) && ($month == 12) ? 30 : static::$daysMonthJalali[intval($month)-1]; + if($day < 1 || $day > $dayLastMonthJalali) { + return false; + } + return true; + } + + /** + * validate a time + * + * @param int $hour + * @param int $minute + * @param int $second + * @return bool + */ + public static function isValidTime($hour, $minute, $second) { + return $hour >= 0 && $hour <= 23 + && $minute >= 0 && $minute <= 59 + && $second >= 0 && $second <= 59; + } + + /** + * Determines if the instance is equal to another + * + * @param Verta $v + * + * @return bool + */ + public function eq(Verta $v = null) + { + return $this == ($v ?: new static()); + } + + /** + * Determines if the instance is equal to another + * + * @param Verta $v + * + * @see eq() + * + * @return bool + */ + public function equalTo(Verta $v = null) + { + return $this->eq($v); + } + + /** + * Determines if the instance is not equal to another + * + * @param Verta $v + * + * @return bool + */ + public function ne(Verta $v = null) + { + return !$this->eq($v); + } + + /** + * Determines if the instance is not equal to another + * + * @param Verta $v + * + * @see ne() + * + * @return bool + */ + public function notEqualTo(Verta $v = null) + { + return $this->ne($v); + } + + /** + * Determines if the instance is greater (after) than another + * + * @param Verta $v + * + * @return bool + */ + public function gt(Verta $v = null) + { + return $this > ($v ?: new static()); + } + + /** + * Determines if the instance is greater (after) than another + * + * @param Verta $v + * + * @see gt() + * + * @return bool + */ + public function greaterThan(Verta $v = null) + { + return $this->gt($v); + } + + /** + * Determines if the instance is greater (after) than or equal to another + * + * @param Verta $v + * + * @return bool + */ + public function gte(Verta $v = null) + { + return $this >= ($v ?: new static()); + } + + /** + * Determines if the instance is greater (after) than or equal to another + * + * @param Verta $v + * + * @see gte() + * + * @return bool + */ + public function greaterThanOrEqualTo(Verta $v = null) + { + return $this->gte($v); + } + + /** + * Determines if the instance is less (before) than another + * + * @param Verta $v + * + * @return bool + */ + public function lt(Verta $v = null) + { + return $this < ($v ?: new static()); + } + + /** + * Determines if the instance is less (before) than another + * + * @param Verta $v + * + * @see lt() + * + * @return bool + */ + public function lessThan(Verta $v = null) + { + return $this->lt($v); + } + + /** + * Determines if the instance is less (before) or equal to another + * + * @param Verta $v + * + * @return bool + */ + public function lte(Verta $v = null) + { + return $this <= ($v ?: new static()); + } + + /** + * Determines if the instance is less (before) or equal to another + * + * @param Verta $v + * + * @see lte() + * + * @return bool + */ + public function lessThanOrEqualTo(Verta $v = null) + { + return $this->lte($v); + } + + /** + * Determines if the instance is between two others + * + * @param Verta $v1 + * @param Verta $v2 + * @param bool $equal Indicates if a > and < comparison should be used or <= or >= + * + * @return bool + */ + public function between(Verta $v1, Verta $v2, $equal = true) + { + if ($v1->gt($v2)) { + $temp = $v1; + $v1 = $v2; + $v2 = $temp; + } + + if ($equal) { + return $this->gte($v1) && $this->lte($v2); + } + + return $this->gt($v1) && $this->lt($v2); + } + + /** + * Get the closest date from the instance. + * + * @param Verta $v1 + * @param Verta $v2 + * + * @return static + */ + public function closest(Verta $v1, Verta $v2) + { + return $this->diffSeconds($v1) < $this->diffSeconds($v2) ? $v1 : $v2; + } + + /** + * Get the farthest date from the instance. + * + * @param Verta $v1 + * @param Verta $v2 + * + * @return static + */ + public function farthest(Verta $v1, Verta $v2) + { + return $this->diffSeconds($v1) > $this->diffSeconds($v2) ? $v1 : $v2; + } + + /** + * Get the minimum instance between a given instance (default now) and the current instance. + * + * @param Verta|null $v + * + * @return static + */ + public function min(Verta $v = null) + { + $v = $v ?: static::now($this->getTimezone()); + + return $this->lt($v) ? $this : $v; + } + + /** + * Get the minimum instance between a given instance (default now) and the current instance. + * + * @param Verta|null $v + * + * @see min() + * + * @return static + */ + public function minimum(Verta $v = null) + { + return $this->min($v); + } + + /** + * Get the maximum instance between a given instance (default now) and the current instance. + * + * @param Verta|null $v + * + * @return static + */ + public function max(Verta $v = null) + { + $v = $v ?: static::now($this->getTimezone()); + + return $this->gt($v) ? $this : $v; + } + + /** + * Get the maximum instance between a given instance (default now) and the current instance. + * + * @param Verta|null $v + * + * @see max() + * + * @return static + */ + public function maximum(Verta $v = null) + { + return $this->max($v); + } + + /** + * Determines if the instance is a weekday + * + * @return bool + */ + public function isWeekday() + { + return !$this->isWeekend(); + } + + /** + * Determines if the instance is a weekend day + * + * @return bool + */ + public function isWeekend() + { + return in_array($this->dayOfWeek, static::$weekendDays); + } + + /** + * Determines if the instance is yesterday + * + * @return bool + */ + public function isYesterday() + { + return $this->formatDate() === static::yesterday($this->getTimezone())->formatDate(); + } + + /** + * Determines if the instance is today + * + * @return bool + */ + public function isToday() + { + return $this->formatDate() === static::now($this->getTimezone())->formatDate(); + } + + /** + * Determines if the instance is tomorrow + * + * @return bool + */ + public function isTomorrow() + { + return $this->formatDate() === static::tomorrow($this->getTimezone())->formatDate(); + } + + /** + * Determines if the instance is within the next week + * + * @return bool + */ + public function isNextWeek() : bool + { + return $this->weekOfYear === static::now($this->getTimezone())->addWeek()->weekOfYear; + } + + /** + * Determines if the instance is within the last week + * + * @return bool + */ + public function isLastWeek() + { + return $this->weekOfYear === static::now($this->getTimezone())->subWeek()->weekOfYear; + } + + /** + * Determines if the instance is within the next month + * + * @return bool + */ + public function isNextMonth() + { + return $this->month === static::now($this->getTimezone())->addMonth()->month; + } + + /** + * Determines if the instance is within the last month + * + * @return bool + */ + public function isLastMonth() + { + return $this->month === static::now($this->getTimezone())->subMonth()->month; + } + + /** + * Determines if the instance is within next year + * + * @return bool + */ + public function isNextYear() + { + return $this->year === static::now($this->getTimezone())->addYear()->year; + } + + /** + * Determines if the instance is within the previous year + * + * @return bool + */ + public function isLastYear() + { + return $this->year === static::now($this->getTimezone())->subYear()->year; + } + + /** + * Determines if the instance is in the future, ie. greater (after) than now + * + * @return bool + */ + public function isFuture() + { + return $this->gt(static::now($this->getTimezone())); + } + + /** + * Determines if the instance is in the past, ie. less (before) than now + * + * @return bool + */ + public function isPast() + { + return $this->lt(static::now($this->getTimezone())); + } + + /** + * Compares the formatted values of the two dates. + * + * @param string $format The date formats to compare. + * @param Verta|null $v The instance to compare with or null to use current day. + * + * @return bool + */ + public function isSameAs($format, Verta $v = null) + { + $v = $v ?: static::now($this->timezone); + + return $this->format($format) === $v->format($format); + } + + /** + * Determines if the instance is in the current year + * + * @return bool + */ + public function isCurrentYear() + { + return $this->isSameYear(); + } + + /** + * Checks if the passed in date is in the same year as the instance year. + * + * @param Verta|null $v The instance to compare with or null to use current day. + * + * @return bool + */ + public function isSameYear(Verta $v = null) + { + return $this->isSameAs('Y', $v); + } + + /** + * Determines if the instance is in the current month + * + * @return bool + */ + public function isCurrentMonth() + { + return $this->isSameMonth(); + } + + /** + * Checks if the passed in date is in the same month as the instance month (and year if needed). + * + * @param Verta|null $v The instance to compare with or null to use current day. + * @param bool $ofSameYear Check if it is the same month in the same year. + * + * @return bool + */ + public function isSameMonth(Verta $v = null, $ofSameYear = false) + { + $format = $ofSameYear ? 'Y-m' : 'm'; + + return $this->isSameAs($format, $v); + } + + /** + * Checks if the passed in date is the same day as the instance current day. + * + * @param Verta $v + * + * @return bool + */ + public function isSameDay(Verta $v) + { + return $this->formatDate() === $v->formatDate(); + } + + /** + * Check if its the birthday. Compares the date/month values of the two dates. + * + * @param Verta|null $dt The instance to compare with or null to use current day. + * + * @return bool + */ + public function isBirthday(Verta $v = null) + { + return $this->isSameAs('md', $v); + } + + /** + * Checks if this day is a Saturday. + * + * @return bool + */ + public function isSaturday() + { + return $this->dayOfWeek === static::SATURDAY; + } + + /** + * Checks if this day is a Sunday. + * + * @return bool + */ + public function isSunday() + { + return $this->dayOfWeek === static::SUNDAY; + } + + /** + * Checks if this day is a Monday. + * + * @return bool + */ + public function isMonday() + { + return $this->dayOfWeek === static::MONDAY; + } + + /** + * Checks if this day is a Tuesday. + * + * @return bool + */ + public function isTuesday() + { + return $this->dayOfWeek === static::TUESDAY; + } + + /** + * Checks if this day is a Wednesday. + * + * @return bool + */ + public function isWednesday() + { + return $this->dayOfWeek === static::WEDNESDAY; + } + + /** + * Checks if this day is a Thursday. + * + * @return bool + */ + public function isThursday() + { + return $this->dayOfWeek === static::THURSDAY; + } + + /** + * Checks if this day is a Friday. + * + * @return bool + */ + public function isFriday() + { + return $this->dayOfWeek === static::FRIDAY; + } + +} \ No newline at end of file diff --git a/src/Traits/Creator.php b/src/Traits/Creator.php new file mode 100644 index 0000000..bae0574 --- /dev/null +++ b/src/Traits/Creator.php @@ -0,0 +1,407 @@ +getTimestamp(); + } + if (is_int($datetime)) { + $datetime = "@$datetime"; + } + + try { + parent::__construct($datetime, static::createTimeZone($timezone) ?: null); + } catch (Exception $exception) { + throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime)); + } + } + + /** + * Create a Verta now datetime + * + * + * @param null $timezone + * @return static + */ + public static function now($timezone = null) { + return new static(null, $timezone); + } + + /** + * Create a Verta instance for today. + * + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + + public static function today($timezone = null) + { + return static::now($timezone)->startDay(); + } + + /** + * Create a Verta instance for tomorrow. + * + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function tomorrow($timezone = null) + { + return static::today($timezone)->addDay(); + } + + /** + * Create a Verta instance for yesterday. + * + * @param \DateTimeZone|string|null $timezone + * + * @return static + */ + public static function yesterday($timezone = null) + { + return static::today($timezone)->subDay(); + } + + /** + * Create a Verta instance from a DateTime one + * + * @param $datetime [optional] + * @param bool $timezone [optional] + * + * @return static + */ + public static function instance($datetime = null, $timezone = null) { + return new static($datetime, $timezone); + } + + /** + * Get a copy of the instance. + * + * @return static + */ + public function copy() + { + return clone $this; + } + + /** + * Get a copy of the instance. + * + * @return static + */ + public function clone() + { + return clone $this; + } + + /** + * Create a DateTime instance from Verta + * + * @return datetime $datetime + */ + public function datetime() { + return new DateTime("@" . $this->getTimestamp(), $this->getTimeZone()); + } + + /** + * Create a Verta instance from a DateTime one + * + * @param $datetime [optional] + * @param bool $timezone [optional] + * + * @return static + */ + public static function parse($datetime , $timezone = null) { + $names = array_map(function ($value) { + return " $value "; + }, self::$monthYear); + + $values = array_map(function ($value) { + return "-$value-"; + }, range(1,12)); + $formatted = str_replace($names, $values, $datetime); + $formatted = str_replace(self::$monthYear, range(1,12), $formatted); + + $parse = date_parse($formatted); + + if($parse['error_count'] > 0 && !self::isValidDate($parse['year'], $parse['month'], $parse['day']) && !self::isValidTime($parse['hour'], $parse['minute'], $parse['second'])) { + throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime)); + } + + 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)); + return new static($datetime, $timezone); + } + + /** + * Create a Verta instance from a DateTime one + * + * @param string $format + * @param string $datetime [optional] + * @param bool $timezone [optional] + * @return static + */ + public static function parseFormat($format, $datetime, $timezone = null) { + $formatted = str_replace(self::$monthYear, range(1,12), $datetime); + + $parse = date_parse_from_format($format, $formatted); + if($parse['error_count'] > 0 && !self::isValidDate($parse['year'], $parse['month'], $parse['day']) && !self::isValidTime($parse['hour'], $parse['minute'], $parse['second'])) { + throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime)); + } + 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)); + return new static($datetime, $timezone); + + } + + /** + * 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 create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) + { + return static::createGregorian($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 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 gregorian. + * + * @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 + */ + protected static function createTimeZone($timezone = null) { + if ($timezone === null) { + $tz = new DateTimeZone(date_default_timezone_get()); + } + elseif ($timezone instanceof DateTimeZone) { + $tz = $timezone; + } + else { + $tz = @timezone_open(strval($timezone)); + if ($tz === false) { + throw new InvalidArgumentException(sprintf("Unknown timezone '%s'", $tz)); + } + } + + return $tz; + } + + /** + * 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 = (new DateTime())->format('Y-n-j-G-i-s'); + $defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now)); + + $year = $year ?? intval($defaults['year']); + $month = $month ?? intval($defaults['month']); + $day = $day ?? intval($defaults['day']); + $hour = $hour ?? intval($defaults['hour']); + $minute = $minute ?? intval($defaults['minute']); + $second = $second ?? intval($defaults['second']); + + if (!checkdate($month, $day, $year) || !static::isValidTime($hour, $minute, $second)) { + throw new \InvalidArgumentException('Unknown datetime'); + } + + return new static(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 = (new static())->format('Y-n-j-G-i-s'); + $defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now)); + + $year = $year ?? intval($defaults['year']); + $month = $month ?? intval($defaults['month']); + $day = $day ?? intval($defaults['day']); + $hour = $hour ?? intval($defaults['hour']); + $minute = $minute ?? intval($defaults['minute']); + $second = $second ?? intval($defaults['second']); + + if (!static::isValidDate($year, $month, $day) || !static::isValidTime($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); + } + +} \ No newline at end of file diff --git a/src/Traits/Date.php b/src/Traits/Date.php new file mode 100644 index 0000000..1f35ae3 --- /dev/null +++ b/src/Traits/Date.php @@ -0,0 +1,152 @@ +getTimezone()); + + return (int) $this->diff($v->datetime())->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->datetime())->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->datetime())->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(); + } + +} \ No newline at end of file diff --git a/src/Traits/Formatting.php b/src/Traits/Formatting.php new file mode 100644 index 0000000..ead783f --- /dev/null +++ b/src/Traits/Formatting.php @@ -0,0 +1,599 @@ +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( '-', parent::format( 'Y-m-d-w' ) ); + 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 .= static::isLeapYear( $pYear ) && ( $pMonth == 12 ) ? 30 : static::$daysMonthJalali[ intval( $pMonth ) - 1 ]; + break; + + # Years + case 'L': + $result .= intval( $this->isLeapYear( $pYear ) ); + break; + + case 'Y': + case 'o': + $result .= $pYear; + break; + + case 'y': + $result .= substr( $pYear, 2 ); + break; + + # Time + case 'a': + case 'A': + if (parent::format( 'a' ) == '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 .= parent::format( $par ); + break; + + # Full Date/Time + case 'c': + $result .= ( $pYear . '-' . $pMonth . '-' . $pDay . ' ' . parent::format( 'H:i:s P' ) ); + break; + + case 'r': + $result .= ( substr( static::$dayWeek[ $pWeek ], 0, 2 ) . '، ' . $pDay . ' ' . substr( static::$monthYear[ $pMonth ], 0, 6 ) . ' ' . $pYear . ' ' . parent::format( 'H:i:s P' ) ); + break; + + case 'U': + $result .= $timestamp; + break; + + default: + $result .= $par; + } + + $i++; + } + return $result; + } + + /** + * The format of the outputted date string (jalali equivalent of php date() function) + * + * @param string $format for example 'Y-m-d H:i:s' + * @return string + */ + protected function dateWord($format){ + + $timestamp = $this->getTimestamp(); + + list($gYear, $gMonth, $gDay, $gWeek) = explode('-', date('Y-m-d-w', $timestamp)); + list($pYear, $pMonth, $pDay) = static::getJalali($gYear, $gMonth, $gDay); + $pWeek = ($gWeek + 1); + + if ($pWeek >= 7) { + $pWeek = 0; + } + + if ($format == '\\') { + $format = '//'; + } + + $lenghFormat = strlen($format); + $i = 0; + $result = ''; + + $word = new Notowo(0, 'fa'); + + while ($i < $lenghFormat) { + $par = $format[$i]; + if ($par == '\\') { + $result .= $format[++$i]; + $i ++; + continue; + } + switch ($par) { + # Day + case 'd': + case 'j': + $result .= $word->getWord(strval($pDay)); + break; + + case 'D': + $result .= substr(static::$dayWeek[$pWeek], 0, 2); + break; + + case 'l': + $result .= static::$dayWeek[$pWeek]; + break; + + case 'N': + $result .= $word->getWord(strval($pWeek + 1)); + break; + + case 'w': + $result .= $word->getWord(strval($pWeek)); + break; + + case 'z': + $result .= $word->getWord(strval($this->daysYear($pMonth, $pDay))); + break; + + case 'S': + $result .= self::NUMBER_TH; + break; + + # Week + case 'W': + $result .= $word->getWord(strval(ceil($this->daysYear($pMonth, $pDay) / 7))); + break; + + # Month + case 'F': + $result .= static::$monthYear[$pMonth-1]; + break; + + case 'm': + case 'n': + $result .= $word->getWord(strval($pMonth)); + break; + + case 'M': + $result .= substr(static::$monthYear[$pMonth-1], 0, 6); + break; + + case 't': + $result .= $word->getWord(strval(static::isLeapYear($pYear) && ($pMonth == 12) ? 30 : static::$daysMonthJalali[intval($pMonth)-1])); + break; + + # Years + case 'L': + $result .= intval($this->isLeapYear($pYear)); + break; + + case 'Y': + case 'o': + $result .= $word->getWord(strval($pYear)); + break; + + case 'y': + $result .= $word->getWord(strval(substr($pYear, 2))); + break; + + # Time + case 'a': + case 'A': + if (date('a', $timestamp) == 'am') { + $result .= (($par == 'a') ? self::AM : self::ANTE_MERIDIEM); + } else { + $result .= (($par == 'a') ? self::PM : self::POST_MERIDIEM); + } + break; + + case 'B': + case 'g': + case 'G': + case 'h': + case 'H': + case 's': + case 'u': + case 'i': + $result .= $word->getWord(strval(date($par, $timestamp))); + break; + case 'e': + case 'I': + case 'O': + case 'P': + case 'T': + case 'Z': + $result .= date($par, $timestamp); + break; + + # Full Date/Time + case 'c': + $result .= $this->dateWord('Y, m, d, H:i:s P'); + break; + + case 'r': + $result .= $this->dateWord('l Y, m, d, H:i:s P'); + break; + + case 'U': + $result .= $word->getWord(strval($timestamp)); + break; + + default: + $result .= $par; + } + $i ++; + } + + return $result; + } + + /** + * return day number from first day of year + * + * @param int $month + * @param int $day + * @return type + * @since 5.0.0 + */ + protected function daysYear($month, $day) { + $days = 0; + for ($i = 1; $i < $month; $i ++) { + $days += static::$daysMonthJalali[$i-1]; + } + return ($days + $day); + } + + /** + * 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", + "%", + ); + + return str_replace($str_format_code, $date_format_code, $format); + } + + /** + * The format of the outputted date string (jalali equivalent of php strftime() function) + * + * @param $format + * @return string + */ + public function format($format) { + return $this->date($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 + * + * @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 formatJalaliDatetime() + { + return $this->format('Y/n/j H:i:s'); + } + + /** + * Format the instance as date + * + * @return string + */ + public function formatJalaliDate() + { + return $this->format('Y/n/j'); + } + + /** + * get difference in all + * + * @param Verta|null $v + * + * @return string + */ + public function formatDifference(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); + } + + public function formatWord($format) + { + return $this->dateWord($this->strftime($format)); + } + + /** + * Convert english numbers to persian numbers + * + * @param string $string + * @return string + */ + public static function enToFaNumbers($string) + { + return str_replace(self::$englishNumber, self::$persianNumber, $string); + } + + /** + * Convert english numbers to persian numbers + * + * @param string $string + * @return string + */ + public static function faToEnNumbers($string) + { + return str_replace(self::$persianNumber, self::$englishNumber, $string); + } + + /** + * Convert english numbers to persian numbers + * + * @param string $string + * @return string + */ + public static function arToEnNumbers($string) + { + return str_replace(self::$arabicNumber, self::$englishNumber, $string); + } + +} \ No newline at end of file diff --git a/src/Traits/Modification.php b/src/Traits/Modification.php new file mode 100644 index 0000000..afbd866 --- /dev/null +++ b/src/Traits/Modification.php @@ -0,0 +1,376 @@ +year($this->year + $value); + } + + /** + * 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); + } + + /** + * Remove a year from the instance + * + * @param int $value + * + * @return static + */ + public function subYear($value = 1) + { + 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; + } + elseif ($month < 1) { + $year += intval($month / 12) - 1; + $month = 12 + ($month % 12); + } + if($month == 0) { + $year--; + $month = 12; + } + if (($month > 6 && $month < 12 && $day == 31)) { + $day--; + } + else { + if ($month == 12 && ($day == 30 || $day == 31)) { + $day = self::isLeapYear($year) ? 30 : 29; + } + } + return self::createJalali($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. + * + * @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 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); + } +} \ No newline at end of file diff --git a/src/Traits/Transformation.php b/src/Traits/Transformation.php new file mode 100644 index 0000000..db1b50c --- /dev/null +++ b/src/Traits/Transformation.php @@ -0,0 +1,98 @@ + 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); + } + + /** + * integer division + * + * @param int $a + * @param int $b + * @return int + */ + private static function div($a, $b) { + return ~~($a / $b); + } +} \ No newline at end of file diff --git a/src/Verta.php b/src/Verta.php index 3a640ba..5a45d70 100755 --- a/src/Verta.php +++ b/src/Verta.php @@ -2,2716 +2,10 @@ namespace Hekmatinasser\Verta; -/* - * This file is part of the Jalali package. - * - * (c) Nasser Hekmati - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - use DateTime; -use DateTimeZone; -use InvalidArgumentException; -use Hekmatinasser\Notowo\Notowo; +use Hekmatinasser\Verta\Traits\Date; -class Verta extends DateTime { +class Verta extends DateTime implements VertaInterface { - /***************************** 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 MONTHS_PER_QUARTER = 3; - 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'; - - /** - * Format to use for __toString. - * - * @var string - */ - protected static $stringFormat = self::DEFAULT_STRING_FORMAT; - - /** - * First day of week. - * - * @var int - */ - protected static $weekStartsAt = self::SATURDAY; - - /** - * Last day of week. - * - * @var int - */ - protected static $weekEndsAt = self::FRIDAY; - - /** - * Days of weekend. - * - * @var array - */ - protected static $weekendDays = array( - self::THURSDAY, - self::FRIDAY, - ); - - /** - * 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 NOW = 'الان'; - const POST = 'بعد'; - - /** - * number day in months gregorian - * - * @var array - */ - protected static $daysMonthGregorian = array( - 31, - 28, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31, - ); - - /** - * 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( - 'فروردین', - 'اردیبهشت', - 'خرداد', - 'تیر', - 'مرداد', - 'شهریور', - 'مهر', - 'آبان', - 'آذر', - 'دی', - 'بهمن', - 'اسفند', - ); - - /** - * 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, - ); - - /** - * arabic number. - * - * @var array - */ - - protected static $arabicNumber = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']; - - /** - * persian number. - * - * @var array - */ - protected static $persianNumber = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; - - /** - * english number. - * - * @var array - */ - protected static $englishNumber = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; - - /***************************** CONSTRUCT ****************************/ - - /** - * create object of Jalali - * - * @param null $datetime - * @param bool $timezone [optional] - * @internal param timestamp $timestamp [optional] - */ - public function __construct($datetime = null, $timezone = null) { - if ($datetime === null) { - $object = new DateTime(); - $instance = $object->getTimestamp(); - } - elseif (is_string($datetime)){ - $datetime = self::faToEnNumbers(self::arToEnNumbers($datetime)); - $object = date_create($datetime); - if ($object === false) { - throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime)); - } - $instance = $object->getTimestamp(); - } - elseif ($datetime instanceof DateTime || $datetime instanceof Verta) { - $instance = $datetime->getTimestamp(); - } - elseif (is_numeric($datetime)) { - $instance = $datetime; - } - else { - throw new \InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime)); - } - - $timezone = static::createTimeZone($timezone); - parent::__construct(date('Y-m-d H:i:s.u', $instance), $timezone); - } - - /** - * Create a Verta now datetime - * - * - * @param null $timezone - * @return static - */ - public static function now($timezone = null) { - return new static(null, $timezone); - } - - /** - * Create a Verta instance for today. - * - * @param \DateTimeZone|string|null $timezone - * - * @return static - */ - - public static function today($timezone = null) - { - return static::now($timezone)->startDay(); - } - - /** - * Create a Verta instance for tomorrow. - * - * @param \DateTimeZone|string|null $timezone - * - * @return static - */ - public static function tomorrow($timezone = null) - { - return static::today($timezone)->addDay(); - } - - /** - * Create a Verta instance for yesterday. - * - * @param \DateTimeZone|string|null $timezone - * - * @return static - */ - public static function yesterday($timezone = null) - { - return static::today($timezone)->subDay(); - } - - /** - * Create a Verta instance from a DateTime one - * - * @param timestamp $datetime [optional] - * @param bool $timezone [optional] - * - * @return static - */ - public static function instance($datetime = null, $timezone = null) { - return new static($datetime, $timezone); - } - - /** - * Get a copy of the instance. - * - * @return static - */ - public function copy() - { - return clone $this; - } - - /** - * 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()); - } - - /** - * 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) { - $monthName = array_map(function ($value) { - return ' ' . $value . ' '; - }, self::$monthYear); - $monthValue = array_map(function ($value) { - return '-' . $value . '-'; - }, range(1,12)); - $datetime = str_replace($monthName, $monthValue, $datetime); - - $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(sprintf("Unknown datetime '%s'", $datetime)); - } - } - - /** - * Create a Verta instance from a DateTime one - * - * @param string $format - * @param string $datetime [optional] - * @param bool $timezone [optional] - * @return static - */ - public static function parseFormat($format,$datetime, $timezone = null) { - $datetime = str_replace(self::$monthYear, range(1,12), $datetime); - - $parse = date_parse_from_format($format, $datetime); - if($parse['error_count'] == 0 && self::isValidDate($parse['year'], $parse['month'], $parse['day']) && self::isValidTime($parse['hour'], $parse['minute'], $parse['second'])){ - 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(sprintf("Unknown datetime '%s'", $datetime)); - } - } - - /** - * 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 create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) - { - return static::createGregorian($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 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 gregorian. - * - * @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 - */ - protected static function createTimeZone($timezone = null) { - if ($timezone === null) { - $newTimezone = new DateTimeZone(date_default_timezone_get()); - } - elseif ($timezone instanceof DateTimeZone) { - $newTimezone = $timezone; - } - else { - $newTimezone = @timezone_open(strval($timezone)); - if ($newTimezone === false) { - throw new InvalidArgumentException(sprintf("Unknown timezone '%s'", $newTimezone)); - } - } - - return $newTimezone; - } - - /** - * 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) - { - - $object = new DateTime(); - $now = date('Y-n-j-G-i-s', $object->getTimestamp()); - $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 (!checkdate($month, $day, $year) || !static::isValidTime($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::isValidDate($year, $month, $day) || !static::isValidTime($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); - } - - /***************************** ACCESS ****************************/ - - /** - * Get a part of the Verta object - * - * @param string $name - * - * @throws \InvalidArgumentException - * - * @return string|int|\DateTimeZone - */ - public function __get($name) - { - static $formats = array( - 'year' => 'Y', - 'month' => 'n', - 'day' => 'j', - 'hour' => 'G', - 'minute' => 'i', - 'second' => 's', - 'micro' => 'u', - 'dayOfWeek' => 'w', - 'dayOfYear' => 'z', - 'weekOfYear' => 'W', - 'daysInMonth' => 't', - 'timestamp' => 'U', - ); - - switch (true) { - case isset($formats[$name]): - 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(); - - default: - throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); - } - } - - /** - * Check if an attribute exists on the object - * - * @param string $name - * - * @return bool - */ - public function __isset($name) - { - try { - $this->__get($name); - } catch (InvalidArgumentException $e) { - return false; - } - - return true; - } - - /** - * Set a part of the Verta object - * - * @param string $name - * @param string|int|\DateTimeZone $value - * - * @throws \InvalidArgumentException - */ - public function __set($name, $value) - { - switch ($name) { - case 'year': - case 'month': - case 'day': - case 'hour': - case 'minute': - case 'second': - list($year, $month, $day, $hour, $minute, $second) = explode('-', $this->format('Y-n-j-G-i-s')); - $$name = $value; - $this->setDateTime($year, $month, $day, $hour, $minute, $second); - break; - - case 'timestamp': - $this->setTimestamp($value); - break; - - case 'timezone': - case 'tz': - $this->setTimezone(new DateTimeZone($value)); - break; - - default: - throw new InvalidArgumentException(sprintf("Unknown setter '%s'", $name)); - } - } - - /** - * Set the instance's year - * - * @param int $value - * - * @return static - */ - public function year($value) - { - $this->year = $value; - - return $this; - } - - /** - * Set the instance's month - * - * @param int $value - * - * @return static - */ - public function month($value) - { - $this->month = $value; - - return $this; - } - - /** - * Set the instance's day - * - * @param int $value - * - * @return static - */ - public function day($value) - { - $this->day = $value; - - return $this; - } - - /** - * Set the instance's hour - * - * @param int $value - * - * @return static - */ - public function hour($value) - { - $this->hour = $value; - - return $this; - } - - /** - * Set the instance's minute - * - * @param int $value - * - * @return static - */ - public function minute($value) - { - $this->minute = $value; - - return $this; - } - - /** - * Set the instance's second - * - * @param int $value - * - * @return static - */ - public function second($value) - { - $this->second = $value; - - return $this; - } - - /** - * Set the instance's timestamp - * - * @param int $value - * - * @return static - */ - public function timestamp($value) - { - return parent::setTimestamp($value); - } - - /** - * Alias for setTimezone() - * - * @param \DateTimeZone|string $value - * - * @return static - */ - public function timezone($value) - { - return parent::setTimezone(new DateTimeZone($value)); - } - - /** - * Set the date and time all together - * - * @param int $year - * @param int $month - * @param int $day - * @param int $hour - * @param int $minute - * @param int $second - * - * @return static - */ - public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0) - { - return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second, $microseconds); - } - - /** - * Sets the current date of the DateTime object to a different date. - * Calls modify as a workaround for a php bug - * - * @param int $year - * @param int $month - * @param int $day - * - * @return static - */ - public function setDate($year, $month, $day) - { - list($year, $month, $day) = self::getGregorian($year, $month, $day); - - return parent::setDate($year, $month, $day); - } - - /** - * Set the time by time string - * - * @param string $time - * - * @return static - * - * @throws \InvalidArgumentException - */ - public function setTimeString($time) - { - $time = explode(':', $time); - - $hour = $time[0]; - $minute = isset($time[1]) ? $time[1] : 0; - $second = isset($time[2]) ? $time[2] : 0; - - return $this->setTime($hour, $minute, $second, 0); - } - - /***************************** STRING FORMATED ****************************/ - - /** - * Reset the format used to the default when type juggling a Verta instance to a string - */ - public static function resetStringFormat() - { - static::setStringFormat(static::DEFAULT_STRING_FORMAT); - } - - /** - * Set the default format used when type juggling a Verta 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 - * - * @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( '-', parent::format( 'Y-m-d-w' ) ); - 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 .= static::isLeapYear( $pYear ) && ( $pMonth == 12 ) ? 30 : static::$daysMonthJalali[ intval( $pMonth ) - 1 ]; - break; - - # Years - case 'L': - $result .= intval( $this->isLeapYear( $pYear ) ); - break; - - case 'Y': - case 'o': - $result .= $pYear; - break; - - case 'y': - $result .= substr( $pYear, 2 ); - break; - - # Time - case 'a': - case 'A': - if (parent::format( 'a' ) == '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 .= parent::format( $par ); - break; - - # Full Date/Time - case 'c': - $result .= ( $pYear . '-' . $pMonth . '-' . $pDay . ' ' . parent::format( 'H:i:s P' ) ); - break; - - case 'r': - $result .= ( substr( static::$dayWeek[ $pWeek ], 0, 2 ) . '، ' . $pDay . ' ' . substr( static::$monthYear[ $pMonth ], 0, 6 ) . ' ' . $pYear . ' ' . parent::format( 'H:i:s P' ) ); - break; - - case 'U': - $result .= $timestamp; - break; - - default: - $result .= $par; - } - - $i++; - } - return $result; - } - - /** - * The format of the outputted date string (jalali equivalent of php date() function) - * - * @param string $format for example 'Y-m-d H:i:s' - * @return string - */ - protected function dateWord($format){ - - $timestamp = $this->getTimestamp(); - - list($gYear, $gMonth, $gDay, $gWeek) = explode('-', date('Y-m-d-w', $timestamp)); - list($pYear, $pMonth, $pDay) = static::getJalali($gYear, $gMonth, $gDay); - $pWeek = ($gWeek + 1); - - if ($pWeek >= 7) { - $pWeek = 0; - } - - if ($format == '\\') { - $format = '//'; - } - - $lenghFormat = strlen($format); - $i = 0; - $result = ''; - - $word = new Notowo(0, 'fa'); - - while ($i < $lenghFormat) { - $par = $format[$i]; - if ($par == '\\') { - $result .= $format[++$i]; - $i ++; - continue; - } - switch ($par) { - # Day - case 'd': - case 'j': - $result .= $word->getWord(strval($pDay)); - break; - - case 'D': - $result .= substr(static::$dayWeek[$pWeek], 0, 2); - break; - - case 'l': - $result .= static::$dayWeek[$pWeek]; - break; - - case 'N': - $result .= $word->getWord(strval($pWeek + 1)); - break; - - case 'w': - $result .= $word->getWord(strval($pWeek)); - break; - - case 'z': - $result .= $word->getWord(strval($this->daysYear($pMonth, $pDay))); - break; - - case 'S': - $result .= self::NUMBER_TH; - break; - - # Week - case 'W': - $result .= $word->getWord(strval(ceil($this->daysYear($pMonth, $pDay) / 7))); - break; - - # Month - case 'F': - $result .= static::$monthYear[$pMonth-1]; - break; - - case 'm': - case 'n': - $result .= $word->getWord(strval($pMonth)); - break; - - case 'M': - $result .= substr(static::$monthYear[$pMonth-1], 0, 6); - break; - - case 't': - $result .= $word->getWord(strval(static::isLeapYear($pYear) && ($pMonth == 12) ? 30 : static::$daysMonthJalali[intval($pMonth)-1])); - break; - - # Years - case 'L': - $result .= intval($this->isLeapYear($pYear)); - break; - - case 'Y': - case 'o': - $result .= $word->getWord(strval($pYear)); - break; - - case 'y': - $result .= $word->getWord(strval(substr($pYear, 2))); - break; - - # Time - case 'a': - case 'A': - if (date('a', $timestamp) == 'am') { - $result .= (($par == 'a') ? self::AM : self::ANTE_MERIDIEM); - } else { - $result .= (($par == 'a') ? self::PM : self::POST_MERIDIEM); - } - break; - - case 'B': - case 'g': - case 'G': - case 'h': - case 'H': - case 's': - case 'u': - case 'i': - $result .= $word->getWord(strval(date($par, $timestamp))); - break; - case 'e': - case 'I': - case 'O': - case 'P': - case 'T': - case 'Z': - $result .= date($par, $timestamp); - break; - - # Full Date/Time - case 'c': - $result .= $this->dateWord('Y, m, d, H:i:s P'); - break; - - case 'r': - $result .= $this->dateWord('l Y, m, d, H:i:s P'); - break; - - case 'U': - $result .= $word->getWord(strval($timestamp)); - break; - - default: - $result .= $par; - } - $i ++; - } - - return $result; - } - - /** - * return day number from first day of year - * - * @param int $month - * @param int $day - * @return type - * @since 5.0.0 - */ - protected function daysYear($month, $day) { - $days = 0; - for ($i = 1; $i < $month; $i ++) { - $days += static::$daysMonthJalali[$i-1]; - } - return ($days + $day); - } - - /** - * 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", - "%", - ); - - return str_replace($str_format_code, $date_format_code, $format); - } - - /** - * The format of the outputted date string (jalali equivalent of php strftime() function) - * - * @param $format - * @return string - */ - public function format($format) { - return $this->date($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 - * - * @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 formatJalaliDatetime() - { - return $this->format('Y/n/j H:i:s'); - } - - /** - * Format the instance as date - * - * @return string - */ - public function formatJalaliDate() - { - return $this->format('Y/n/j'); - } - - /** - * get difference in all - * - * @param Verta|null $v - * - * @return string - */ - public function formatDifference(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); - } - - public function formatWord($format) - { - return $this->dateWord($this->strftime($format)); - } - - /** - * Convert english numbers to persian numbers - * - * @param string $string - * @return string - */ - public static function enToFaNumbers($string) - { - return str_replace(self::$englishNumber, self::$persianNumber, $string); - } - - /** - * Convert english numbers to persian numbers - * - * @param string $string - * @return string - */ - public static function faToEnNumbers($string) - { - return str_replace(self::$persianNumber, self::$englishNumber, $string); - } - - /** - * Convert english numbers to persian numbers - * - * @param string $string - * @return string - */ - public static function arToEnNumbers($string) - { - return str_replace(self::$persianNumber, self::$englishNumber, $string); - } - - /***************************** COMPARISION ****************************/ - - /** - * check jalali the instance is a leap year - * - * @param int $year - * @return bool - */ - public static function isLeapYear($year) { - return in_array(($year % 33) , [1 , 5 , 9 , 13 ,17 , 22 , 26 , 30]); - } - - /** - * validate a jalali date (jalali equivalent of php checkdate() function) - * - * @param int $month - * @param int $day - * @param int $year - * @return bool - */ - public static function isValidDate($year, $month, $day) { - if($year < 0 || $year > 32766) { - return false; - } - if($month < 1 || $month > 12) { - return false; - } - $dayLastMonthJalali = static::isLeapYear($year) && ($month == 12) ? 30 : static::$daysMonthJalali[intval($month)-1]; - if($day < 1 || $day > $dayLastMonthJalali) { - return false; - } - return true; - } - - /** - * validate a time - * - * @param int $hour - * @param int $minute - * @param int $second - * @return bool - */ - public static function isValidTime($hour, $minute, $second) { - return $hour >= 0 && $hour <= 23 - && $minute >= 0 && $minute <= 59 - && $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->datetime())->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->datetime())->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->datetime())->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 - * - * @param Verta $v - * - * @return bool - */ - public function eq(Verta $v = null) - { - return $this == ($v ?: new static()); - } - - /** - * Determines if the instance is equal to another - * - * @param Verta $v - * - * @see eq() - * - * @return bool - */ - public function equalTo(Verta $v = null) - { - return $this->eq($v); - } - - /** - * Determines if the instance is not equal to another - * - * @param Verta $v - * - * @return bool - */ - public function ne(Verta $v = null) - { - return !$this->eq($v); - } - - /** - * Determines if the instance is not equal to another - * - * @param Verta $v - * - * @see ne() - * - * @return bool - */ - public function notEqualTo(Verta $v = null) - { - return $this->ne($v); - } - - /** - * Determines if the instance is greater (after) than another - * - * @param Verta $v - * - * @return bool - */ - public function gt(Verta $v = null) - { - return $this > ($v ?: new static()); - } - - /** - * Determines if the instance is greater (after) than another - * - * @param Verta $v - * - * @see gt() - * - * @return bool - */ - public function greaterThan(Verta $v = null) - { - return $this->gt($v); - } - - /** - * Determines if the instance is greater (after) than or equal to another - * - * @param Verta $v - * - * @return bool - */ - public function gte(Verta $v = null) - { - return $this >= ($v ?: new static()); - } - - /** - * Determines if the instance is greater (after) than or equal to another - * - * @param Verta $v - * - * @see gte() - * - * @return bool - */ - public function greaterThanOrEqualTo(Verta $v = null) - { - return $this->gte($v); - } - - /** - * Determines if the instance is less (before) than another - * - * @param Verta $v - * - * @return bool - */ - public function lt(Verta $v = null) - { - return $this < ($v ?: new static()); - } - - /** - * Determines if the instance is less (before) than another - * - * @param Verta $v - * - * @see lt() - * - * @return bool - */ - public function lessThan(Verta $v = null) - { - return $this->lt($v); - } - - /** - * Determines if the instance is less (before) or equal to another - * - * @param Verta $v - * - * @return bool - */ - public function lte(Verta $v = null) - { - return $this <= ($v ?: new static()); - } - - /** - * Determines if the instance is less (before) or equal to another - * - * @param Verta $v - * - * @see lte() - * - * @return bool - */ - public function lessThanOrEqualTo(Verta $v = null) - { - return $this->lte($v); - } - - /** - * Determines if the instance is between two others - * - * @param Verta $v1 - * @param Verta $v2 - * @param bool $equal Indicates if a > and < comparison should be used or <= or >= - * - * @return bool - */ - public function between(Verta $v1, Verta $v2, $equal = true) - { - if ($v1->gt($v2)) { - $temp = $v1; - $v1 = $v2; - $v2 = $temp; - } - - if ($equal) { - return $this->gte($v1) && $this->lte($v2); - } - - return $this->gt($v1) && $this->lt($v2); - } - - /** - * Get the closest date from the instance. - * - * @param Verta $v1 - * @param Verta $v2 - * - * @return static - */ - public function closest(Verta $v1, Verta $v2) - { - return $this->diffSeconds($v1) < $this->diffSeconds($v2) ? $v1 : $v2; - } - - /** - * Get the farthest date from the instance. - * - * @param Verta $v1 - * @param Verta $v2 - * - * @return static - */ - public function farthest(Verta $v1, Verta $v2) - { - return $this->diffSeconds($v1) > $this->diffSeconds($v2) ? $v1 : $v2; - } - - /** - * Get the minimum instance between a given instance (default now) and the current instance. - * - * @param Verta|null $v - * - * @return static - */ - public function min(Verta $v = null) - { - $v = $v ?: static::now($this->getTimezone()); - - return $this->lt($v) ? $this : $v; - } - - /** - * Get the minimum instance between a given instance (default now) and the current instance. - * - * @param Verta|null $v - * - * @see min() - * - * @return static - */ - public function minimum(Verta $v = null) - { - return $this->min($v); - } - - /** - * Get the maximum instance between a given instance (default now) and the current instance. - * - * @param Verta|null $v - * - * @return static - */ - public function max(Verta $v = null) - { - $v = $v ?: static::now($this->getTimezone()); - - return $this->gt($v) ? $this : $v; - } - - /** - * Get the maximum instance between a given instance (default now) and the current instance. - * - * @param Verta|null $v - * - * @see max() - * - * @return static - */ - public function maximum(Verta $v = null) - { - return $this->max($v); - } - - /** - * Determines if the instance is a weekday - * - * @return bool - */ - public function isWeekday() - { - return !$this->isWeekend(); - } - - /** - * Determines if the instance is a weekend day - * - * @return bool - */ - public function isWeekend() - { - return in_array($this->dayOfWeek, static::$weekendDays); - } - - /** - * Determines if the instance is yesterday - * - * @return bool - */ - public function isYesterday() - { - return $this->formatDate() === static::yesterday($this->getTimezone())->formatDate(); - } - - /** - * Determines if the instance is today - * - * @return bool - */ - public function isToday() - { - return $this->formatDate() === static::now($this->getTimezone())->formatDate(); - } - - /** - * Determines if the instance is tomorrow - * - * @return bool - */ - public function isTomorrow() - { - return $this->formatDate() === static::tomorrow($this->getTimezone())->formatDate(); - } - - /** - * Determines if the instance is within the next week - * - * @return bool - */ - public function isNextWeek() - { - return $this->weekOfYear === static::now($this->getTimezone())->addWeek()->weekOfYear; - } - - /** - * Determines if the instance is within the last week - * - * @return bool - */ - public function isLastWeek() - { - return $this->weekOfYear === static::now($this->getTimezone())->subWeek()->weekOfYear; - } - - /** - * Determines if the instance is within the next month - * - * @return bool - */ - public function isNextMonth() - { - return $this->month === static::now($this->getTimezone())->addMonth()->month; - } - - /** - * Determines if the instance is within the last month - * - * @return bool - */ - public function isLastMonth() - { - return $this->month === static::now($this->getTimezone())->subMonth()->month; - } - - /** - * Determines if the instance is within next year - * - * @return bool - */ - public function isNextYear() - { - return $this->year === static::now($this->getTimezone())->addYear()->year; - } - - /** - * Determines if the instance is within the previous year - * - * @return bool - */ - public function isLastYear() - { - return $this->year === static::now($this->getTimezone())->subYear()->year; - } - - /** - * Determines if the instance is in the future, ie. greater (after) than now - * - * @return bool - */ - public function isFuture() - { - return $this->gt(static::now($this->getTimezone())); - } - - /** - * Determines if the instance is in the past, ie. less (before) than now - * - * @return bool - */ - public function isPast() - { - return $this->lt(static::now($this->getTimezone())); - } - - /** - * Compares the formatted values of the two dates. - * - * @param string $format The date formats to compare. - * @param Verta|null $v The instance to compare with or null to use current day. - * - * @return bool - */ - public function isSameAs($format, Verta $v = null) - { - $v = $v ?: static::now($this->timezone); - - return $this->format($format) === $v->format($format); - } - - /** - * Determines if the instance is in the current year - * - * @return bool - */ - public function isCurrentYear() - { - return $this->isSameYear(); - } - - /** - * Checks if the passed in date is in the same year as the instance year. - * - * @param Verta|null $v The instance to compare with or null to use current day. - * - * @return bool - */ - public function isSameYear(Verta $v = null) - { - return $this->isSameAs('Y', $v); - } - - /** - * Determines if the instance is in the current month - * - * @return bool - */ - public function isCurrentMonth() - { - return $this->isSameMonth(); - } - - /** - * Checks if the passed in date is in the same month as the instance month (and year if needed). - * - * @param Verta|null $v The instance to compare with or null to use current day. - * @param bool $ofSameYear Check if it is the same month in the same year. - * - * @return bool - */ - public function isSameMonth(Verta $v = null, $ofSameYear = false) - { - $format = $ofSameYear ? 'Y-m' : 'm'; - - return $this->isSameAs($format, $v); - } - - /** - * Checks if the passed in date is the same day as the instance current day. - * - * @param Verta $v - * - * @return bool - */ - public function isSameDay(Verta $v) - { - return $this->formatDate() === $v->formatDate(); - } - - /** - * Check if its the birthday. Compares the date/month values of the two dates. - * - * @param Verta|null $dt The instance to compare with or null to use current day. - * - * @return bool - */ - public function isBirthday(Verta $v = null) - { - return $this->isSameAs('md', $v); - } - - /** - * Checks if this day is a Saturday. - * - * @return bool - */ - public function isSaturday() - { - return $this->dayOfWeek === static::SATURDAY; - } - - /** - * Checks if this day is a Sunday. - * - * @return bool - */ - public function isSunday() - { - return $this->dayOfWeek === static::SUNDAY; - } - - /** - * Checks if this day is a Monday. - * - * @return bool - */ - public function isMonday() - { - return $this->dayOfWeek === static::MONDAY; - } - - /** - * Checks if this day is a Tuesday. - * - * @return bool - */ - public function isTuesday() - { - return $this->dayOfWeek === static::TUESDAY; - } - - /** - * Checks if this day is a Wednesday. - * - * @return bool - */ - public function isWednesday() - { - return $this->dayOfWeek === static::WEDNESDAY; - } - - /** - * Checks if this day is a Thursday. - * - * @return bool - */ - public function isThursday() - { - return $this->dayOfWeek === static::THURSDAY; - } - - /** - * Checks if this day is a Friday. - * - * @return bool - */ - public function isFriday() - { - return $this->dayOfWeek === static::FRIDAY; - } - - /***************************** MODIFY ****************************/ - - /** - * 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->year($this->year + $value); - } - - /** - * 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); - } - - /** - * Remove a year from the instance - * - * @param int $value - * - * @return static - */ - public function subYear($value = 1) - { - 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; - } - elseif ($month < 1) { - $year += intval($month / 12) - 1; - $month = 12 + ($month % 12); - } - if($month == 0) { - $year--; - $month = 12; - } - if (($month > 6 && $month < 12 && $day == 31)) { - $day--; - } - else { - if ($month == 12 && ($day == 30 || $day == 31)) { - $day = self::isLeapYear($year) ? 30 : 29; - } - } - return self::createJalali($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. - * - * @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 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); - } - - /** - * Resets the time to 00:00:00 - * - * @return static - */ - public function startDay() - { - return $this->setTime(0, 0, 0); - } - - /** - * Resets the time to 23:59:59 - * - * @return static - */ - 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 ****************************/ - - /** - * 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); - } - - /** - * integer division - * - * @param int $a - * @param int $b - * @return int - */ - private static function div($a, $b) { - return ~~($a / $b); - } -} + use Date; +} \ No newline at end of file diff --git a/src/VertaInterface.php b/src/VertaInterface.php new file mode 100644 index 0000000..f5cff8c --- /dev/null +++ b/src/VertaInterface.php @@ -0,0 +1,50 @@ +assertEquals(35, $datetime->second); $this->assertEquals(0, $datetime->micro); $this->assertEquals(3, $datetime->dayOfWeek); - $this->assertEquals(286, $datetime->dayOfYear); + $this->assertEquals(287, $datetime->dayOfYear); $this->assertEquals(41, $datetime->weekOfYear); $this->assertEquals(30, $datetime->daysInMonth); $this->assertEquals(1546350635, $datetime->timestamp); @@ -58,7 +58,7 @@ class AccessTest extends TestCase public function testSetMethods() { - $datetime = verta(); + $datetime = Verta::parse('1399-05-12 12:36:32'); $datetime->year(1395); $this->assertEquals(1395, $datetime->year); diff --git a/tests/CreatorTest.php b/tests/CreatorTest.php index 9444831..0b3d8e4 100644 --- a/tests/CreatorTest.php +++ b/tests/CreatorTest.php @@ -77,7 +77,7 @@ class CreatorTest extends TestCase public function testParse() { - $datetime = Verta::parse('1397-10-11 10:20:11')->formatGregorian('Y-m-d H:i:s'); + $datetime = Verta::parse('1397 دی 11 10:20:11')->formatGregorian('Y-m-d H:i:s'); $this->assertEquals('2019-01-01 10:20:11', $datetime); }