add new modify function

This commit is contained in:
nasser committed 2017-07-20 21:56:09 +04:30
1 parent d10618745d
commit b6c2c52310
2 files changed
+326 -202

No files matched your search

+218 -123
View File
@@ -38,6 +38,7 @@ class Verta extends DateTime {
const YEARS_PER_CENTURY = 100;
const YEARS_PER_DECADE = 10;
const MONTHS_PER_YEAR = 12;
const MONTHS_PER_QUARTER = 3;
const WEEKS_PER_YEAR = 52;
const WEEKS_PER_MONTH = 4.35;
const DAYS_PER_WEEK = 7;
@@ -614,6 +615,9 @@ class Verta extends DateTime {
)):
return (int) $this->format($formats[$name]);
case $name === 'quarter':
return (int) ceil($this->month / static::MONTHS_PER_QUARTER);
case $name === 'timezone':
return $this->getTimezone()->getName();
@@ -1218,6 +1222,31 @@ class Verta extends DateTime {
return $this->format('Y/n/j');
}
/**
* get difference in all
*
* @param Verta|null $v
*
* @return string
*/
public function diffFormat(Verta $v = null)
{
$difference = $this->diffSeconds($v);
$absolute = $difference < 0 ? self::POST : self::PRE;
$difference = abs($difference);
for ($j = 0; $difference >= static::$unitNumber[$j] and $j < count(static::$unitNumber) - 1; $j++) {
$difference /= static::$unitNumber[$j];
}
$difference = intval(round($difference));
if($difference === 0) {
return self::NOW;
}
return sprintf('%s %s %s', $difference, static::$unitName[$j], $absolute);
}
/**
* Convert english numbers to persian numbers
*
@@ -1274,6 +1303,98 @@ class Verta extends DateTime {
&& $second >= 0 && $second <= 59;
}
/**
* Get the difference in years
*
* @param Verta|null $v
*
* @return int
*/
public function diffYears(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return (int) $this->diff($v)->format('%r%y');
}
/**
* Get the difference in months
*
* @param Verta|null $v
*
* @return int
*/
public function diffMonths(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return $this->diffYears($v) * static::MONTHS_PER_YEAR + (int) $this->diff($v)->format('%r%m');
}
/**
* Get the difference in weeks
*
* @param Verta|null $v
*
* @return int
*/
public function diffWeeks(Verta $v = null)
{
return (int) ($this->diffDays($v) / static::DAYS_PER_WEEK);
}
/**
* Get the difference in days
*
* @param Verta|null $v
*
* @return int
*/
public function diffDays(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return (int) $this->diff($v)->format('%r%a');
}
/**
* Get the difference in hours
*
* @param Verta|null $v
*
* @return int
*/
public function diffHours(Verta $v = null)
{
return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR);
}
/**
* Get the difference in minutes
*
* @param Verta|null $v
*
* @return int
*/
public function diffMinutes(Verta $v = null)
{
return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE);
}
/**
* Get the difference in seconds
*
* @param Verta|null $v
*
* @return int
*/
public function diffSeconds(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return $v->getTimestamp() - $this->getTimestamp();
}
/**
* Determines if the instance is equal to another
*
@@ -1822,7 +1943,7 @@ class Verta extends DateTime {
return $this->dayOfWeek === static::FRIDAY;
}
/***************************** CALCULATION ****************************/
/***************************** MODIFY ****************************/
/**
* Add years to the instance. Positive $value travel forward while
@@ -2195,132 +2316,12 @@ class Verta extends DateTime {
return $this->addSeconds(-1 * $value);
}
/**
* Get the difference in years
*
* @param Verta|null $v
*
* @return int
*/
public function diffYears(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return (int) $this->diff($v)->format('%r%y');
}
/**
* Get the difference in months
*
* @param Verta|null $v
*
* @return int
*/
public function diffMonths(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return $this->diffYears($v) * static::MONTHS_PER_YEAR + (int) $this->diff($v)->format('%r%m');
}
/**
* Get the difference in weeks
*
* @param Verta|null $v
*
* @return int
*/
public function diffWeeks(Verta $v = null)
{
return (int) ($this->diffDays($v) / static::DAYS_PER_WEEK);
}
/**
* Get the difference in days
*
* @param Verta|null $v
*
* @return int
*/
public function diffDays(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return (int) $this->diff($v)->format('%r%a');
}
/**
* Get the difference in hours
*
* @param Verta|null $v
*
* @return int
*/
public function diffHours(Verta $v = null)
{
return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR);
}
/**
* Get the difference in minutes
*
* @param Verta|null $v
*
* @return int
*/
public function diffMinutes(Verta $v = null)
{
return (int) ($this->diffSeconds($v) / static::SECONDS_PER_MINUTE);
}
/**
* Get the difference in seconds
*
* @param Verta|null $v
*
* @return int
*/
public function diffSeconds(Verta $v = null)
{
$v = $v ?: static::now($this->getTimezone());
return $v->getTimestamp() - $this->getTimestamp();
}
/**
* get difference in all
*
* @param Verta|null $v
*
* @return string
*/
public function diffFormat(Verta $v = null)
{
$difference = $this->diffSeconds($v);
$absolute = $difference < 0 ? self::POST : self::PRE;
$difference = abs($difference);
for ($j = 0; $difference >= static::$unitNumber[$j] and $j < count(static::$unitNumber) - 1; $j++) {
$difference /= static::$unitNumber[$j];
}
$difference = intval(round($difference));
if($difference === 0) {
return self::NOW;
}
return sprintf('%s %s %s', $difference, static::$unitName[$j], $absolute);
}
/***************************** MODIFY ****************************/
/**
* Resets the time to 00:00:00
*
* @return static
*/
public function startOfDay()
public function startDay()
{
return $this->setTime(0, 0, 0);
}
@@ -2330,11 +2331,103 @@ class Verta extends DateTime {
*
* @return static
*/
public function endOfDay()
public function endDay()
{
return $this->setTime(23, 59, 59);
}
/**
* Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
*
* @return static
*/
public function startWeek()
{
while ($this->dayOfWeek !== static::$weekStartsAt) {
$this->subDay();
}
return $this->startDay();
}
/**
* Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
*
* @return static
*/
public function endWeek()
{
while ($this->dayOfWeek !== static::$weekEndsAt) {
$this->addDay();
}
return $this->endDay();
}
/**
* Resets the date to the first day of the month and the time to 00:00:00
*
* @return static
*/
public function startMonth()
{
return $this->setDateTime($this->year, $this->month, 1, 0, 0, 0);
}
/**
* Resets the date to end of the month and time to 23:59:59
*
* @return static
*/
public function endMonth()
{
return $this->setDateTime($this->year, $this->month, $this->daysInMonth, 23, 59, 59);
}
/**
* Resets the date to the first day of the quarter and the time to 00:00:00
*
* @return static
*/
public function startQuarter()
{
$month = ($this->quarter - 1) * static::MONTHS_PER_QUARTER + 1;
return $this->setDateTime($this->year, $month, 1, 0, 0, 0);
}
/**
* Resets the date to end of the quarter and time to 23:59:59
*
* @return static
*/
public function endQuarter()
{
return $this->startQuarter()->addMonths(static::MONTHS_PER_QUARTER - 1)->endMonth();
}
/**
* Resets the date to the first day of the year and the time to 00:00:00
*
* @return static
*/
public function startYear()
{
return $this->setDateTime($this->year, 1, 1, 0, 0, 0);
}
/**
* Resets the date to end of the year and time to 23:59:59
*
* @return static
*/
public function endYear()
{
$day = $this->format('L') ? 30 : 29;
return $this->setDateTime($this->year, 12, $day, 23, 59, 59);
}
/***************************** TRANSFORMATION ****************************/
/**
@@ -2370,6 +2463,7 @@ class Verta extends DateTime {
}
return array($jy, $i + 1, $j_day_no + 1);
}
/**
* jalali to gregorian convertion
*
@@ -2413,6 +2507,7 @@ class Verta extends DateTime {
}
return array($gy, $i + 1, $g_day_no + 1);
}
/**
* integer division
*