vendor/nesbot/carbon/src/Carbon/Carbon.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Carbon package.
  4.  *
  5.  * (c) Brian Nesbitt <brian@nesbot.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Carbon;
  11. use Carbon\Exceptions\InvalidDateException;
  12. use Closure;
  13. use DateInterval;
  14. use DatePeriod;
  15. use DateTime;
  16. use DateTimeInterface;
  17. use DateTimeZone;
  18. use InvalidArgumentException;
  19. use JsonSerializable;
  20. use Symfony\Component\Translation\TranslatorInterface;
  21. /**
  22.  * A simple API extension for DateTime
  23.  *
  24.  * @property      int $year
  25.  * @property      int $yearIso
  26.  * @property      int $month
  27.  * @property      int $day
  28.  * @property      int $hour
  29.  * @property      int $minute
  30.  * @property      int $second
  31.  * @property      int $timestamp seconds since the Unix Epoch
  32.  * @property      \DateTimeZone $timezone the current timezone
  33.  * @property      \DateTimeZone $tz alias of timezone
  34.  * @property-read int $micro
  35.  * @property-read int $dayOfWeek 0 (for Sunday) through 6 (for Saturday)
  36.  * @property-read int $dayOfWeekIso 1 (for Monday) through 7 (for Sunday)
  37.  * @property-read int $dayOfYear 0 through 365
  38.  * @property-read int $weekOfMonth 1 through 5
  39.  * @property-read int $weekNumberInMonth 1 through 5
  40.  * @property-read int $weekOfYear ISO-8601 week number of year, weeks starting on Monday
  41.  * @property-read int $daysInMonth number of days in the given month
  42.  * @property-read int $age does a diffInYears() with default parameters
  43.  * @property-read int $quarter the quarter of this instance, 1 - 4
  44.  * @property-read int $offset the timezone offset in seconds from UTC
  45.  * @property-read int $offsetHours the timezone offset in hours from UTC
  46.  * @property-read bool $dst daylight savings time indicator, true if DST, false otherwise
  47.  * @property-read bool $local checks if the timezone is local, true if local, false otherwise
  48.  * @property-read bool $utc checks if the timezone is UTC, true if UTC, false otherwise
  49.  * @property-read string $timezoneName
  50.  * @property-read string $tzName
  51.  * @property-read string $englishDayOfWeek the day of week in English
  52.  * @property-read string $shortEnglishDayOfWeek the abbreviated day of week in English
  53.  * @property-read string $englishMonth the day of week in English
  54.  * @property-read string $shortEnglishMonth the abbreviated day of week in English
  55.  * @property-read string $localeDayOfWeek the day of week in current locale LC_TIME
  56.  * @property-read string $shortLocaleDayOfWeek the abbreviated day of week in current locale LC_TIME
  57.  * @property-read string $localeMonth the month in current locale LC_TIME
  58.  * @property-read string $shortLocaleMonth the abbreviated month in current locale LC_TIME
  59.  */
  60. class Carbon extends DateTime implements JsonSerializable
  61. {
  62.     const NO_ZERO_DIFF 01;
  63.     const JUST_NOW 02;
  64.     const ONE_DAY_WORDS 04;
  65.     const TWO_DAY_WORDS 010;
  66.     // Substitutes for Carbon 2 modes
  67.     const DIFF_RELATIVE_TO_NOW 'relative-to-now';
  68.     const DIFF_RELATIVE_TO_OTHER 'relative-to-other';
  69.     /**
  70.      * The day constants.
  71.      */
  72.     const SUNDAY 0;
  73.     const MONDAY 1;
  74.     const TUESDAY 2;
  75.     const WEDNESDAY 3;
  76.     const THURSDAY 4;
  77.     const FRIDAY 5;
  78.     const SATURDAY 6;
  79.     /**
  80.      * Names of days of the week.
  81.      *
  82.      * @var array
  83.      */
  84.     protected static $days = array(
  85.         self::SUNDAY => 'Sunday',
  86.         self::MONDAY => 'Monday',
  87.         self::TUESDAY => 'Tuesday',
  88.         self::WEDNESDAY => 'Wednesday',
  89.         self::THURSDAY => 'Thursday',
  90.         self::FRIDAY => 'Friday',
  91.         self::SATURDAY => 'Saturday',
  92.     );
  93.     /**
  94.      * Number of X in Y.
  95.      */
  96.     const YEARS_PER_MILLENNIUM 1000;
  97.     const YEARS_PER_CENTURY 100;
  98.     const YEARS_PER_DECADE 10;
  99.     const MONTHS_PER_YEAR 12;
  100.     const MONTHS_PER_QUARTER 3;
  101.     const WEEKS_PER_YEAR 52;
  102.     const WEEKS_PER_MONTH 4;
  103.     const DAYS_PER_WEEK 7;
  104.     const HOURS_PER_DAY 24;
  105.     const MINUTES_PER_HOUR 60;
  106.     const SECONDS_PER_MINUTE 60;
  107.     const MICROSECONDS_PER_MILLISECOND 1000;
  108.     const MICROSECONDS_PER_SECOND 1000000;
  109.     /**
  110.      * RFC7231 DateTime format.
  111.      *
  112.      * @var string
  113.      */
  114.     const RFC7231_FORMAT 'D, d M Y H:i:s \G\M\T';
  115.     /**
  116.      * Default format to use for __toString method when type juggling occurs.
  117.      *
  118.      * @var string
  119.      */
  120.     const DEFAULT_TO_STRING_FORMAT 'Y-m-d H:i:s';
  121.     /**
  122.      * Format for converting mocked time, includes microseconds.
  123.      *
  124.      * @var string
  125.      */
  126.     const MOCK_DATETIME_FORMAT 'Y-m-d H:i:s.u';
  127.     /**
  128.      * Customizable PHP_INT_SIZE override.
  129.      *
  130.      * @var int
  131.      */
  132.     public static $PHPIntSize PHP_INT_SIZE;
  133.     /**
  134.      * Format to use for __toString method when type juggling occurs.
  135.      *
  136.      * @var string
  137.      */
  138.     protected static $toStringFormat self::DEFAULT_TO_STRING_FORMAT;
  139.     /**
  140.      * First day of week.
  141.      *
  142.      * @var int
  143.      */
  144.     protected static $weekStartsAt self::MONDAY;
  145.     /**
  146.      * Last day of week.
  147.      *
  148.      * @var int
  149.      */
  150.     protected static $weekEndsAt self::SUNDAY;
  151.     /**
  152.      * Days of weekend.
  153.      *
  154.      * @var array
  155.      */
  156.     protected static $weekendDays = array(
  157.         self::SATURDAY,
  158.         self::SUNDAY,
  159.     );
  160.     /**
  161.      * Midday/noon hour.
  162.      *
  163.      * @var int
  164.      */
  165.     protected static $midDayAt 12;
  166.     /**
  167.      * Format regex patterns.
  168.      *
  169.      * @var array
  170.      */
  171.     protected static $regexFormats = array(
  172.         'd' => '(3[01]|[12][0-9]|0[1-9])',
  173.         'D' => '([a-zA-Z]{3})',
  174.         'j' => '([123][0-9]|[1-9])',
  175.         'l' => '([a-zA-Z]{2,})',
  176.         'N' => '([1-7])',
  177.         'S' => '([a-zA-Z]{2})',
  178.         'w' => '([0-6])',
  179.         'z' => '(36[0-5]|3[0-5][0-9]|[12][0-9]{2}|[1-9]?[0-9])',
  180.         'W' => '(5[012]|[1-4][0-9]|[1-9])',
  181.         'F' => '([a-zA-Z]{2,})',
  182.         'm' => '(1[012]|0[1-9])',
  183.         'M' => '([a-zA-Z]{3})',
  184.         'n' => '(1[012]|[1-9])',
  185.         't' => '(2[89]|3[01])',
  186.         'L' => '(0|1)',
  187.         'o' => '([1-9][0-9]{0,4})',
  188.         'Y' => '([1-9]?[0-9]{4})',
  189.         'y' => '([0-9]{2})',
  190.         'a' => '(am|pm)',
  191.         'A' => '(AM|PM)',
  192.         'B' => '([0-9]{3})',
  193.         'g' => '(1[012]|[1-9])',
  194.         'G' => '(2[0-3]|1?[0-9])',
  195.         'h' => '(1[012]|0[1-9])',
  196.         'H' => '(2[0-3]|[01][0-9])',
  197.         'i' => '([0-5][0-9])',
  198.         's' => '([0-5][0-9])',
  199.         'u' => '([0-9]{1,6})',
  200.         'v' => '([0-9]{1,3})',
  201.         'e' => '([a-zA-Z]{1,5})|([a-zA-Z]*\/[a-zA-Z]*)',
  202.         'I' => '(0|1)',
  203.         'O' => '([\+\-](1[012]|0[0-9])[0134][05])',
  204.         'P' => '([\+\-](1[012]|0[0-9]):[0134][05])',
  205.         'T' => '([a-zA-Z]{1,5})',
  206.         'Z' => '(-?[1-5]?[0-9]{1,4})',
  207.         'U' => '([0-9]*)',
  208.         // The formats below are combinations of the above formats.
  209.         'c' => '(([1-9]?[0-9]{4})\-(1[012]|0[1-9])\-(3[01]|[12][0-9]|0[1-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])[\+\-](1[012]|0[0-9]):([0134][05]))'// Y-m-dTH:i:sP
  210.         'r' => '(([a-zA-Z]{3}), ([123][0-9]|[1-9]) ([a-zA-Z]{3}) ([1-9]?[0-9]{4}) (2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9]) [\+\-](1[012]|0[0-9])([0134][05]))'// D, j M Y H:i:s O
  211.     );
  212.     /**
  213.      * A test Carbon instance to be returned when now instances are created.
  214.      *
  215.      * @var \Carbon\Carbon
  216.      */
  217.     protected static $testNow;
  218.     /**
  219.      * A translator to ... er ... translate stuff.
  220.      *
  221.      * @var \Symfony\Component\Translation\TranslatorInterface
  222.      */
  223.     protected static $translator;
  224.     /**
  225.      * The errors that can occur.
  226.      *
  227.      * @var array
  228.      */
  229.     protected static $lastErrors;
  230.     /**
  231.      * The custom Carbon JSON serializer.
  232.      *
  233.      * @var callable|null
  234.      */
  235.     protected static $serializer;
  236.     /**
  237.      * The registered string macros.
  238.      *
  239.      * @var array
  240.      */
  241.     protected static $localMacros = array();
  242.     /**
  243.      * Will UTF8 encoding be used to print localized date/time ?
  244.      *
  245.      * @var bool
  246.      */
  247.     protected static $utf8 false;
  248.     /**
  249.      * Add microseconds to now on PHP < 7.1 and 7.1.3. true by default.
  250.      *
  251.      * @var bool
  252.      */
  253.     protected static $microsecondsFallback true;
  254.     /**
  255.      * Indicates if months should be calculated with overflow.
  256.      *
  257.      * @var bool
  258.      */
  259.     protected static $monthsOverflow true;
  260.     /**
  261.      * Indicates if years should be calculated with overflow.
  262.      *
  263.      * @var bool
  264.      */
  265.     protected static $yearsOverflow true;
  266.     /**
  267.      * Indicates if years are compared with month by default so isSameMonth and isSameQuarter have $ofSameYear set
  268.      * to true by default.
  269.      *
  270.      * @var bool
  271.      */
  272.     protected static $compareYearWithMonth false;
  273.     /**
  274.      * Options for diffForHumans().
  275.      *
  276.      * @var int
  277.      */
  278.     protected static $humanDiffOptions self::NO_ZERO_DIFF;
  279.     /**
  280.      * @param int $humanDiffOptions
  281.      */
  282.     public static function setHumanDiffOptions($humanDiffOptions)
  283.     {
  284.         static::$humanDiffOptions $humanDiffOptions;
  285.     }
  286.     /**
  287.      * @param int $humanDiffOption
  288.      */
  289.     public static function enableHumanDiffOption($humanDiffOption)
  290.     {
  291.         static::$humanDiffOptions = static::getHumanDiffOptions() | $humanDiffOption;
  292.     }
  293.     /**
  294.      * @param int $humanDiffOption
  295.      */
  296.     public static function disableHumanDiffOption($humanDiffOption)
  297.     {
  298.         static::$humanDiffOptions = static::getHumanDiffOptions() & ~$humanDiffOption;
  299.     }
  300.     /**
  301.      * @return int
  302.      */
  303.     public static function getHumanDiffOptions()
  304.     {
  305.         return static::$humanDiffOptions;
  306.     }
  307.     /**
  308.      * Add microseconds to now on PHP < 7.1 and 7.1.3 if set to true,
  309.      * let microseconds to 0 on those PHP versions if false.
  310.      *
  311.      * @param bool $microsecondsFallback
  312.      */
  313.     public static function useMicrosecondsFallback($microsecondsFallback true)
  314.     {
  315.         static::$microsecondsFallback $microsecondsFallback;
  316.     }
  317.     /**
  318.      * Return true if microseconds fallback on PHP < 7.1 and 7.1.3 is
  319.      * enabled. false if disabled.
  320.      *
  321.      * @return bool
  322.      */
  323.     public static function isMicrosecondsFallbackEnabled()
  324.     {
  325.         return static::$microsecondsFallback;
  326.     }
  327.     /**
  328.      * Indicates if months should be calculated with overflow.
  329.      *
  330.      * @param bool $monthsOverflow
  331.      *
  332.      * @return void
  333.      */
  334.     public static function useMonthsOverflow($monthsOverflow true)
  335.     {
  336.         static::$monthsOverflow $monthsOverflow;
  337.     }
  338.     /**
  339.      * Reset the month overflow behavior.
  340.      *
  341.      * @return void
  342.      */
  343.     public static function resetMonthsOverflow()
  344.     {
  345.         static::$monthsOverflow true;
  346.     }
  347.     /**
  348.      * Get the month overflow behavior.
  349.      *
  350.      * @return bool
  351.      */
  352.     public static function shouldOverflowMonths()
  353.     {
  354.         return static::$monthsOverflow;
  355.     }
  356.     /**
  357.      * Indicates if years should be calculated with overflow.
  358.      *
  359.      * @param bool $yearsOverflow
  360.      *
  361.      * @return void
  362.      */
  363.     public static function useYearsOverflow($yearsOverflow true)
  364.     {
  365.         static::$yearsOverflow $yearsOverflow;
  366.     }
  367.     /**
  368.      * Reset the month overflow behavior.
  369.      *
  370.      * @return void
  371.      */
  372.     public static function resetYearsOverflow()
  373.     {
  374.         static::$yearsOverflow true;
  375.     }
  376.     /**
  377.      * Get the month overflow behavior.
  378.      *
  379.      * @return bool
  380.      */
  381.     public static function shouldOverflowYears()
  382.     {
  383.         return static::$yearsOverflow;
  384.     }
  385.     /**
  386.      * Get the month comparison default behavior.
  387.      *
  388.      * @return bool
  389.      */
  390.     public static function compareYearWithMonth($compareYearWithMonth true)
  391.     {
  392.         static::$compareYearWithMonth $compareYearWithMonth;
  393.     }
  394.     /**
  395.      * Get the month comparison default behavior.
  396.      *
  397.      * @return bool
  398.      */
  399.     public static function shouldCompareYearWithMonth()
  400.     {
  401.         return static::$compareYearWithMonth;
  402.     }
  403.     /**
  404.      * Creates a DateTimeZone from a string, DateTimeZone or integer offset.
  405.      *
  406.      * @param \DateTimeZone|string|int|null $object
  407.      *
  408.      * @throws \InvalidArgumentException
  409.      *
  410.      * @return \DateTimeZone
  411.      */
  412.     protected static function safeCreateDateTimeZone($object)
  413.     {
  414.         if ($object === null) {
  415.             // Don't return null... avoid Bug #52063 in PHP <5.3.6
  416.             return new DateTimeZone(date_default_timezone_get());
  417.         }
  418.         if ($object instanceof DateTimeZone) {
  419.             return $object;
  420.         }
  421.         if (is_numeric($object)) {
  422.             $tzName timezone_name_from_abbr(null$object 3600true);
  423.             if ($tzName === false) {
  424.                 throw new InvalidArgumentException('Unknown or bad timezone ('.$object.')');
  425.             }
  426.             $object $tzName;
  427.         }
  428.         $tz = @timezone_open($object = (string) $object);
  429.         if ($tz !== false) {
  430.             return $tz;
  431.         }
  432.         // Work-around for a bug fixed in PHP 5.5.10 https://bugs.php.net/bug.php?id=45528
  433.         // See: https://stackoverflow.com/q/14068594/2646927
  434.         // @codeCoverageIgnoreStart
  435.         if (strpos($object':') !== false) {
  436.             try {
  437.                 return static::createFromFormat('O'$object)->getTimezone();
  438.             } catch (InvalidArgumentException $e) {
  439.                 //
  440.             }
  441.         }
  442.         // @codeCoverageIgnoreEnd
  443.         throw new InvalidArgumentException('Unknown or bad timezone ('.$object.')');
  444.     }
  445.     ///////////////////////////////////////////////////////////////////
  446.     //////////////////////////// CONSTRUCTORS /////////////////////////
  447.     ///////////////////////////////////////////////////////////////////
  448.     /**
  449.      * Create a new Carbon instance.
  450.      *
  451.      * Please see the testing aids section (specifically static::setTestNow())
  452.      * for more on the possibility of this constructor returning a test instance.
  453.      *
  454.      * @param string|null               $time
  455.      * @param \DateTimeZone|string|null $tz
  456.      */
  457.     public function __construct($time null$tz null)
  458.     {
  459.         // If the class has a test now set and we are trying to create a now()
  460.         // instance then override as required
  461.         $isNow = empty($time) || $time === 'now';
  462.         if (static::hasTestNow() && ($isNow || static::hasRelativeKeywords($time))) {
  463.             $testInstance = clone static::getTestNow();
  464.             //shift the time according to the given time zone
  465.             if ($tz !== null && $tz !== static::getTestNow()->getTimezone()) {
  466.                 $testInstance->setTimezone($tz);
  467.             } else {
  468.                 $tz $testInstance->getTimezone();
  469.             }
  470.             if (static::hasRelativeKeywords($time)) {
  471.                 $testInstance->modify($time);
  472.             }
  473.             $time $testInstance->format(static::MOCK_DATETIME_FORMAT);
  474.         }
  475.         $timezone = static::safeCreateDateTimeZone($tz);
  476.         // @codeCoverageIgnoreStart
  477.         if ($isNow && !isset($testInstance) && static::isMicrosecondsFallbackEnabled() && (
  478.                 version_compare(PHP_VERSION'7.1.0-dev''<')
  479.                 ||
  480.                 version_compare(PHP_VERSION'7.1.3-dev''>=') && version_compare(PHP_VERSION'7.1.4-dev''<')
  481.             )
  482.         ) {
  483.             // Get microseconds from microtime() if "now" asked and PHP < 7.1 and PHP 7.1.3 if fallback enabled.
  484.             list($microTime$timeStamp) = explode(' 'microtime());
  485.             $dateTime = new DateTime('now'$timezone);
  486.             $dateTime->setTimestamp($timeStamp); // Use the timestamp returned by microtime as now can happen in the next second
  487.             $time $dateTime->format(static::DEFAULT_TO_STRING_FORMAT).substr($microTime17);
  488.         }
  489.         // @codeCoverageIgnoreEnd
  490.         // Work-around for PHP bug https://bugs.php.net/bug.php?id=67127
  491.         if (strpos((string) .1'.') === false) {
  492.             $locale setlocale(LC_NUMERIC'0');
  493.             setlocale(LC_NUMERIC'C');
  494.         }
  495.         parent::__construct($time$timezone);
  496.         if (isset($locale)) {
  497.             setlocale(LC_NUMERIC$locale);
  498.         }
  499.         static::setLastErrors(parent::getLastErrors());
  500.     }
  501.     /**
  502.      * Create a Carbon instance from a DateTime one.
  503.      *
  504.      * @param \DateTime|\DateTimeInterface $date
  505.      *
  506.      * @return static
  507.      */
  508.     public static function instance($date)
  509.     {
  510.         if ($date instanceof static) {
  511.             return clone $date;
  512.         }
  513.         static::expectDateTime($date);
  514.         return new static($date->format('Y-m-d H:i:s.u'), $date->getTimezone());
  515.     }
  516.     /**
  517.      * Create a carbon instance from a string.
  518.      *
  519.      * This is an alias for the constructor that allows better fluent syntax
  520.      * as it allows you to do Carbon::parse('Monday next week')->fn() rather
  521.      * than (new Carbon('Monday next week'))->fn().
  522.      *
  523.      * @param string|null               $time
  524.      * @param \DateTimeZone|string|null $tz
  525.      *
  526.      * @return static
  527.      */
  528.     public static function parse($time null$tz null)
  529.     {
  530.         return new static($time$tz);
  531.     }
  532.     /**
  533.      * Get a Carbon instance for the current date and time.
  534.      *
  535.      * @param \DateTimeZone|string|null $tz
  536.      *
  537.      * @return static
  538.      */
  539.     public static function now($tz null)
  540.     {
  541.         return new static(null$tz);
  542.     }
  543.     /**
  544.      * Create a Carbon instance for today.
  545.      *
  546.      * @param \DateTimeZone|string|null $tz
  547.      *
  548.      * @return static
  549.      */
  550.     public static function today($tz null)
  551.     {
  552.         return static::parse('today'$tz);
  553.     }
  554.     /**
  555.      * Create a Carbon instance for tomorrow.
  556.      *
  557.      * @param \DateTimeZone|string|null $tz
  558.      *
  559.      * @return static
  560.      */
  561.     public static function tomorrow($tz null)
  562.     {
  563.         return static::parse('tomorrow'$tz);
  564.     }
  565.     /**
  566.      * Create a Carbon instance for yesterday.
  567.      *
  568.      * @param \DateTimeZone|string|null $tz
  569.      *
  570.      * @return static
  571.      */
  572.     public static function yesterday($tz null)
  573.     {
  574.         return static::parse('yesterday'$tz);
  575.     }
  576.     /**
  577.      * Create a Carbon instance for the greatest supported date.
  578.      *
  579.      * @return static
  580.      */
  581.     public static function maxValue()
  582.     {
  583.         if (self::$PHPIntSize === 4) {
  584.             // 32 bit
  585.             return static::createFromTimestamp(PHP_INT_MAX); // @codeCoverageIgnore
  586.         }
  587.         // 64 bit
  588.         return static::create(99991231235959);
  589.     }
  590.     /**
  591.      * Create a Carbon instance for the lowest supported date.
  592.      *
  593.      * @return static
  594.      */
  595.     public static function minValue()
  596.     {
  597.         if (self::$PHPIntSize === 4) {
  598.             // 32 bit
  599.             return static::createFromTimestamp(~PHP_INT_MAX); // @codeCoverageIgnore
  600.         }
  601.         // 64 bit
  602.         return static::create(111000);
  603.     }
  604.     /**
  605.      * Create a new Carbon instance from a specific date and time.
  606.      *
  607.      * If any of $year, $month or $day are set to null their now() values will
  608.      * be used.
  609.      *
  610.      * If $hour is null it will be set to its now() value and the default
  611.      * values for $minute and $second will be their now() values.
  612.      *
  613.      * If $hour is not null then the default values for $minute and $second
  614.      * will be 0.
  615.      *
  616.      * @param int|null                  $year
  617.      * @param int|null                  $month
  618.      * @param int|null                  $day
  619.      * @param int|null                  $hour
  620.      * @param int|null                  $minute
  621.      * @param int|null                  $second
  622.      * @param \DateTimeZone|string|null $tz
  623.      *
  624.      * @throws \InvalidArgumentException
  625.      *
  626.      * @return static
  627.      */
  628.     public static function create($year null$month null$day null$hour null$minute null$second null$tz null)
  629.     {
  630.         $now = static::hasTestNow() ? static::getTestNow() : static::now($tz);
  631.         $defaults array_combine(array(
  632.             'year',
  633.             'month',
  634.             'day',
  635.             'hour',
  636.             'minute',
  637.             'second',
  638.         ), explode('-'$now->format('Y-n-j-G-i-s')));
  639.         $year $year === null $defaults['year'] : $year;
  640.         $month $month === null $defaults['month'] : $month;
  641.         $day $day === null $defaults['day'] : $day;
  642.         if ($hour === null) {
  643.             $hour $defaults['hour'];
  644.             $minute $minute === null $defaults['minute'] : $minute;
  645.             $second $second === null $defaults['second'] : $second;
  646.         } else {
  647.             $minute $minute === null $minute;
  648.             $second $second === null $second;
  649.         }
  650.         $fixYear null;
  651.         if ($year 0) {
  652.             $fixYear $year;
  653.             $year 0;
  654.         } elseif ($year 9999) {
  655.             $fixYear $year 9999;
  656.             $year 9999;
  657.         }
  658.         $instance = static::createFromFormat('!Y-n-j G:i:s'sprintf('%s-%s-%s %s:%02s:%02s'$year$month$day$hour$minute$second), $tz);
  659.         if ($fixYear !== null) {
  660.             $instance->addYears($fixYear);
  661.         }
  662.         return $instance;
  663.     }
  664.     /**
  665.      * Create a new safe Carbon instance from a specific date and time.
  666.      *
  667.      * If any of $year, $month or $day are set to null their now() values will
  668.      * be used.
  669.      *
  670.      * If $hour is null it will be set to its now() value and the default
  671.      * values for $minute and $second will be their now() values.
  672.      *
  673.      * If $hour is not null then the default values for $minute and $second
  674.      * will be 0.
  675.      *
  676.      * If one of the set values is not valid, an \InvalidArgumentException
  677.      * will be thrown.
  678.      *
  679.      * @param int|null                  $year
  680.      * @param int|null                  $month
  681.      * @param int|null                  $day
  682.      * @param int|null                  $hour
  683.      * @param int|null                  $minute
  684.      * @param int|null                  $second
  685.      * @param \DateTimeZone|string|null $tz
  686.      *
  687.      * @throws \Carbon\Exceptions\InvalidDateException|\InvalidArgumentException
  688.      *
  689.      * @return static
  690.      */
  691.     public static function createSafe($year null$month null$day null$hour null$minute null$second null$tz null)
  692.     {
  693.         $fields = array(
  694.             'year' => array(09999),
  695.             'month' => array(012),
  696.             'day' => array(031),
  697.             'hour' => array(024),
  698.             'minute' => array(059),
  699.             'second' => array(059),
  700.         );
  701.         foreach ($fields as $field => $range) {
  702.             if ($$field !== null && (!is_int($$field) || $$field $range[0] || $$field $range[1])) {
  703.                 throw new InvalidDateException($field, $$field);
  704.             }
  705.         }
  706.         $instance = static::create($year$month$day$hour$minute$second$tz);
  707.         foreach (array_reverse($fields) as $field => $range) {
  708.             if ($$field !== null && (!is_int($$field) || $$field !== $instance->$field)) {
  709.                 throw new InvalidDateException($field, $$field);
  710.             }
  711.         }
  712.         return $instance;
  713.     }
  714.     /**
  715.      * Create a Carbon instance from just a date. The time portion is set to now.
  716.      *
  717.      * @param int|null                  $year
  718.      * @param int|null                  $month
  719.      * @param int|null                  $day
  720.      * @param \DateTimeZone|string|null $tz
  721.      *
  722.      * @throws \InvalidArgumentException
  723.      *
  724.      * @return static
  725.      */
  726.     public static function createFromDate($year null$month null$day null$tz null)
  727.     {
  728.         return static::create($year$month$daynullnullnull$tz);
  729.     }
  730.     /**
  731.      * Create a Carbon instance from just a date. The time portion is set to midnight.
  732.      *
  733.      * @param int|null                  $year
  734.      * @param int|null                  $month
  735.      * @param int|null                  $day
  736.      * @param \DateTimeZone|string|null $tz
  737.      *
  738.      * @return static
  739.      */
  740.     public static function createMidnightDate($year null$month null$day null$tz null)
  741.     {
  742.         return static::create($year$month$day000$tz);
  743.     }
  744.     /**
  745.      * Create a Carbon instance from just a time. The date portion is set to today.
  746.      *
  747.      * @param int|null                  $hour
  748.      * @param int|null                  $minute
  749.      * @param int|null                  $second
  750.      * @param \DateTimeZone|string|null $tz
  751.      *
  752.      * @throws \InvalidArgumentException
  753.      *
  754.      * @return static
  755.      */
  756.     public static function createFromTime($hour null$minute null$second null$tz null)
  757.     {
  758.         return static::create(nullnullnull$hour$minute$second$tz);
  759.     }
  760.     /**
  761.      * Create a Carbon instance from a time string. The date portion is set to today.
  762.      *
  763.      * @param string                    $time
  764.      * @param \DateTimeZone|string|null $tz
  765.      *
  766.      * @throws \InvalidArgumentException
  767.      *
  768.      * @return static
  769.      */
  770.     public static function createFromTimeString($time$tz null)
  771.     {
  772.         return static::today($tz)->setTimeFromTimeString($time);
  773.     }
  774.     private static function createFromFormatAndTimezone($format$time$tz)
  775.     {
  776.         return $tz !== null
  777.             parent::createFromFormat($format$time, static::safeCreateDateTimeZone($tz))
  778.             : parent::createFromFormat($format$time);
  779.     }
  780.     /**
  781.      * Create a Carbon instance from a specific format.
  782.      *
  783.      * @param string                    $format Datetime format
  784.      * @param string                    $time
  785.      * @param \DateTimeZone|string|null $tz
  786.      *
  787.      * @throws InvalidArgumentException
  788.      *
  789.      * @return static
  790.      */
  791.     public static function createFromFormat($format$time$tz null)
  792.     {
  793.         // First attempt to create an instance, so that error messages are based on the unmodified format.
  794.         $date self::createFromFormatAndTimezone($format$time$tz);
  795.         $lastErrors parent::getLastErrors();
  796.         if (($mock = static::getTestNow()) && ($date instanceof DateTime || $date instanceof DateTimeInterface)) {
  797.             // Set timezone from mock if custom timezone was neither given directly nor as a part of format.
  798.             // First let's skip the part that will be ignored by the parser.
  799.             $nonEscaped '(?<!\\\\)(\\\\{2})*';
  800.             $nonIgnored preg_replace("/^.*{$nonEscaped}!/s"''$format);
  801.             if ($tz === null && !preg_match("/{$nonEscaped}[eOPT]/"$nonIgnored)) {
  802.                 $tz $mock->getTimezone();
  803.             }
  804.             // Prepend mock datetime only if the format does not contain non escaped unix epoch reset flag.
  805.             if (!preg_match("/{$nonEscaped}[!|]/"$format)) {
  806.                 $format = static::MOCK_DATETIME_FORMAT.' '.$format;
  807.                 $time $mock->format(static::MOCK_DATETIME_FORMAT).' '.$time;
  808.             }
  809.             // Regenerate date from the modified format to base result on the mocked instance instead of now.
  810.             $date self::createFromFormatAndTimezone($format$time$tz);
  811.         }
  812.         if ($date instanceof DateTime || $date instanceof DateTimeInterface) {
  813.             $instance = static::instance($date);
  814.             $instance::setLastErrors($lastErrors);
  815.             return $instance;
  816.         }
  817.         throw new InvalidArgumentException(implode(PHP_EOL$lastErrors['errors']));
  818.     }
  819.     /**
  820.      * Set last errors.
  821.      *
  822.      * @param array $lastErrors
  823.      *
  824.      * @return void
  825.      */
  826.     private static function setLastErrors(array $lastErrors)
  827.     {
  828.         static::$lastErrors $lastErrors;
  829.     }
  830.     /**
  831.      * {@inheritdoc}
  832.      */
  833.     public static function getLastErrors()
  834.     {
  835.         return static::$lastErrors;
  836.     }
  837.     /**
  838.      * Create a Carbon instance from a timestamp.
  839.      *
  840.      * @param int                       $timestamp
  841.      * @param \DateTimeZone|string|null $tz
  842.      *
  843.      * @return static
  844.      */
  845.     public static function createFromTimestamp($timestamp$tz null)
  846.     {
  847.         return static::today($tz)->setTimestamp($timestamp);
  848.     }
  849.     /**
  850.      * Create a Carbon instance from a timestamp in milliseconds.
  851.      *
  852.      * @param int                       $timestamp
  853.      * @param \DateTimeZone|string|null $tz
  854.      *
  855.      * @return static
  856.      */
  857.     public static function createFromTimestampMs($timestamp$tz null)
  858.     {
  859.         return static::createFromFormat('U.u'sprintf('%F'$timestamp 1000))
  860.             ->setTimezone($tz);
  861.     }
  862.     /**
  863.      * Create a Carbon instance from an UTC timestamp.
  864.      *
  865.      * @param int $timestamp
  866.      *
  867.      * @return static
  868.      */
  869.     public static function createFromTimestampUTC($timestamp)
  870.     {
  871.         return new static('@'.$timestamp);
  872.     }
  873.     /**
  874.      * Make a Carbon instance from given variable if possible.
  875.      *
  876.      * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals
  877.      * and recurrences). Throw an exception for invalid format, but otherwise return null.
  878.      *
  879.      * @param mixed $var
  880.      *
  881.      * @return static|null
  882.      */
  883.     public static function make($var)
  884.     {
  885.         if ($var instanceof DateTime || $var instanceof DateTimeInterface) {
  886.             return static::instance($var);
  887.         }
  888.         if (is_string($var)) {
  889.             $var trim($var);
  890.             $first substr($var01);
  891.             if (is_string($var) && $first !== 'P' && $first !== 'R' && preg_match('/[a-z0-9]/i'$var)) {
  892.                 return static::parse($var);
  893.             }
  894.         }
  895.     }
  896.     /**
  897.      * Get a copy of the instance.
  898.      *
  899.      * @return static
  900.      */
  901.     public function copy()
  902.     {
  903.         return clone $this;
  904.     }
  905.     /**
  906.      * Returns a present instance in the same timezone.
  907.      *
  908.      * @return static
  909.      */
  910.     public function nowWithSameTz()
  911.     {
  912.         return static::now($this->getTimezone());
  913.     }
  914.     /**
  915.      * Throws an exception if the given object is not a DateTime and does not implement DateTimeInterface
  916.      * and not in $other.
  917.      *
  918.      * @param mixed        $date
  919.      * @param string|array $other
  920.      *
  921.      * @throws \InvalidArgumentException
  922.      */
  923.     protected static function expectDateTime($date$other = array())
  924.     {
  925.         $message 'Expected ';
  926.         foreach ((array) $other as $expect) {
  927.             $message .= "{$expect}, ";
  928.         }
  929.         if (!$date instanceof DateTime && !$date instanceof DateTimeInterface) {
  930.             throw new InvalidArgumentException(
  931.                 $message.'DateTime or DateTimeInterface, '.
  932.                 (is_object($date) ? get_class($date) : gettype($date)).' given'
  933.             );
  934.         }
  935.     }
  936.     /**
  937.      * Return the Carbon instance passed through, a now instance in the same timezone
  938.      * if null given or parse the input if string given.
  939.      *
  940.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  941.      *
  942.      * @return static
  943.      */
  944.     protected function resolveCarbon($date null)
  945.     {
  946.         if (!$date) {
  947.             return $this->nowWithSameTz();
  948.         }
  949.         if (is_string($date)) {
  950.             return static::parse($date$this->getTimezone());
  951.         }
  952.         static::expectDateTime($date, array('null''string'));
  953.         return $date instanceof self $date : static::instance($date);
  954.     }
  955.     ///////////////////////////////////////////////////////////////////
  956.     ///////////////////////// GETTERS AND SETTERS /////////////////////
  957.     ///////////////////////////////////////////////////////////////////
  958.     /**
  959.      * Get a part of the Carbon object
  960.      *
  961.      * @param string $name
  962.      *
  963.      * @throws \InvalidArgumentException
  964.      *
  965.      * @return string|int|bool|\DateTimeZone
  966.      */
  967.     public function __get($name)
  968.     {
  969.         static $formats = array(
  970.             'year' => 'Y',
  971.             'yearIso' => 'o',
  972.             'month' => 'n',
  973.             'day' => 'j',
  974.             'hour' => 'G',
  975.             'minute' => 'i',
  976.             'second' => 's',
  977.             'micro' => 'u',
  978.             'dayOfWeek' => 'w',
  979.             'dayOfWeekIso' => 'N',
  980.             'dayOfYear' => 'z',
  981.             'weekOfYear' => 'W',
  982.             'daysInMonth' => 't',
  983.             'timestamp' => 'U',
  984.             'englishDayOfWeek' => 'l',
  985.             'shortEnglishDayOfWeek' => 'D',
  986.             'englishMonth' => 'F',
  987.             'shortEnglishMonth' => 'M',
  988.             'localeDayOfWeek' => '%A',
  989.             'shortLocaleDayOfWeek' => '%a',
  990.             'localeMonth' => '%B',
  991.             'shortLocaleMonth' => '%b',
  992.         );
  993.         switch (true) {
  994.             case isset($formats[$name]):
  995.                 $format $formats[$name];
  996.                 $method substr($format01) === '%' 'formatLocalized' 'format';
  997.                 $value $this->$method($format);
  998.                 return is_numeric($value) ? (int) $value $value;
  999.             case $name === 'weekOfMonth':
  1000.                 return (int) ceil($this->day / static::DAYS_PER_WEEK);
  1001.             case $name === 'weekNumberInMonth':
  1002.                 return (int) ceil(($this->day $this->copy()->startOfMonth()->dayOfWeek 1) / static::DAYS_PER_WEEK);
  1003.             case $name === 'age':
  1004.                 return $this->diffInYears();
  1005.             case $name === 'quarter':
  1006.                 return (int) ceil($this->month / static::MONTHS_PER_QUARTER);
  1007.             case $name === 'offset':
  1008.                 return $this->getOffset();
  1009.             case $name === 'offsetHours':
  1010.                 return $this->getOffset() / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR;
  1011.             case $name === 'dst':
  1012.                 return $this->format('I') === '1';
  1013.             case $name === 'local':
  1014.                 return $this->getOffset() === $this->copy()->setTimezone(date_default_timezone_get())->getOffset();
  1015.             case $name === 'utc':
  1016.                 return $this->getOffset() === 0;
  1017.             case $name === 'timezone' || $name === 'tz':
  1018.                 return $this->getTimezone();
  1019.             case $name === 'timezoneName' || $name === 'tzName':
  1020.                 return $this->getTimezone()->getName();
  1021.             default:
  1022.                 throw new InvalidArgumentException(sprintf("Unknown getter '%s'"$name));
  1023.         }
  1024.     }
  1025.     /**
  1026.      * Check if an attribute exists on the object
  1027.      *
  1028.      * @param string $name
  1029.      *
  1030.      * @return bool
  1031.      */
  1032.     public function __isset($name)
  1033.     {
  1034.         try {
  1035.             $this->__get($name);
  1036.         } catch (InvalidArgumentException $e) {
  1037.             return false;
  1038.         }
  1039.         return true;
  1040.     }
  1041.     /**
  1042.      * Set a part of the Carbon object
  1043.      *
  1044.      * @param string                   $name
  1045.      * @param string|int|\DateTimeZone $value
  1046.      *
  1047.      * @throws \InvalidArgumentException
  1048.      *
  1049.      * @return void
  1050.      */
  1051.     public function __set($name$value)
  1052.     {
  1053.         switch ($name) {
  1054.             case 'year':
  1055.             case 'month':
  1056.             case 'day':
  1057.             case 'hour':
  1058.             case 'minute':
  1059.             case 'second':
  1060.                 list($year$month$day$hour$minute$second) = explode('-'$this->format('Y-n-j-G-i-s'));
  1061.                 $$name $value;
  1062.                 $this->setDateTime($year$month$day$hour$minute$second);
  1063.                 break;
  1064.             case 'timestamp':
  1065.                 parent::setTimestamp($value);
  1066.                 break;
  1067.             case 'timezone':
  1068.             case 'tz':
  1069.                 $this->setTimezone($value);
  1070.                 break;
  1071.             default:
  1072.                 throw new InvalidArgumentException(sprintf("Unknown setter '%s'"$name));
  1073.         }
  1074.     }
  1075.     /**
  1076.      * Set the instance's year
  1077.      *
  1078.      * @param int $value
  1079.      *
  1080.      * @return static
  1081.      */
  1082.     public function year($value)
  1083.     {
  1084.         $this->year $value;
  1085.         return $this;
  1086.     }
  1087.     /**
  1088.      * Set the instance's month
  1089.      *
  1090.      * @param int $value
  1091.      *
  1092.      * @return static
  1093.      */
  1094.     public function month($value)
  1095.     {
  1096.         $this->month $value;
  1097.         return $this;
  1098.     }
  1099.     /**
  1100.      * Set the instance's day
  1101.      *
  1102.      * @param int $value
  1103.      *
  1104.      * @return static
  1105.      */
  1106.     public function day($value)
  1107.     {
  1108.         $this->day $value;
  1109.         return $this;
  1110.     }
  1111.     /**
  1112.      * Set the instance's hour
  1113.      *
  1114.      * @param int $value
  1115.      *
  1116.      * @return static
  1117.      */
  1118.     public function hour($value)
  1119.     {
  1120.         $this->hour $value;
  1121.         return $this;
  1122.     }
  1123.     /**
  1124.      * Set the instance's minute
  1125.      *
  1126.      * @param int $value
  1127.      *
  1128.      * @return static
  1129.      */
  1130.     public function minute($value)
  1131.     {
  1132.         $this->minute $value;
  1133.         return $this;
  1134.     }
  1135.     /**
  1136.      * Set the instance's second
  1137.      *
  1138.      * @param int $value
  1139.      *
  1140.      * @return static
  1141.      */
  1142.     public function second($value)
  1143.     {
  1144.         $this->second $value;
  1145.         return $this;
  1146.     }
  1147.     /**
  1148.      * Sets the current date of the DateTime object to a different date.
  1149.      * Calls modify as a workaround for a php bug
  1150.      *
  1151.      * @param int $year
  1152.      * @param int $month
  1153.      * @param int $day
  1154.      *
  1155.      * @return static
  1156.      *
  1157.      * @see https://github.com/briannesbitt/Carbon/issues/539
  1158.      * @see https://bugs.php.net/bug.php?id=63863
  1159.      */
  1160.     public function setDate($year$month$day)
  1161.     {
  1162.         $this->modify('+0 day');
  1163.         return parent::setDate($year$month$day);
  1164.     }
  1165.     /**
  1166.      * Set the date and time all together
  1167.      *
  1168.      * @param int $year
  1169.      * @param int $month
  1170.      * @param int $day
  1171.      * @param int $hour
  1172.      * @param int $minute
  1173.      * @param int $second
  1174.      *
  1175.      * @return static
  1176.      */
  1177.     public function setDateTime($year$month$day$hour$minute$second 0)
  1178.     {
  1179.         return $this->setDate($year$month$day)->setTime($hour$minute$second);
  1180.     }
  1181.     /**
  1182.      * Set the time by time string
  1183.      *
  1184.      * @param string $time
  1185.      *
  1186.      * @return static
  1187.      */
  1188.     public function setTimeFromTimeString($time)
  1189.     {
  1190.         if (strpos($time':') === false) {
  1191.             $time .= ':0';
  1192.         }
  1193.         return $this->modify($time);
  1194.     }
  1195.     /**
  1196.      * Set the instance's timestamp
  1197.      *
  1198.      * @param int $value
  1199.      *
  1200.      * @return static
  1201.      */
  1202.     public function timestamp($value)
  1203.     {
  1204.         return $this->setTimestamp($value);
  1205.     }
  1206.     /**
  1207.      * Alias for setTimezone()
  1208.      *
  1209.      * @param \DateTimeZone|string $value
  1210.      *
  1211.      * @return static
  1212.      */
  1213.     public function timezone($value)
  1214.     {
  1215.         return $this->setTimezone($value);
  1216.     }
  1217.     /**
  1218.      * Alias for setTimezone()
  1219.      *
  1220.      * @param \DateTimeZone|string $value
  1221.      *
  1222.      * @return static
  1223.      */
  1224.     public function tz($value)
  1225.     {
  1226.         return $this->setTimezone($value);
  1227.     }
  1228.     /**
  1229.      * Set the instance's timezone from a string or object
  1230.      *
  1231.      * @param \DateTimeZone|string $value
  1232.      *
  1233.      * @return static
  1234.      */
  1235.     public function setTimezone($value)
  1236.     {
  1237.         parent::setTimezone(static::safeCreateDateTimeZone($value));
  1238.         // https://bugs.php.net/bug.php?id=72338
  1239.         // just workaround on this bug
  1240.         $this->getTimestamp();
  1241.         return $this;
  1242.     }
  1243.     /**
  1244.      * Set the year, month, and date for this instance to that of the passed instance.
  1245.      *
  1246.      * @param \Carbon\Carbon|\DateTimeInterface $date
  1247.      *
  1248.      * @return static
  1249.      */
  1250.     public function setDateFrom($date)
  1251.     {
  1252.         $date = static::instance($date);
  1253.         $this->setDate($date->year$date->month$date->day);
  1254.         return $this;
  1255.     }
  1256.     /**
  1257.      * Set the hour, day, and time for this instance to that of the passed instance.
  1258.      *
  1259.      * @param \Carbon\Carbon|\DateTimeInterface $date
  1260.      *
  1261.      * @return static
  1262.      */
  1263.     public function setTimeFrom($date)
  1264.     {
  1265.         $date = static::instance($date);
  1266.         $this->setTime($date->hour$date->minute$date->second);
  1267.         return $this;
  1268.     }
  1269.     /**
  1270.      * Get the days of the week
  1271.      *
  1272.      * @return array
  1273.      */
  1274.     public static function getDays()
  1275.     {
  1276.         return static::$days;
  1277.     }
  1278.     ///////////////////////////////////////////////////////////////////
  1279.     /////////////////////// WEEK SPECIAL DAYS /////////////////////////
  1280.     ///////////////////////////////////////////////////////////////////
  1281.     /**
  1282.      * Get the first day of week
  1283.      *
  1284.      * @return int
  1285.      */
  1286.     public static function getWeekStartsAt()
  1287.     {
  1288.         return static::$weekStartsAt;
  1289.     }
  1290.     /**
  1291.      * Set the first day of week
  1292.      *
  1293.      * @param int $day week start day
  1294.      *
  1295.      * @throws InvalidArgumentException
  1296.      *
  1297.      * @return void
  1298.      */
  1299.     public static function setWeekStartsAt($day)
  1300.     {
  1301.         if ($day > static::SATURDAY || $day < static::SUNDAY) {
  1302.             throw new InvalidArgumentException('Day of a week should be greater than or equal to 0 and less than or equal to 6.');
  1303.         }
  1304.         static::$weekStartsAt $day;
  1305.     }
  1306.     /**
  1307.      * Get the last day of week
  1308.      *
  1309.      * @return int
  1310.      */
  1311.     public static function getWeekEndsAt()
  1312.     {
  1313.         return static::$weekEndsAt;
  1314.     }
  1315.     /**
  1316.      * Set the last day of week
  1317.      *
  1318.      * @param int $day
  1319.      *
  1320.      * @throws InvalidArgumentException
  1321.      *
  1322.      * @return void
  1323.      */
  1324.     public static function setWeekEndsAt($day)
  1325.     {
  1326.         if ($day > static::SATURDAY || $day < static::SUNDAY) {
  1327.             throw new InvalidArgumentException('Day of a week should be greater than or equal to 0 and less than or equal to 6.');
  1328.         }
  1329.         static::$weekEndsAt $day;
  1330.     }
  1331.     /**
  1332.      * Get weekend days
  1333.      *
  1334.      * @return array
  1335.      */
  1336.     public static function getWeekendDays()
  1337.     {
  1338.         return static::$weekendDays;
  1339.     }
  1340.     /**
  1341.      * Set weekend days
  1342.      *
  1343.      * @param array $days
  1344.      *
  1345.      * @return void
  1346.      */
  1347.     public static function setWeekendDays($days)
  1348.     {
  1349.         static::$weekendDays $days;
  1350.     }
  1351.     /**
  1352.      * get midday/noon hour
  1353.      *
  1354.      * @return int
  1355.      */
  1356.     public static function getMidDayAt()
  1357.     {
  1358.         return static::$midDayAt;
  1359.     }
  1360.     /**
  1361.      * Set midday/noon hour
  1362.      *
  1363.      * @param int $hour midday hour
  1364.      *
  1365.      * @return void
  1366.      */
  1367.     public static function setMidDayAt($hour)
  1368.     {
  1369.         static::$midDayAt $hour;
  1370.     }
  1371.     ///////////////////////////////////////////////////////////////////
  1372.     ///////////////////////// TESTING AIDS ////////////////////////////
  1373.     ///////////////////////////////////////////////////////////////////
  1374.     /**
  1375.      * Set a Carbon instance (real or mock) to be returned when a "now"
  1376.      * instance is created.  The provided instance will be returned
  1377.      * specifically under the following conditions:
  1378.      *   - A call to the static now() method, ex. Carbon::now()
  1379.      *   - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null)
  1380.      *   - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now')
  1381.      *   - When a string containing the desired time is passed to Carbon::parse().
  1382.      *
  1383.      * Note the timezone parameter was left out of the examples above and
  1384.      * has no affect as the mock value will be returned regardless of its value.
  1385.      *
  1386.      * To clear the test instance call this method using the default
  1387.      * parameter of null.
  1388.      *
  1389.      * @param \Carbon\Carbon|null        $testNow real or mock Carbon instance
  1390.      * @param \Carbon\Carbon|string|null $testNow
  1391.      */
  1392.     public static function setTestNow($testNow null)
  1393.     {
  1394.         static::$testNow is_string($testNow) ? static::parse($testNow) : $testNow;
  1395.     }
  1396.     /**
  1397.      * Get the Carbon instance (real or mock) to be returned when a "now"
  1398.      * instance is created.
  1399.      *
  1400.      * @return static the current instance used for testing
  1401.      */
  1402.     public static function getTestNow()
  1403.     {
  1404.         return static::$testNow;
  1405.     }
  1406.     /**
  1407.      * Determine if there is a valid test instance set. A valid test instance
  1408.      * is anything that is not null.
  1409.      *
  1410.      * @return bool true if there is a test instance, otherwise false
  1411.      */
  1412.     public static function hasTestNow()
  1413.     {
  1414.         return static::getTestNow() !== null;
  1415.     }
  1416.     /**
  1417.      * Determine if a time string will produce a relative date.
  1418.      *
  1419.      * @param string $time
  1420.      *
  1421.      * @return bool true if time match a relative date, false if absolute or invalid time string
  1422.      */
  1423.     public static function hasRelativeKeywords($time)
  1424.     {
  1425.         if (strtotime($time) === false) {
  1426.             return false;
  1427.         }
  1428.         $date1 = new DateTime('2000-01-01T00:00:00Z');
  1429.         $date1->modify($time);
  1430.         $date2 = new DateTime('2001-12-25T00:00:00Z');
  1431.         $date2->modify($time);
  1432.         return $date1 != $date2;
  1433.     }
  1434.     ///////////////////////////////////////////////////////////////////
  1435.     /////////////////////// LOCALIZATION //////////////////////////////
  1436.     ///////////////////////////////////////////////////////////////////
  1437.     /**
  1438.      * Initialize the translator instance if necessary.
  1439.      *
  1440.      * @return \Symfony\Component\Translation\TranslatorInterface
  1441.      */
  1442.     protected static function translator()
  1443.     {
  1444.         if (static::$translator === null) {
  1445.             static::$translator Translator::get();
  1446.         }
  1447.         return static::$translator;
  1448.     }
  1449.     /**
  1450.      * Get the translator instance in use
  1451.      *
  1452.      * @return \Symfony\Component\Translation\TranslatorInterface
  1453.      */
  1454.     public static function getTranslator()
  1455.     {
  1456.         return static::translator();
  1457.     }
  1458.     /**
  1459.      * Set the translator instance to use
  1460.      *
  1461.      * @param \Symfony\Component\Translation\TranslatorInterface $translator
  1462.      *
  1463.      * @return void
  1464.      */
  1465.     public static function setTranslator(TranslatorInterface $translator)
  1466.     {
  1467.         static::$translator $translator;
  1468.     }
  1469.     /**
  1470.      * Get the current translator locale
  1471.      *
  1472.      * @return string
  1473.      */
  1474.     public static function getLocale()
  1475.     {
  1476.         return static::translator()->getLocale();
  1477.     }
  1478.     /**
  1479.      * Set the current translator locale and indicate if the source locale file exists
  1480.      *
  1481.      * @param string $locale locale ex. en
  1482.      *
  1483.      * @return bool
  1484.      */
  1485.     public static function setLocale($locale)
  1486.     {
  1487.         return static::translator()->setLocale($locale) !== false;
  1488.     }
  1489.     /**
  1490.      * Set the current locale to the given, execute the passed function, reset the locale to previous one,
  1491.      * then return the result of the closure (or null if the closure was void).
  1492.      *
  1493.      * @param string $locale locale ex. en
  1494.      *
  1495.      * @return mixed
  1496.      */
  1497.     public static function executeWithLocale($locale$func)
  1498.     {
  1499.         $currentLocale = static::getLocale();
  1500.         $result call_user_func($func, static::setLocale($locale) ? static::getLocale() : false, static::translator());
  1501.         static::setLocale($currentLocale);
  1502.         return $result;
  1503.     }
  1504.     /**
  1505.      * Returns true if the given locale is internally supported and has short-units support.
  1506.      * Support is considered enabled if either year, day or hour has a short variant translated.
  1507.      *
  1508.      * @param string $locale locale ex. en
  1509.      *
  1510.      * @return bool
  1511.      */
  1512.     public static function localeHasShortUnits($locale)
  1513.     {
  1514.         return static::executeWithLocale($locale, function ($newLocaleTranslatorInterface $translator) {
  1515.             return $newLocale &&
  1516.                 (
  1517.                     ($y $translator->trans('y')) !== 'y' &&
  1518.                     $y !== $translator->trans('year')
  1519.                 ) || (
  1520.                     ($y $translator->trans('d')) !== 'd' &&
  1521.                     $y !== $translator->trans('day')
  1522.                 ) || (
  1523.                     ($y $translator->trans('h')) !== 'h' &&
  1524.                     $y !== $translator->trans('hour')
  1525.                 );
  1526.         });
  1527.     }
  1528.     /**
  1529.      * Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after).
  1530.      * Support is considered enabled if the 4 sentences are translated in the given locale.
  1531.      *
  1532.      * @param string $locale locale ex. en
  1533.      *
  1534.      * @return bool
  1535.      */
  1536.     public static function localeHasDiffSyntax($locale)
  1537.     {
  1538.         return static::executeWithLocale($locale, function ($newLocaleTranslatorInterface $translator) {
  1539.             return $newLocale &&
  1540.                 $translator->trans('ago') !== 'ago' &&
  1541.                 $translator->trans('from_now') !== 'from_now' &&
  1542.                 $translator->trans('before') !== 'before' &&
  1543.                 $translator->trans('after') !== 'after';
  1544.         });
  1545.     }
  1546.     /**
  1547.      * Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow).
  1548.      * Support is considered enabled if the 3 words are translated in the given locale.
  1549.      *
  1550.      * @param string $locale locale ex. en
  1551.      *
  1552.      * @return bool
  1553.      */
  1554.     public static function localeHasDiffOneDayWords($locale)
  1555.     {
  1556.         return static::executeWithLocale($locale, function ($newLocaleTranslatorInterface $translator) {
  1557.             return $newLocale &&
  1558.                 $translator->trans('diff_now') !== 'diff_now' &&
  1559.                 $translator->trans('diff_yesterday') !== 'diff_yesterday' &&
  1560.                 $translator->trans('diff_tomorrow') !== 'diff_tomorrow';
  1561.         });
  1562.     }
  1563.     /**
  1564.      * Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow).
  1565.      * Support is considered enabled if the 2 words are translated in the given locale.
  1566.      *
  1567.      * @param string $locale locale ex. en
  1568.      *
  1569.      * @return bool
  1570.      */
  1571.     public static function localeHasDiffTwoDayWords($locale)
  1572.     {
  1573.         return static::executeWithLocale($locale, function ($newLocaleTranslatorInterface $translator) {
  1574.             return $newLocale &&
  1575.                 $translator->trans('diff_before_yesterday') !== 'diff_before_yesterday' &&
  1576.                 $translator->trans('diff_after_tomorrow') !== 'diff_after_tomorrow';
  1577.         });
  1578.     }
  1579.     /**
  1580.      * Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X).
  1581.      * Support is considered enabled if the 4 sentences are translated in the given locale.
  1582.      *
  1583.      * @param string $locale locale ex. en
  1584.      *
  1585.      * @return bool
  1586.      */
  1587.     public static function localeHasPeriodSyntax($locale)
  1588.     {
  1589.         return static::executeWithLocale($locale, function ($newLocaleTranslatorInterface $translator) {
  1590.             return $newLocale &&
  1591.                 $translator->trans('period_recurrences') !== 'period_recurrences' &&
  1592.                 $translator->trans('period_interval') !== 'period_interval' &&
  1593.                 $translator->trans('period_start_date') !== 'period_start_date' &&
  1594.                 $translator->trans('period_end_date') !== 'period_end_date';
  1595.         });
  1596.     }
  1597.     /**
  1598.      * Returns the list of internally available locales and already loaded custom locales.
  1599.      * (It will ignore custom translator dynamic loading.)
  1600.      *
  1601.      * @return array
  1602.      */
  1603.     public static function getAvailableLocales()
  1604.     {
  1605.         $translator = static::translator();
  1606.         $locales = array();
  1607.         if ($translator instanceof Translator) {
  1608.             foreach (glob(__DIR__.'/Lang/*.php') as $file) {
  1609.                 $locales[] = substr($filestrrpos($file'/') + 1, -4);
  1610.             }
  1611.             $locales array_unique(array_merge($localesarray_keys($translator->getMessages())));
  1612.         }
  1613.         return $locales;
  1614.     }
  1615.     ///////////////////////////////////////////////////////////////////
  1616.     /////////////////////// STRING FORMATTING /////////////////////////
  1617.     ///////////////////////////////////////////////////////////////////
  1618.     /**
  1619.      * Set if UTF8 will be used for localized date/time
  1620.      *
  1621.      * @param bool $utf8
  1622.      */
  1623.     public static function setUtf8($utf8)
  1624.     {
  1625.         static::$utf8 $utf8;
  1626.     }
  1627.     /**
  1628.      * Format the instance with the current locale.  You can set the current
  1629.      * locale using setlocale() http://php.net/setlocale.
  1630.      *
  1631.      * @param string $format
  1632.      *
  1633.      * @return string
  1634.      */
  1635.     public function formatLocalized($format)
  1636.     {
  1637.         // Check for Windows to find and replace the %e modifier correctly.
  1638.         if (strtoupper(substr(PHP_OS03)) === 'WIN') {
  1639.             $format preg_replace('#(?<!%)((?:%%)*)%e#''\1%#d'$format); // @codeCoverageIgnore
  1640.         }
  1641.         $formatted strftime($formatstrtotime($this->toDateTimeString()));
  1642.         return static::$utf8 utf8_encode($formatted) : $formatted;
  1643.     }
  1644.     /**
  1645.      * Reset the format used to the default when type juggling a Carbon instance to a string
  1646.      *
  1647.      * @return void
  1648.      */
  1649.     public static function resetToStringFormat()
  1650.     {
  1651.         static::setToStringFormat(static::DEFAULT_TO_STRING_FORMAT);
  1652.     }
  1653.     /**
  1654.      * Set the default format used when type juggling a Carbon instance to a string
  1655.      *
  1656.      * @param string|Closure $format
  1657.      *
  1658.      * @return void
  1659.      */
  1660.     public static function setToStringFormat($format)
  1661.     {
  1662.         static::$toStringFormat $format;
  1663.     }
  1664.     /**
  1665.      * Format the instance as a string using the set format
  1666.      *
  1667.      * @return string
  1668.      */
  1669.     public function __toString()
  1670.     {
  1671.         $format = static::$toStringFormat;
  1672.         return $this->format($format instanceof Closure $format($this) : $format);
  1673.     }
  1674.     /**
  1675.      * Format the instance as date
  1676.      *
  1677.      * @return string
  1678.      */
  1679.     public function toDateString()
  1680.     {
  1681.         return $this->format('Y-m-d');
  1682.     }
  1683.     /**
  1684.      * Format the instance as a readable date
  1685.      *
  1686.      * @return string
  1687.      */
  1688.     public function toFormattedDateString()
  1689.     {
  1690.         return $this->format('M j, Y');
  1691.     }
  1692.     /**
  1693.      * Format the instance as time
  1694.      *
  1695.      * @return string
  1696.      */
  1697.     public function toTimeString()
  1698.     {
  1699.         return $this->format('H:i:s');
  1700.     }
  1701.     /**
  1702.      * Format the instance as date and time
  1703.      *
  1704.      * @return string
  1705.      */
  1706.     public function toDateTimeString()
  1707.     {
  1708.         return $this->format('Y-m-d H:i:s');
  1709.     }
  1710.     /**
  1711.      * Format the instance as date and time T-separated with no timezone
  1712.      *
  1713.      * @example
  1714.      * ```
  1715.      * echo Carbon::now()->toDateTimeLocalString();
  1716.      * ```
  1717.      *
  1718.      * @return string
  1719.      */
  1720.     public function toDateTimeLocalString()
  1721.     {
  1722.         return $this->format('Y-m-d\TH:i:s');
  1723.     }
  1724.     /**
  1725.      * Format the instance with day, date and time
  1726.      *
  1727.      * @return string
  1728.      */
  1729.     public function toDayDateTimeString()
  1730.     {
  1731.         return $this->format('D, M j, Y g:i A');
  1732.     }
  1733.     /**
  1734.      * Format the instance as ATOM
  1735.      *
  1736.      * @return string
  1737.      */
  1738.     public function toAtomString()
  1739.     {
  1740.         return $this->format(static::ATOM);
  1741.     }
  1742.     /**
  1743.      * Format the instance as COOKIE
  1744.      *
  1745.      * @return string
  1746.      */
  1747.     public function toCookieString()
  1748.     {
  1749.         return $this->format(static::COOKIE);
  1750.     }
  1751.     /**
  1752.      * Format the instance as ISO8601
  1753.      *
  1754.      * @return string
  1755.      */
  1756.     public function toIso8601String()
  1757.     {
  1758.         return $this->toAtomString();
  1759.     }
  1760.     /**
  1761.      * Format the instance as RFC822
  1762.      *
  1763.      * @return string
  1764.      */
  1765.     public function toRfc822String()
  1766.     {
  1767.         return $this->format(static::RFC822);
  1768.     }
  1769.     /**
  1770.      * Convert the instance to UTC and return as Zulu ISO8601
  1771.      *
  1772.      * @return string
  1773.      */
  1774.     public function toIso8601ZuluString()
  1775.     {
  1776.         return $this->copy()->setTimezone('UTC')->format('Y-m-d\TH:i:s\Z');
  1777.     }
  1778.     /**
  1779.      * Format the instance as RFC850
  1780.      *
  1781.      * @return string
  1782.      */
  1783.     public function toRfc850String()
  1784.     {
  1785.         return $this->format(static::RFC850);
  1786.     }
  1787.     /**
  1788.      * Format the instance as RFC1036
  1789.      *
  1790.      * @return string
  1791.      */
  1792.     public function toRfc1036String()
  1793.     {
  1794.         return $this->format(static::RFC1036);
  1795.     }
  1796.     /**
  1797.      * Format the instance as RFC1123
  1798.      *
  1799.      * @return string
  1800.      */
  1801.     public function toRfc1123String()
  1802.     {
  1803.         return $this->format(static::RFC1123);
  1804.     }
  1805.     /**
  1806.      * Format the instance as RFC2822
  1807.      *
  1808.      * @return string
  1809.      */
  1810.     public function toRfc2822String()
  1811.     {
  1812.         return $this->format(static::RFC2822);
  1813.     }
  1814.     /**
  1815.      * Format the instance as RFC3339
  1816.      *
  1817.      * @return string
  1818.      */
  1819.     public function toRfc3339String()
  1820.     {
  1821.         return $this->format(static::RFC3339);
  1822.     }
  1823.     /**
  1824.      * Format the instance as RSS
  1825.      *
  1826.      * @return string
  1827.      */
  1828.     public function toRssString()
  1829.     {
  1830.         return $this->format(static::RSS);
  1831.     }
  1832.     /**
  1833.      * Format the instance as W3C
  1834.      *
  1835.      * @return string
  1836.      */
  1837.     public function toW3cString()
  1838.     {
  1839.         return $this->format(static::W3C);
  1840.     }
  1841.     /**
  1842.      * Format the instance as RFC7231
  1843.      *
  1844.      * @return string
  1845.      */
  1846.     public function toRfc7231String()
  1847.     {
  1848.         return $this->copy()
  1849.             ->setTimezone('GMT')
  1850.             ->format(static::RFC7231_FORMAT);
  1851.     }
  1852.     /**
  1853.      * Get default array representation
  1854.      *
  1855.      * @return array
  1856.      */
  1857.     public function toArray()
  1858.     {
  1859.         return array(
  1860.             'year' => $this->year,
  1861.             'month' => $this->month,
  1862.             'day' => $this->day,
  1863.             'dayOfWeek' => $this->dayOfWeek,
  1864.             'dayOfYear' => $this->dayOfYear,
  1865.             'hour' => $this->hour,
  1866.             'minute' => $this->minute,
  1867.             'second' => $this->second,
  1868.             'micro' => $this->micro,
  1869.             'timestamp' => $this->timestamp,
  1870.             'formatted' => $this->format(self::DEFAULT_TO_STRING_FORMAT),
  1871.             'timezone' => $this->timezone,
  1872.         );
  1873.     }
  1874.     /**
  1875.      * Get default object representation.
  1876.      *
  1877.      * @example
  1878.      * ```
  1879.      * var_dump(Carbon::now()->toObject());
  1880.      * ```
  1881.      *
  1882.      * @return object
  1883.      */
  1884.     public function toObject()
  1885.     {
  1886.         return (object) $this->toArray();
  1887.     }
  1888.     /**
  1889.      * Returns english human readable complete date string.
  1890.      *
  1891.      * @example
  1892.      * ```
  1893.      * echo Carbon::now()->toString();
  1894.      * ```
  1895.      *
  1896.      * @return string
  1897.      */
  1898.     public function toString()
  1899.     {
  1900.         return $this->format('D M j Y H:i:s \G\M\TO');
  1901.     }
  1902.     /**
  1903.      * Return the ISO-8601 string (ex: 1977-04-22T06:00:00Z, if $keepOffset truthy, offset will be kept:
  1904.      * 1977-04-22T01:00:00-05:00).
  1905.      *
  1906.      * @example
  1907.      * ```
  1908.      * echo Carbon::now('America/Toronto')->toISOString() . "\n";
  1909.      * echo Carbon::now('America/Toronto')->toISOString(true) . "\n";
  1910.      * ```
  1911.      *
  1912.      * @param bool $keepOffset Pass true to keep the date offset. Else forced to UTC.
  1913.      *
  1914.      * @return null|string
  1915.      */
  1916.     public function toISOString($keepOffset false)
  1917.     {
  1918.         if ($this->year === 0) {
  1919.             return null;
  1920.         }
  1921.         $year $this->year || $this->year 9999
  1922.             ? ($this->year '-' '+').str_pad(abs($this->year), 6'0'STR_PAD_LEFT)
  1923.             : str_pad($this->year4'0'STR_PAD_LEFT);
  1924.         $tz $keepOffset $this->format('P') : 'Z';
  1925.         $date $keepOffset $this $this->copy()->setTimezone('UTC');
  1926.         return $year.$date->format('-m-d\TH:i:s.u').$tz;
  1927.     }
  1928.     /**
  1929.      * Return the ISO-8601 string (ex: 1977-04-22T06:00:00Z) with UTC timezone.
  1930.      *
  1931.      * @example
  1932.      * ```
  1933.      * echo Carbon::now('America/Toronto')->toJSON();
  1934.      * ```
  1935.      *
  1936.      * @return null|string
  1937.      */
  1938.     public function toJSON()
  1939.     {
  1940.         return $this->toISOString();
  1941.     }
  1942.     /**
  1943.      * Return native DateTime PHP object matching the current instance.
  1944.      *
  1945.      * @example
  1946.      * ```
  1947.      * var_dump(Carbon::now()->toDateTime());
  1948.      * ```
  1949.      *
  1950.      * @return DateTime
  1951.      */
  1952.     public function toDateTime()
  1953.     {
  1954.         return new DateTime($this->format('Y-m-d H:i:s.u'), $this->getTimezone());
  1955.     }
  1956.     /**
  1957.      * @alias toDateTime
  1958.      *
  1959.      * Return native DateTime PHP object matching the current instance.
  1960.      *
  1961.      * @example
  1962.      * ```
  1963.      * var_dump(Carbon::now()->toDate());
  1964.      * ```
  1965.      *
  1966.      * @return DateTime
  1967.      */
  1968.     public function toDate()
  1969.     {
  1970.         return $this->toDateTime();
  1971.     }
  1972.     ///////////////////////////////////////////////////////////////////
  1973.     ////////////////////////// COMPARISONS ////////////////////////////
  1974.     ///////////////////////////////////////////////////////////////////
  1975.     /**
  1976.      * Determines if the instance is equal to another
  1977.      *
  1978.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  1979.      *
  1980.      * @return bool
  1981.      */
  1982.     public function eq($date)
  1983.     {
  1984.         return $this == $date;
  1985.     }
  1986.     /**
  1987.      * Determines if the instance is equal to another
  1988.      *
  1989.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  1990.      *
  1991.      * @see eq()
  1992.      *
  1993.      * @return bool
  1994.      */
  1995.     public function equalTo($date)
  1996.     {
  1997.         return $this->eq($date);
  1998.     }
  1999.     /**
  2000.      * Determines if the instance is not equal to another
  2001.      *
  2002.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2003.      *
  2004.      * @return bool
  2005.      */
  2006.     public function ne($date)
  2007.     {
  2008.         return !$this->eq($date);
  2009.     }
  2010.     /**
  2011.      * Determines if the instance is not equal to another
  2012.      *
  2013.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2014.      *
  2015.      * @see ne()
  2016.      *
  2017.      * @return bool
  2018.      */
  2019.     public function notEqualTo($date)
  2020.     {
  2021.         return $this->ne($date);
  2022.     }
  2023.     /**
  2024.      * Determines if the instance is greater (after) than another
  2025.      *
  2026.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2027.      *
  2028.      * @return bool
  2029.      */
  2030.     public function gt($date)
  2031.     {
  2032.         return $this $date;
  2033.     }
  2034.     /**
  2035.      * Determines if the instance is greater (after) than another
  2036.      *
  2037.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2038.      *
  2039.      * @see gt()
  2040.      *
  2041.      * @return bool
  2042.      */
  2043.     public function greaterThan($date)
  2044.     {
  2045.         return $this->gt($date);
  2046.     }
  2047.     /**
  2048.      * Determines if the instance is greater (after) than another
  2049.      *
  2050.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2051.      *
  2052.      * @see gt()
  2053.      *
  2054.      * @return bool
  2055.      */
  2056.     public function isAfter($date)
  2057.     {
  2058.         return $this->gt($date);
  2059.     }
  2060.     /**
  2061.      * Determines if the instance is greater (after) than or equal to another
  2062.      *
  2063.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2064.      *
  2065.      * @return bool
  2066.      */
  2067.     public function gte($date)
  2068.     {
  2069.         return $this >= $date;
  2070.     }
  2071.     /**
  2072.      * Determines if the instance is greater (after) than or equal to another
  2073.      *
  2074.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2075.      *
  2076.      * @see gte()
  2077.      *
  2078.      * @return bool
  2079.      */
  2080.     public function greaterThanOrEqualTo($date)
  2081.     {
  2082.         return $this->gte($date);
  2083.     }
  2084.     /**
  2085.      * Determines if the instance is less (before) than another
  2086.      *
  2087.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2088.      *
  2089.      * @return bool
  2090.      */
  2091.     public function lt($date)
  2092.     {
  2093.         return $this $date;
  2094.     }
  2095.     /**
  2096.      * Determines if the instance is less (before) than another
  2097.      *
  2098.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2099.      *
  2100.      * @see lt()
  2101.      *
  2102.      * @return bool
  2103.      */
  2104.     public function lessThan($date)
  2105.     {
  2106.         return $this->lt($date);
  2107.     }
  2108.     /**
  2109.      * Determines if the instance is less (before) than another
  2110.      *
  2111.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2112.      *
  2113.      * @see lt()
  2114.      *
  2115.      * @return bool
  2116.      */
  2117.     public function isBefore($date)
  2118.     {
  2119.         return $this->lt($date);
  2120.     }
  2121.     /**
  2122.      * Determines if the instance is less (before) or equal to another
  2123.      *
  2124.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2125.      *
  2126.      * @return bool
  2127.      */
  2128.     public function lte($date)
  2129.     {
  2130.         return $this <= $date;
  2131.     }
  2132.     /**
  2133.      * Determines if the instance is less (before) or equal to another
  2134.      *
  2135.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2136.      *
  2137.      * @see lte()
  2138.      *
  2139.      * @return bool
  2140.      */
  2141.     public function lessThanOrEqualTo($date)
  2142.     {
  2143.         return $this->lte($date);
  2144.     }
  2145.     /**
  2146.      * Determines if the instance is between two others
  2147.      *
  2148.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
  2149.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
  2150.      * @param bool                                    $equal Indicates if an equal to comparison should be done
  2151.      *
  2152.      * @return bool
  2153.      */
  2154.     public function between($date1$date2$equal true)
  2155.     {
  2156.         if ($date1->gt($date2)) {
  2157.             $temp $date1;
  2158.             $date1 $date2;
  2159.             $date2 $temp;
  2160.         }
  2161.         if ($equal) {
  2162.             return $this->gte($date1) && $this->lte($date2);
  2163.         }
  2164.         return $this->gt($date1) && $this->lt($date2);
  2165.     }
  2166.     protected function floatDiffInSeconds($date)
  2167.     {
  2168.         $date $this->resolveCarbon($date);
  2169.         return abs($this->diffInRealSeconds($datefalse) + ($date->micro $this->micro) / 1000000);
  2170.     }
  2171.     /**
  2172.      * Determines if the instance is between two others
  2173.      *
  2174.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
  2175.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
  2176.      * @param bool                                    $equal Indicates if a > and < comparison should be used or <= or >=
  2177.      *
  2178.      * @return bool
  2179.      */
  2180.     public function isBetween($date1$date2$equal true)
  2181.     {
  2182.         return $this->between($date1$date2$equal);
  2183.     }
  2184.     /**
  2185.      * Get the closest date from the instance.
  2186.      *
  2187.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
  2188.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
  2189.      *
  2190.      * @return static
  2191.      */
  2192.     public function closest($date1$date2)
  2193.     {
  2194.         return $this->floatDiffInSeconds($date1) < $this->floatDiffInSeconds($date2) ? $date1 $date2;
  2195.     }
  2196.     /**
  2197.      * Get the farthest date from the instance.
  2198.      *
  2199.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
  2200.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
  2201.      *
  2202.      * @return static
  2203.      */
  2204.     public function farthest($date1$date2)
  2205.     {
  2206.         return $this->floatDiffInSeconds($date1) > $this->floatDiffInSeconds($date2) ? $date1 $date2;
  2207.     }
  2208.     /**
  2209.      * Get the minimum instance between a given instance (default now) and the current instance.
  2210.      *
  2211.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  2212.      *
  2213.      * @return static
  2214.      */
  2215.     public function min($date null)
  2216.     {
  2217.         $date $this->resolveCarbon($date);
  2218.         return $this->lt($date) ? $this $date;
  2219.     }
  2220.     /**
  2221.      * Get the minimum instance between a given instance (default now) and the current instance.
  2222.      *
  2223.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2224.      *
  2225.      * @see min()
  2226.      *
  2227.      * @return static
  2228.      */
  2229.     public function minimum($date null)
  2230.     {
  2231.         return $this->min($date);
  2232.     }
  2233.     /**
  2234.      * Get the maximum instance between a given instance (default now) and the current instance.
  2235.      *
  2236.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  2237.      *
  2238.      * @return static
  2239.      */
  2240.     public function max($date null)
  2241.     {
  2242.         $date $this->resolveCarbon($date);
  2243.         return $this->gt($date) ? $this $date;
  2244.     }
  2245.     /**
  2246.      * Get the maximum instance between a given instance (default now) and the current instance.
  2247.      *
  2248.      * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
  2249.      *
  2250.      * @see max()
  2251.      *
  2252.      * @return static
  2253.      */
  2254.     public function maximum($date null)
  2255.     {
  2256.         return $this->max($date);
  2257.     }
  2258.     /**
  2259.      * Determines if the instance is a weekday.
  2260.      *
  2261.      * @return bool
  2262.      */
  2263.     public function isWeekday()
  2264.     {
  2265.         return !$this->isWeekend();
  2266.     }
  2267.     /**
  2268.      * Determines if the instance is a weekend day.
  2269.      *
  2270.      * @return bool
  2271.      */
  2272.     public function isWeekend()
  2273.     {
  2274.         return in_array($this->dayOfWeek, static::$weekendDays);
  2275.     }
  2276.     /**
  2277.      * Determines if the instance is yesterday.
  2278.      *
  2279.      * @return bool
  2280.      */
  2281.     public function isYesterday()
  2282.     {
  2283.         return $this->toDateString() === static::yesterday($this->getTimezone())->toDateString();
  2284.     }
  2285.     /**
  2286.      * Determines if the instance is today.
  2287.      *
  2288.      * @return bool
  2289.      */
  2290.     public function isToday()
  2291.     {
  2292.         return $this->toDateString() === $this->nowWithSameTz()->toDateString();
  2293.     }
  2294.     /**
  2295.      * Determines if the instance is tomorrow.
  2296.      *
  2297.      * @return bool
  2298.      */
  2299.     public function isTomorrow()
  2300.     {
  2301.         return $this->toDateString() === static::tomorrow($this->getTimezone())->toDateString();
  2302.     }
  2303.     /**
  2304.      * Determines if the instance is within the next week.
  2305.      *
  2306.      * @return bool
  2307.      */
  2308.     public function isNextWeek()
  2309.     {
  2310.         return $this->weekOfYear === $this->nowWithSameTz()->addWeek()->weekOfYear;
  2311.     }
  2312.     /**
  2313.      * Determines if the instance is within the last week.
  2314.      *
  2315.      * @return bool
  2316.      */
  2317.     public function isLastWeek()
  2318.     {
  2319.         return $this->weekOfYear === $this->nowWithSameTz()->subWeek()->weekOfYear;
  2320.     }
  2321.     /**
  2322.      * Determines if the instance is within the next quarter.
  2323.      *
  2324.      * @return bool
  2325.      */
  2326.     public function isNextQuarter()
  2327.     {
  2328.         return $this->quarter === $this->nowWithSameTz()->addQuarter()->quarter;
  2329.     }
  2330.     /**
  2331.      * Determines if the instance is within the last quarter.
  2332.      *
  2333.      * @return bool
  2334.      */
  2335.     public function isLastQuarter()
  2336.     {
  2337.         return $this->quarter === $this->nowWithSameTz()->subQuarter()->quarter;
  2338.     }
  2339.     /**
  2340.      * Determines if the instance is within the next month.
  2341.      *
  2342.      * @return bool
  2343.      */
  2344.     public function isNextMonth()
  2345.     {
  2346.         return $this->month === $this->nowWithSameTz()->addMonthNoOverflow()->month;
  2347.     }
  2348.     /**
  2349.      * Determines if the instance is within the last month.
  2350.      *
  2351.      * @return bool
  2352.      */
  2353.     public function isLastMonth()
  2354.     {
  2355.         return $this->month === $this->nowWithSameTz()->subMonthNoOverflow()->month;
  2356.     }
  2357.     /**
  2358.      * Determines if the instance is within next year.
  2359.      *
  2360.      * @return bool
  2361.      */
  2362.     public function isNextYear()
  2363.     {
  2364.         return $this->year === $this->nowWithSameTz()->addYear()->year;
  2365.     }
  2366.     /**
  2367.      * Determines if the instance is within the previous year.
  2368.      *
  2369.      * @return bool
  2370.      */
  2371.     public function isLastYear()
  2372.     {
  2373.         return $this->year === $this->nowWithSameTz()->subYear()->year;
  2374.     }
  2375.     /**
  2376.      * Determines if the instance is in the future, ie. greater (after) than now.
  2377.      *
  2378.      * @return bool
  2379.      */
  2380.     public function isFuture()
  2381.     {
  2382.         return $this->gt($this->nowWithSameTz());
  2383.     }
  2384.     /**
  2385.      * Determines if the instance is in the past, ie. less (before) than now.
  2386.      *
  2387.      * @return bool
  2388.      */
  2389.     public function isPast()
  2390.     {
  2391.         return $this->lt($this->nowWithSameTz());
  2392.     }
  2393.     /**
  2394.      * Determines if the instance is a leap year.
  2395.      *
  2396.      * @return bool
  2397.      */
  2398.     public function isLeapYear()
  2399.     {
  2400.         return $this->format('L') === '1';
  2401.     }
  2402.     /**
  2403.      * Determines if the instance is a long year
  2404.      *
  2405.      * @see https://en.wikipedia.org/wiki/ISO_8601#Week_dates
  2406.      *
  2407.      * @return bool
  2408.      */
  2409.     public function isLongYear()
  2410.     {
  2411.         return static::create($this->year1228000$this->tz)->weekOfYear === 53;
  2412.     }
  2413.     /**
  2414.      * Compares the formatted values of the two dates.
  2415.      *
  2416.      * @param string                                 $format The date formats to compare.
  2417.      * @param \Carbon\Carbon|\DateTimeInterface|null $date   The instance to compare with or null to use current day.
  2418.      *
  2419.      * @throws \InvalidArgumentException
  2420.      *
  2421.      * @return bool
  2422.      */
  2423.     public function isSameAs($format$date null)
  2424.     {
  2425.         $date $date ?: static::now($this->tz);
  2426.         static::expectDateTime($date'null');
  2427.         return $this->format($format) === $date->format($format);
  2428.     }
  2429.     /**
  2430.      * Determines if the instance is in the current year.
  2431.      *
  2432.      * @return bool
  2433.      */
  2434.     public function isCurrentYear()
  2435.     {
  2436.         return $this->isSameYear();
  2437.     }
  2438.     /**
  2439.      * Checks if the passed in date is in the same year as the instance year.
  2440.      *
  2441.      * @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use current day.
  2442.      *
  2443.      * @return bool
  2444.      */
  2445.     public function isSameYear($date null)
  2446.     {
  2447.         return $this->isSameAs('Y'$date);
  2448.     }
  2449.     /**
  2450.      * Determines if the instance is in the current month.
  2451.      *
  2452.      * @return bool
  2453.      */
  2454.     public function isCurrentQuarter()
  2455.     {
  2456.         return $this->isSameQuarter();
  2457.     }
  2458.     /**
  2459.      * Checks if the passed in date is in the same quarter as the instance quarter (and year if needed).
  2460.      *
  2461.      * @param \Carbon\Carbon|\DateTimeInterface|null $date       The instance to compare with or null to use current day.
  2462.      * @param bool                                   $ofSameYear Check if it is the same month in the same year.
  2463.      *
  2464.      * @return bool
  2465.      */
  2466.     public function isSameQuarter($date null$ofSameYear null)
  2467.     {
  2468.         $date $date ? static::instance($date) : static::now($this->tz);
  2469.         static::expectDateTime($date'null');
  2470.         $ofSameYear is_null($ofSameYear) ? static::shouldCompareYearWithMonth() : $ofSameYear;
  2471.         return $this->quarter === $date->quarter && (!$ofSameYear || $this->isSameYear($date));
  2472.     }
  2473.     /**
  2474.      * Determines if the instance is in the current month.
  2475.      *
  2476.      * @param bool $ofSameYear Check if it is the same month in the same year.
  2477.      *
  2478.      * @return bool
  2479.      */
  2480.     public function isCurrentMonth($ofSameYear null)
  2481.     {
  2482.         return $this->isSameMonth(null$ofSameYear);
  2483.     }
  2484.     /**
  2485.      * Checks if the passed in date is in the same month as the instance´s month.
  2486.      *
  2487.      * Note that this defaults to only comparing the month while ignoring the year.
  2488.      * To test if it is the same exact month of the same year, pass in true as the second parameter.
  2489.      *
  2490.      * @param \Carbon\Carbon|\DateTimeInterface|null $date       The instance to compare with or null to use the current date.
  2491.      * @param bool                                   $ofSameYear Check if it is the same month in the same year.
  2492.      *
  2493.      * @return bool
  2494.      */
  2495.     public function isSameMonth($date null$ofSameYear null)
  2496.     {
  2497.         $ofSameYear is_null($ofSameYear) ? static::shouldCompareYearWithMonth() : $ofSameYear;
  2498.         return $this->isSameAs($ofSameYear 'Y-m' 'm'$date);
  2499.     }
  2500.     /**
  2501.      * Determines if the instance is in the current day.
  2502.      *
  2503.      * @return bool
  2504.      */
  2505.     public function isCurrentDay()
  2506.     {
  2507.         return $this->isSameDay();
  2508.     }
  2509.     /**
  2510.      * Checks if the passed in date is the same exact day as the instance´s day.
  2511.      *
  2512.      * @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use the current date.
  2513.      *
  2514.      * @return bool
  2515.      */
  2516.     public function isSameDay($date null)
  2517.     {
  2518.         return $this->isSameAs('Y-m-d'$date);
  2519.     }
  2520.     /**
  2521.      * Determines if the instance is in the current hour.
  2522.      *
  2523.      * @return bool
  2524.      */
  2525.     public function isCurrentHour()
  2526.     {
  2527.         return $this->isSameHour();
  2528.     }
  2529.     /**
  2530.      * Checks if the passed in date is the same exact hour as the instance´s hour.
  2531.      *
  2532.      * @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use the current date.
  2533.      *
  2534.      * @return bool
  2535.      */
  2536.     public function isSameHour($date null)
  2537.     {
  2538.         return $this->isSameAs('Y-m-d H'$date);
  2539.     }
  2540.     /**
  2541.      * Determines if the instance is in the current minute.
  2542.      *
  2543.      * @return bool
  2544.      */
  2545.     public function isCurrentMinute()
  2546.     {
  2547.         return $this->isSameMinute();
  2548.     }
  2549.     /**
  2550.      * Checks if the passed in date is the same exact minute as the instance´s minute.
  2551.      *
  2552.      * @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use the current date.
  2553.      *
  2554.      * @return bool
  2555.      */
  2556.     public function isSameMinute($date null)
  2557.     {
  2558.         return $this->isSameAs('Y-m-d H:i'$date);
  2559.     }
  2560.     /**
  2561.      * Determines if the instance is in the current second.
  2562.      *
  2563.      * @return bool
  2564.      */
  2565.     public function isCurrentSecond()
  2566.     {
  2567.         return $this->isSameSecond();
  2568.     }
  2569.     /**
  2570.      * Checks if the passed in date is the same exact second as the instance´s second.
  2571.      *
  2572.      * @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use the current date.
  2573.      *
  2574.      * @return bool
  2575.      */
  2576.     public function isSameSecond($date null)
  2577.     {
  2578.         return $this->isSameAs('Y-m-d H:i:s'$date);
  2579.     }
  2580.     /**
  2581.      * Checks if this day is a specific day of the week.
  2582.      *
  2583.      * @param int $dayOfWeek
  2584.      *
  2585.      * @return bool
  2586.      */
  2587.     public function isDayOfWeek($dayOfWeek)
  2588.     {
  2589.         return $this->dayOfWeek === $dayOfWeek;
  2590.     }
  2591.     /**
  2592.      * Checks if this day is a Sunday.
  2593.      *
  2594.      * @return bool
  2595.      */
  2596.     public function isSunday()
  2597.     {
  2598.         return $this->dayOfWeek === static::SUNDAY;
  2599.     }
  2600.     /**
  2601.      * Checks if this day is a Monday.
  2602.      *
  2603.      * @return bool
  2604.      */
  2605.     public function isMonday()
  2606.     {
  2607.         return $this->dayOfWeek === static::MONDAY;
  2608.     }
  2609.     /**
  2610.      * Checks if this day is a Tuesday.
  2611.      *
  2612.      * @return bool
  2613.      */
  2614.     public function isTuesday()
  2615.     {
  2616.         return $this->dayOfWeek === static::TUESDAY;
  2617.     }
  2618.     /**
  2619.      * Checks if this day is a Wednesday.
  2620.      *
  2621.      * @return bool
  2622.      */
  2623.     public function isWednesday()
  2624.     {
  2625.         return $this->dayOfWeek === static::WEDNESDAY;
  2626.     }
  2627.     /**
  2628.      * Checks if this day is a Thursday.
  2629.      *
  2630.      * @return bool
  2631.      */
  2632.     public function isThursday()
  2633.     {
  2634.         return $this->dayOfWeek === static::THURSDAY;
  2635.     }
  2636.     /**
  2637.      * Checks if this day is a Friday.
  2638.      *
  2639.      * @return bool
  2640.      */
  2641.     public function isFriday()
  2642.     {
  2643.         return $this->dayOfWeek === static::FRIDAY;
  2644.     }
  2645.     /**
  2646.      * Checks if this day is a Saturday.
  2647.      *
  2648.      * @return bool
  2649.      */
  2650.     public function isSaturday()
  2651.     {
  2652.         return $this->dayOfWeek === static::SATURDAY;
  2653.     }
  2654.     /**
  2655.      * Check if its the birthday. Compares the date/month values of the two dates.
  2656.      *
  2657.      * @param \Carbon\Carbon|\DateTimeInterface|null $date The instance to compare with or null to use current day.
  2658.      *
  2659.      * @return bool
  2660.      */
  2661.     public function isBirthday($date null)
  2662.     {
  2663.         return $this->isSameAs('md'$date);
  2664.     }
  2665.     /**
  2666.      * Check if today is the last day of the Month
  2667.      *
  2668.      * @return bool
  2669.      */
  2670.     public function isLastOfMonth()
  2671.     {
  2672.         return $this->day === $this->daysInMonth;
  2673.     }
  2674.     /**
  2675.      * Check if the instance is start of day / midnight.
  2676.      *
  2677.      * @param bool $checkMicroseconds check time at microseconds precision
  2678.      *                                /!\ Warning, this is not reliable with PHP < 7.1.4
  2679.      *
  2680.      * @return bool
  2681.      */
  2682.     public function isStartOfDay($checkMicroseconds false)
  2683.     {
  2684.         return $checkMicroseconds
  2685.             $this->format('H:i:s.u') === '00:00:00.000000'
  2686.             $this->format('H:i:s') === '00:00:00';
  2687.     }
  2688.     /**
  2689.      * Check if the instance is end of day.
  2690.      *
  2691.      * @param bool $checkMicroseconds check time at microseconds precision
  2692.      *                                /!\ Warning, this is not reliable with PHP < 7.1.4
  2693.      *
  2694.      * @return bool
  2695.      */
  2696.     public function isEndOfDay($checkMicroseconds false)
  2697.     {
  2698.         return $checkMicroseconds
  2699.             $this->format('H:i:s.u') === '23:59:59.999999'
  2700.             $this->format('H:i:s') === '23:59:59';
  2701.     }
  2702.     /**
  2703.      * Check if the instance is start of day / midnight.
  2704.      *
  2705.      * @return bool
  2706.      */
  2707.     public function isMidnight()
  2708.     {
  2709.         return $this->isStartOfDay();
  2710.     }
  2711.     /**
  2712.      * Check if the instance is midday.
  2713.      *
  2714.      * @return bool
  2715.      */
  2716.     public function isMidday()
  2717.     {
  2718.         return $this->format('G:i:s') === static::$midDayAt.':00:00';
  2719.     }
  2720.     /**
  2721.      * Checks if the (date)time string is in a given format.
  2722.      *
  2723.      * @param string $date
  2724.      * @param string $format
  2725.      *
  2726.      * @return bool
  2727.      */
  2728.     public static function hasFormat($date$format)
  2729.     {
  2730.         try {
  2731.             // Try to create a DateTime object. Throws an InvalidArgumentException if the provided time string
  2732.             // doesn't match the format in any way.
  2733.             static::createFromFormat($format$date);
  2734.             // createFromFormat() is known to handle edge cases silently.
  2735.             // E.g. "1975-5-1" (Y-n-j) will still be parsed correctly when "Y-m-d" is supplied as the format.
  2736.             // To ensure we're really testing against our desired format, perform an additional regex validation.
  2737.             $regex strtr(
  2738.                 preg_quote($format'/'),
  2739.                 static::$regexFormats
  2740.             );
  2741.             return (bool) preg_match('/^'.$regex.'$/'$date);
  2742.         } catch (InvalidArgumentException $e) {
  2743.         }
  2744.         return false;
  2745.     }
  2746.     ///////////////////////////////////////////////////////////////////
  2747.     /////////////////// ADDITIONS AND SUBTRACTIONS ////////////////////
  2748.     ///////////////////////////////////////////////////////////////////
  2749.     /**
  2750.      * Add centuries to the instance. Positive $value travels forward while
  2751.      * negative $value travels into the past.
  2752.      *
  2753.      * @param int $value
  2754.      *
  2755.      * @return static
  2756.      */
  2757.     public function addCenturies($value)
  2758.     {
  2759.         return $this->addYears(static::YEARS_PER_CENTURY $value);
  2760.     }
  2761.     /**
  2762.      * Add a century to the instance
  2763.      *
  2764.      * @param int $value
  2765.      *
  2766.      * @return static
  2767.      */
  2768.     public function addCentury($value 1)
  2769.     {
  2770.         return $this->addCenturies($value);
  2771.     }
  2772.     /**
  2773.      * Remove centuries from the instance
  2774.      *
  2775.      * @param int $value
  2776.      *
  2777.      * @return static
  2778.      */
  2779.     public function subCenturies($value)
  2780.     {
  2781.         return $this->addCenturies(-$value);
  2782.     }
  2783.     /**
  2784.      * Remove a century from the instance
  2785.      *
  2786.      * @param int $value
  2787.      *
  2788.      * @return static
  2789.      */
  2790.     public function subCentury($value 1)
  2791.     {
  2792.         return $this->subCenturies($value);
  2793.     }
  2794.     /**
  2795.      * Add years to the instance. Positive $value travel forward while
  2796.      * negative $value travel into the past.
  2797.      *
  2798.      * @param int $value
  2799.      *
  2800.      * @return static
  2801.      */
  2802.     public function addYears($value)
  2803.     {
  2804.         if ($this->shouldOverflowYears()) {
  2805.             return $this->addYearsWithOverflow($value);
  2806.         }
  2807.         return $this->addYearsNoOverflow($value);
  2808.     }
  2809.     /**
  2810.      * Add a year to the instance
  2811.      *
  2812.      * @param int $value
  2813.      *
  2814.      * @return static
  2815.      */
  2816.     public function addYear($value 1)
  2817.     {
  2818.         return $this->addYears($value);
  2819.     }
  2820.     /**
  2821.      * Add years to the instance with no overflow of months
  2822.      * Positive $value travel forward while
  2823.      * negative $value travel into the past.
  2824.      *
  2825.      * @param int $value
  2826.      *
  2827.      * @return static
  2828.      */
  2829.     public function addYearsNoOverflow($value)
  2830.     {
  2831.         return $this->addMonthsNoOverflow($value * static::MONTHS_PER_YEAR);
  2832.     }
  2833.     /**
  2834.      * Add year with overflow months set to false
  2835.      *
  2836.      * @param int $value
  2837.      *
  2838.      * @return static
  2839.      */
  2840.     public function addYearNoOverflow($value 1)
  2841.     {
  2842.         return $this->addYearsNoOverflow($value);
  2843.     }
  2844.     /**
  2845.      * Add years to the instance.
  2846.      * Positive $value travel forward while
  2847.      * negative $value travel into the past.
  2848.      *
  2849.      * @param int $value
  2850.      *
  2851.      * @return static
  2852.      */
  2853.     public function addYearsWithOverflow($value)
  2854.     {
  2855.         return $this->modify((int) $value.' year');
  2856.     }
  2857.     /**
  2858.      * Add year with overflow.
  2859.      *
  2860.      * @param int $value
  2861.      *
  2862.      * @return static
  2863.      */
  2864.     public function addYearWithOverflow($value 1)
  2865.     {
  2866.         return $this->addYearsWithOverflow($value);
  2867.     }
  2868.     /**
  2869.      * Remove years from the instance.
  2870.      *
  2871.      * @param int $value
  2872.      *
  2873.      * @return static
  2874.      */
  2875.     public function subYears($value)
  2876.     {
  2877.         return $this->addYears(-$value);
  2878.     }
  2879.     /**
  2880.      * Remove a year from the instance
  2881.      *
  2882.      * @param int $value
  2883.      *
  2884.      * @return static
  2885.      */
  2886.     public function subYear($value 1)
  2887.     {
  2888.         return $this->subYears($value);
  2889.     }
  2890.     /**
  2891.      * Remove years from the instance with no month overflow.
  2892.      *
  2893.      * @param int $value
  2894.      *
  2895.      * @return static
  2896.      */
  2897.     public function subYearsNoOverflow($value)
  2898.     {
  2899.         return $this->subMonthsNoOverflow($value * static::MONTHS_PER_YEAR);
  2900.     }
  2901.     /**
  2902.      * Remove year from the instance with no month overflow
  2903.      *
  2904.      * @param int $value
  2905.      *
  2906.      * @return static
  2907.      */
  2908.     public function subYearNoOverflow($value 1)
  2909.     {
  2910.         return $this->subYearsNoOverflow($value);
  2911.     }
  2912.     /**
  2913.      * Remove years from the instance.
  2914.      *
  2915.      * @param int $value
  2916.      *
  2917.      * @return static
  2918.      */
  2919.     public function subYearsWithOverflow($value)
  2920.     {
  2921.         return $this->subMonthsWithOverflow($value * static::MONTHS_PER_YEAR);
  2922.     }
  2923.     /**
  2924.      * Remove year from the instance.
  2925.      *
  2926.      * @param int $value
  2927.      *
  2928.      * @return static
  2929.      */
  2930.     public function subYearWithOverflow($value 1)
  2931.     {
  2932.         return $this->subYearsWithOverflow($value);
  2933.     }
  2934.     /**
  2935.      * Add quarters to the instance. Positive $value travels forward while
  2936.      * negative $value travels into the past.
  2937.      *
  2938.      * @param int $value
  2939.      *
  2940.      * @return static
  2941.      */
  2942.     public function addQuarters($value)
  2943.     {
  2944.         return $this->addMonths(static::MONTHS_PER_QUARTER $value);
  2945.     }
  2946.     /**
  2947.      * Add a quarter to the instance
  2948.      *
  2949.      * @param int $value
  2950.      *
  2951.      * @return static
  2952.      */
  2953.     public function addQuarter($value 1)
  2954.     {
  2955.         return $this->addQuarters($value);
  2956.     }
  2957.     /**
  2958.      * Remove quarters from the instance
  2959.      *
  2960.      * @param int $value
  2961.      *
  2962.      * @return static
  2963.      */
  2964.     public function subQuarters($value)
  2965.     {
  2966.         return $this->addQuarters(-$value);
  2967.     }
  2968.     /**
  2969.      * Remove a quarter from the instance
  2970.      *
  2971.      * @param int $value
  2972.      *
  2973.      * @return static
  2974.      */
  2975.     public function subQuarter($value 1)
  2976.     {
  2977.         return $this->subQuarters($value);
  2978.     }
  2979.     /**
  2980.      * Add months to the instance. Positive $value travels forward while
  2981.      * negative $value travels into the past.
  2982.      *
  2983.      * @param int $value
  2984.      *
  2985.      * @return static
  2986.      */
  2987.     public function addMonths($value)
  2988.     {
  2989.         if (static::shouldOverflowMonths()) {
  2990.             return $this->addMonthsWithOverflow($value);
  2991.         }
  2992.         return $this->addMonthsNoOverflow($value);
  2993.     }
  2994.     /**
  2995.      * Add a month to the instance
  2996.      *
  2997.      * @param int $value
  2998.      *
  2999.      * @return static
  3000.      */
  3001.     public function addMonth($value 1)
  3002.     {
  3003.         return $this->addMonths($value);
  3004.     }
  3005.     /**
  3006.      * Remove months from the instance
  3007.      *
  3008.      * @param int $value
  3009.      *
  3010.      * @return static
  3011.      */
  3012.     public function subMonths($value)
  3013.     {
  3014.         return $this->addMonths(-$value);
  3015.     }
  3016.     /**
  3017.      * Remove a month from the instance
  3018.      *
  3019.      * @param int $value
  3020.      *
  3021.      * @return static
  3022.      */
  3023.     public function subMonth($value 1)
  3024.     {
  3025.         return $this->subMonths($value);
  3026.     }
  3027.     /**
  3028.      * Add months to the instance. Positive $value travels forward while
  3029.      * negative $value travels into the past.
  3030.      *
  3031.      * @param int $value
  3032.      *
  3033.      * @return static
  3034.      */
  3035.     public function addMonthsWithOverflow($value)
  3036.     {
  3037.         return $this->modify((int) $value.' month');
  3038.     }
  3039.     /**
  3040.      * Add a month to the instance
  3041.      *
  3042.      * @param int $value
  3043.      *
  3044.      * @return static
  3045.      */
  3046.     public function addMonthWithOverflow($value 1)
  3047.     {
  3048.         return $this->addMonthsWithOverflow($value);
  3049.     }
  3050.     /**
  3051.      * Remove months from the instance
  3052.      *
  3053.      * @param int $value
  3054.      *
  3055.      * @return static
  3056.      */
  3057.     public function subMonthsWithOverflow($value)
  3058.     {
  3059.         return $this->addMonthsWithOverflow(-$value);
  3060.     }
  3061.     /**
  3062.      * Remove a month from the instance
  3063.      *
  3064.      * @param int $value
  3065.      *
  3066.      * @return static
  3067.      */
  3068.     public function subMonthWithOverflow($value 1)
  3069.     {
  3070.         return $this->subMonthsWithOverflow($value);
  3071.     }
  3072.     /**
  3073.      * Add months without overflowing to the instance. Positive $value
  3074.      * travels forward while negative $value travels into the past.
  3075.      *
  3076.      * @param int $value
  3077.      *
  3078.      * @return static
  3079.      */
  3080.     public function addMonthsNoOverflow($value)
  3081.     {
  3082.         $day $this->day;
  3083.         $this->modify((int) $value.' month');
  3084.         if ($day !== $this->day) {
  3085.             $this->modify('last day of previous month');
  3086.         }
  3087.         return $this;
  3088.     }
  3089.     /**
  3090.      * Add a month with no overflow to the instance
  3091.      *
  3092.      * @param int $value
  3093.      *
  3094.      * @return static
  3095.      */
  3096.     public function addMonthNoOverflow($value 1)
  3097.     {
  3098.         return $this->addMonthsNoOverflow($value);
  3099.     }
  3100.     /**
  3101.      * Remove months with no overflow from the instance
  3102.      *
  3103.      * @param int $value
  3104.      *
  3105.      * @return static
  3106.      */
  3107.     public function subMonthsNoOverflow($value)
  3108.     {
  3109.         return $this->addMonthsNoOverflow(-$value);
  3110.     }
  3111.     /**
  3112.      * Remove a month with no overflow from the instance
  3113.      *
  3114.      * @param int $value
  3115.      *
  3116.      * @return static
  3117.      */
  3118.     public function subMonthNoOverflow($value 1)
  3119.     {
  3120.         return $this->subMonthsNoOverflow($value);
  3121.     }
  3122.     /**
  3123.      * Add days to the instance. Positive $value travels forward while
  3124.      * negative $value travels into the past.
  3125.      *
  3126.      * @param int $value
  3127.      *
  3128.      * @return static
  3129.      */
  3130.     public function addDays($value)
  3131.     {
  3132.         return $this->modify((int) $value.' day');
  3133.     }
  3134.     /**
  3135.      * Add a day to the instance
  3136.      *
  3137.      * @param int $value
  3138.      *
  3139.      * @return static
  3140.      */
  3141.     public function addDay($value 1)
  3142.     {
  3143.         return $this->addDays($value);
  3144.     }
  3145.     /**
  3146.      * Remove days from the instance
  3147.      *
  3148.      * @param int $value
  3149.      *
  3150.      * @return static
  3151.      */
  3152.     public function subDays($value)
  3153.     {
  3154.         return $this->addDays(-$value);
  3155.     }
  3156.     /**
  3157.      * Remove a day from the instance
  3158.      *
  3159.      * @param int $value
  3160.      *
  3161.      * @return static
  3162.      */
  3163.     public function subDay($value 1)
  3164.     {
  3165.         return $this->subDays($value);
  3166.     }
  3167.     /**
  3168.      * Add weekdays to the instance. Positive $value travels forward while
  3169.      * negative $value travels into the past.
  3170.      *
  3171.      * @param int $value
  3172.      *
  3173.      * @return static
  3174.      */
  3175.     public function addWeekdays($value)
  3176.     {
  3177.         // Fix for weekday bug https://bugs.php.net/bug.php?id=54909
  3178.         $t $this->toTimeString();
  3179.         $this->modify((int) $value.' weekday');
  3180.         return $this->setTimeFromTimeString($t);
  3181.     }
  3182.     /**
  3183.      * Add a weekday to the instance
  3184.      *
  3185.      * @param int $value
  3186.      *
  3187.      * @return static
  3188.      */
  3189.     public function addWeekday($value 1)
  3190.     {
  3191.         return $this->addWeekdays($value);
  3192.     }
  3193.     /**
  3194.      * Remove weekdays from the instance
  3195.      *
  3196.      * @param int $value
  3197.      *
  3198.      * @return static
  3199.      */
  3200.     public function subWeekdays($value)
  3201.     {
  3202.         return $this->addWeekdays(-$value);
  3203.     }
  3204.     /**
  3205.      * Remove a weekday from the instance
  3206.      *
  3207.      * @param int $value
  3208.      *
  3209.      * @return static
  3210.      */
  3211.     public function subWeekday($value 1)
  3212.     {
  3213.         return $this->subWeekdays($value);
  3214.     }
  3215.     /**
  3216.      * Add weeks to the instance. Positive $value travels forward while
  3217.      * negative $value travels into the past.
  3218.      *
  3219.      * @param int $value
  3220.      *
  3221.      * @return static
  3222.      */
  3223.     public function addWeeks($value)
  3224.     {
  3225.         return $this->modify((int) $value.' week');
  3226.     }
  3227.     /**
  3228.      * Add a week to the instance
  3229.      *
  3230.      * @param int $value
  3231.      *
  3232.      * @return static
  3233.      */
  3234.     public function addWeek($value 1)
  3235.     {
  3236.         return $this->addWeeks($value);
  3237.     }
  3238.     /**
  3239.      * Remove weeks to the instance
  3240.      *
  3241.      * @param int $value
  3242.      *
  3243.      * @return static
  3244.      */
  3245.     public function subWeeks($value)
  3246.     {
  3247.         return $this->addWeeks(-$value);
  3248.     }
  3249.     /**
  3250.      * Remove a week from the instance
  3251.      *
  3252.      * @param int $value
  3253.      *
  3254.      * @return static
  3255.      */
  3256.     public function subWeek($value 1)
  3257.     {
  3258.         return $this->subWeeks($value);
  3259.     }
  3260.     /**
  3261.      * Add hours to the instance. Positive $value travels forward while
  3262.      * negative $value travels into the past.
  3263.      *
  3264.      * @param int $value
  3265.      *
  3266.      * @return static
  3267.      */
  3268.     public function addHours($value)
  3269.     {
  3270.         return $this->modify((int) $value.' hour');
  3271.     }
  3272.     /**
  3273.      * Add hours to the instance using timestamp. Positive $value travels
  3274.      * forward while negative $value travels into the past.
  3275.      *
  3276.      * @param int $value
  3277.      *
  3278.      * @return static
  3279.      */
  3280.     public function addRealHours($value)
  3281.     {
  3282.         return $this->addRealMinutes($value * static::MINUTES_PER_HOUR);
  3283.     }
  3284.     /**
  3285.      * Add an hour to the instance.
  3286.      *
  3287.      * @param int $value
  3288.      *
  3289.      * @return static
  3290.      */
  3291.     public function addHour($value 1)
  3292.     {
  3293.         return $this->addHours($value);
  3294.     }
  3295.     /**
  3296.      * Add an hour to the instance using timestamp.
  3297.      *
  3298.      * @param int $value
  3299.      *
  3300.      * @return static
  3301.      */
  3302.     public function addRealHour($value 1)
  3303.     {
  3304.         return $this->addRealHours($value);
  3305.     }
  3306.     /**
  3307.      * Remove hours from the instance.
  3308.      *
  3309.      * @param int $value
  3310.      *
  3311.      * @return static
  3312.      */
  3313.     public function subHours($value)
  3314.     {
  3315.         return $this->addHours(-$value);
  3316.     }
  3317.     /**
  3318.      * Remove hours from the instance using timestamp.
  3319.      *
  3320.      * @param int $value
  3321.      *
  3322.      * @return static
  3323.      */
  3324.     public function subRealHours($value)
  3325.     {
  3326.         return $this->addRealHours(-$value);
  3327.     }
  3328.     /**
  3329.      * Remove an hour from the instance.
  3330.      *
  3331.      * @param int $value
  3332.      *
  3333.      * @return static
  3334.      */
  3335.     public function subHour($value 1)
  3336.     {
  3337.         return $this->subHours($value);
  3338.     }
  3339.     /**
  3340.      * Remove an hour from the instance.
  3341.      *
  3342.      * @param int $value
  3343.      *
  3344.      * @return static
  3345.      */
  3346.     public function subRealHour($value 1)
  3347.     {
  3348.         return $this->subRealHours($value);
  3349.     }
  3350.     /**
  3351.      * Add minutes to the instance using timestamp. Positive $value
  3352.      * travels forward while negative $value travels into the past.
  3353.      *
  3354.      * @param int $value
  3355.      *
  3356.      * @return static
  3357.      */
  3358.     public function addMinutes($value)
  3359.     {
  3360.         return $this->modify((int) $value.' minute');
  3361.     }
  3362.     /**
  3363.      * Add minutes to the instance using timestamp. Positive $value travels
  3364.      * forward while negative $value travels into the past.
  3365.      *
  3366.      * @param int $value
  3367.      *
  3368.      * @return static
  3369.      */
  3370.     public function addRealMinutes($value)
  3371.     {
  3372.         return $this->addRealSeconds($value * static::SECONDS_PER_MINUTE);
  3373.     }
  3374.     /**
  3375.      * Add a minute to the instance.
  3376.      *
  3377.      * @param int $value
  3378.      *
  3379.      * @return static
  3380.      */
  3381.     public function addMinute($value 1)
  3382.     {
  3383.         return $this->addMinutes($value);
  3384.     }
  3385.     /**
  3386.      * Add a minute to the instance using timestamp.
  3387.      *
  3388.      * @param int $value
  3389.      *
  3390.      * @return static
  3391.      */
  3392.     public function addRealMinute($value 1)
  3393.     {
  3394.         return $this->addRealMinutes($value);
  3395.     }
  3396.     /**
  3397.      * Remove a minute from the instance.
  3398.      *
  3399.      * @param int $value
  3400.      *
  3401.      * @return static
  3402.      */
  3403.     public function subMinute($value 1)
  3404.     {
  3405.         return $this->subMinutes($value);
  3406.     }
  3407.     /**
  3408.      * Remove a minute from the instance using timestamp.
  3409.      *
  3410.      * @param int $value
  3411.      *
  3412.      * @return static
  3413.      */
  3414.     public function subRealMinute($value 1)
  3415.     {
  3416.         return $this->addRealMinutes(-$value);
  3417.     }
  3418.     /**
  3419.      * Remove minutes from the instance.
  3420.      *
  3421.      * @param int $value
  3422.      *
  3423.      * @return static
  3424.      */
  3425.     public function subMinutes($value)
  3426.     {
  3427.         return $this->addMinutes(-$value);
  3428.     }
  3429.     /**
  3430.      * Remove a minute from the instance using timestamp.
  3431.      *
  3432.      * @param int $value
  3433.      *
  3434.      * @return static
  3435.      */
  3436.     public function subRealMinutes($value 1)
  3437.     {
  3438.         return $this->subRealMinute($value);
  3439.     }
  3440.     /**
  3441.      * Add seconds to the instance. Positive $value travels forward while
  3442.      * negative $value travels into the past.
  3443.      *
  3444.      * @param int $value
  3445.      *
  3446.      * @return static
  3447.      */
  3448.     public function addSeconds($value)
  3449.     {
  3450.         return $this->modify((int) $value.' second');
  3451.     }
  3452.     /**
  3453.      * Add seconds to the instance using timestamp. Positive $value travels
  3454.      * forward while negative $value travels into the past.
  3455.      *
  3456.      * @param int $value
  3457.      *
  3458.      * @return static
  3459.      */
  3460.     public function addRealSeconds($value)
  3461.     {
  3462.         return $this->setTimestamp($this->getTimestamp() + $value);
  3463.     }
  3464.     /**
  3465.      * Add a second to the instance.
  3466.      *
  3467.      * @param int $value
  3468.      *
  3469.      * @return static
  3470.      */
  3471.     public function addSecond($value 1)
  3472.     {
  3473.         return $this->addSeconds($value);
  3474.     }
  3475.     /**
  3476.      * Add a second to the instance using timestamp.
  3477.      *
  3478.      * @param int $value
  3479.      *
  3480.      * @return static
  3481.      */
  3482.     public function addRealSecond($value 1)
  3483.     {
  3484.         return $this->addRealSeconds($value);
  3485.     }
  3486.     /**
  3487.      * Remove seconds from the instance.
  3488.      *
  3489.      * @param int $value
  3490.      *
  3491.      * @return static
  3492.      */
  3493.     public function subSeconds($value)
  3494.     {
  3495.         return $this->addSeconds(-$value);
  3496.     }
  3497.     /**
  3498.      * Remove seconds from the instance using timestamp.
  3499.      *
  3500.      * @param int $value
  3501.      *
  3502.      * @return static
  3503.      */
  3504.     public function subRealSeconds($value)
  3505.     {
  3506.         return $this->addRealSeconds(-$value);
  3507.     }
  3508.     /**
  3509.      * Remove a second from the instance
  3510.      *
  3511.      * @param int $value
  3512.      *
  3513.      * @return static
  3514.      */
  3515.     public function subSecond($value 1)
  3516.     {
  3517.         return $this->subSeconds($value);
  3518.     }
  3519.     /**
  3520.      * Remove a second from the instance using timestamp.
  3521.      *
  3522.      * @param int $value
  3523.      *
  3524.      * @return static
  3525.      */
  3526.     public function subRealSecond($value 1)
  3527.     {
  3528.         return $this->subRealSeconds($value);
  3529.     }
  3530.     ///////////////////////////////////////////////////////////////////
  3531.     /////////////////////////// DIFFERENCES ///////////////////////////
  3532.     ///////////////////////////////////////////////////////////////////
  3533.     /**
  3534.      * @param DateInterval $diff
  3535.      * @param bool         $absolute
  3536.      * @param bool         $trimMicroseconds
  3537.      *
  3538.      * @return CarbonInterval
  3539.      */
  3540.     protected static function fixDiffInterval(DateInterval $diff$absolute$trimMicroseconds)
  3541.     {
  3542.         $diff CarbonInterval::instance($diff$trimMicroseconds);
  3543.         // @codeCoverageIgnoreStart
  3544.         if (version_compare(PHP_VERSION'7.1.0-dev''<')) {
  3545.             return $diff;
  3546.         }
  3547.         // Work-around for https://bugs.php.net/bug.php?id=77145
  3548.         if ($diff->&& $diff->=== -&& $diff->=== 11 && $diff->>= 27 && $diff->=== 23 && $diff->=== 59 && $diff->=== 59) {
  3549.             $diff->0;
  3550.             $diff->0;
  3551.             $diff->0;
  3552.             $diff->0;
  3553.             $diff->0;
  3554.             $diff->0;
  3555.             $diff->= (1000000 round($diff->1000000)) / 1000000;
  3556.             $diff->invert();
  3557.         } elseif ($diff->0) {
  3558.             if ($diff->!== || $diff->!== || $diff->!== || $diff->!== || $diff->!== || $diff->!== 0) {
  3559.                 $diff->= (round($diff->1000000) + 1000000) / 1000000;
  3560.                 $diff->s--;
  3561.                 if ($diff->0) {
  3562.                     $diff->+= 60;
  3563.                     $diff->i--;
  3564.                     if ($diff->0) {
  3565.                         $diff->+= 60;
  3566.                         $diff->h--;
  3567.                         if ($diff->0) {
  3568.                             $diff->+= 24;
  3569.                             $diff->d--;
  3570.                             if ($diff->0) {
  3571.                                 $diff->+= 30;
  3572.                                 $diff->m--;
  3573.                                 if ($diff->0) {
  3574.                                     $diff->+= 12;
  3575.                                     $diff->y--;
  3576.                                 }
  3577.                             }
  3578.                         }
  3579.                     }
  3580.                 }
  3581.             } else {
  3582.                 $diff->*= -1;
  3583.                 $diff->invert();
  3584.             }
  3585.         }
  3586.         // @codeCoverageIgnoreEnd
  3587.         if ($absolute && $diff->invert) {
  3588.             $diff->invert();
  3589.         }
  3590.         return $diff;
  3591.     }
  3592.     /**
  3593.      * Get the difference as a CarbonInterval instance.
  3594.      *
  3595.      * Pass false as second argument to get a microseconds-precise interval. Else
  3596.      * microseconds in the original interval will not be kept.
  3597.      *
  3598.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3599.      * @param bool                                          $absolute         Get the absolute of the difference
  3600.      * @param bool                                          $trimMicroseconds (true by default)
  3601.      *
  3602.      * @return CarbonInterval
  3603.      */
  3604.     public function diffAsCarbonInterval($date null$absolute true$trimMicroseconds true)
  3605.     {
  3606.         $from $this;
  3607.         $to $this->resolveCarbon($date);
  3608.         if ($trimMicroseconds) {
  3609.             $from $from->copy()->startOfSecond();
  3610.             $to $to->copy()->startOfSecond();
  3611.         }
  3612.         return static::fixDiffInterval($from->diff($to$absolute), $absolute$trimMicroseconds);
  3613.     }
  3614.     /**
  3615.      * Get the difference in years
  3616.      *
  3617.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3618.      * @param bool                                          $absolute Get the absolute of the difference
  3619.      *
  3620.      * @return int
  3621.      */
  3622.     public function diffInYears($date null$absolute true)
  3623.     {
  3624.         return (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%y');
  3625.     }
  3626.     /**
  3627.      * Get the difference in months
  3628.      *
  3629.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3630.      * @param bool                                          $absolute Get the absolute of the difference
  3631.      *
  3632.      * @return int
  3633.      */
  3634.     public function diffInMonths($date null$absolute true)
  3635.     {
  3636.         $date $this->resolveCarbon($date);
  3637.         return $this->diffInYears($date$absolute) * static::MONTHS_PER_YEAR + (int) $this->diff($date$absolute)->format('%r%m');
  3638.     }
  3639.     /**
  3640.      * Get the difference in weeks
  3641.      *
  3642.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3643.      * @param bool                                          $absolute Get the absolute of the difference
  3644.      *
  3645.      * @return int
  3646.      */
  3647.     public function diffInWeeks($date null$absolute true)
  3648.     {
  3649.         return (int) ($this->diffInDays($date$absolute) / static::DAYS_PER_WEEK);
  3650.     }
  3651.     /**
  3652.      * Get the difference in days
  3653.      *
  3654.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3655.      * @param bool                                          $absolute Get the absolute of the difference
  3656.      *
  3657.      * @return int
  3658.      */
  3659.     public function diffInDays($date null$absolute true)
  3660.     {
  3661.         return (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%a');
  3662.     }
  3663.     /**
  3664.      * Get the difference in days using a filter closure
  3665.      *
  3666.      * @param Closure                                       $callback
  3667.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3668.      * @param bool                                          $absolute Get the absolute of the difference
  3669.      *
  3670.      * @return int
  3671.      */
  3672.     public function diffInDaysFiltered(Closure $callback$date null$absolute true)
  3673.     {
  3674.         return $this->diffFiltered(CarbonInterval::day(), $callback$date$absolute);
  3675.     }
  3676.     /**
  3677.      * Get the difference in hours using a filter closure
  3678.      *
  3679.      * @param Closure                                       $callback
  3680.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3681.      * @param bool                                          $absolute Get the absolute of the difference
  3682.      *
  3683.      * @return int
  3684.      */
  3685.     public function diffInHoursFiltered(Closure $callback$date null$absolute true)
  3686.     {
  3687.         return $this->diffFiltered(CarbonInterval::hour(), $callback$date$absolute);
  3688.     }
  3689.     /**
  3690.      * Get the difference by the given interval using a filter closure
  3691.      *
  3692.      * @param CarbonInterval                                $ci       An interval to traverse by
  3693.      * @param Closure                                       $callback
  3694.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3695.      * @param bool                                          $absolute Get the absolute of the difference
  3696.      *
  3697.      * @return int
  3698.      */
  3699.     public function diffFiltered(CarbonInterval $ciClosure $callback$date null$absolute true)
  3700.     {
  3701.         $start $this;
  3702.         $end $this->resolveCarbon($date);
  3703.         $inverse false;
  3704.         if ($end $start) {
  3705.             $start $end;
  3706.             $end $this;
  3707.             $inverse true;
  3708.         }
  3709.         $period = new DatePeriod($start$ci$end);
  3710.         $values array_filter(iterator_to_array($period), function ($date) use ($callback) {
  3711.             return call_user_func($callbackCarbon::instance($date));
  3712.         });
  3713.         $diff count($values);
  3714.         return $inverse && !$absolute ? -$diff $diff;
  3715.     }
  3716.     /**
  3717.      * Get the difference in weekdays
  3718.      *
  3719.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3720.      * @param bool                                          $absolute Get the absolute of the difference
  3721.      *
  3722.      * @return int
  3723.      */
  3724.     public function diffInWeekdays($date null$absolute true)
  3725.     {
  3726.         return $this->diffInDaysFiltered(function (Carbon $date) {
  3727.             return $date->isWeekday();
  3728.         }, $date$absolute);
  3729.     }
  3730.     /**
  3731.      * Get the difference in weekend days using a filter
  3732.      *
  3733.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3734.      * @param bool                                          $absolute Get the absolute of the difference
  3735.      *
  3736.      * @return int
  3737.      */
  3738.     public function diffInWeekendDays($date null$absolute true)
  3739.     {
  3740.         return $this->diffInDaysFiltered(function (Carbon $date) {
  3741.             return $date->isWeekend();
  3742.         }, $date$absolute);
  3743.     }
  3744.     /**
  3745.      * Get the difference in hours.
  3746.      *
  3747.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3748.      * @param bool                                          $absolute Get the absolute of the difference
  3749.      *
  3750.      * @return int
  3751.      */
  3752.     public function diffInHours($date null$absolute true)
  3753.     {
  3754.         return (int) ($this->diffInSeconds($date$absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR);
  3755.     }
  3756.     /**
  3757.      * Get the difference in hours using timestamps.
  3758.      *
  3759.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3760.      * @param bool                                          $absolute Get the absolute of the difference
  3761.      *
  3762.      * @return int
  3763.      */
  3764.     public function diffInRealHours($date null$absolute true)
  3765.     {
  3766.         return (int) ($this->diffInRealSeconds($date$absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR);
  3767.     }
  3768.     /**
  3769.      * Get the difference in minutes.
  3770.      *
  3771.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3772.      * @param bool                                          $absolute Get the absolute of the difference
  3773.      *
  3774.      * @return int
  3775.      */
  3776.     public function diffInMinutes($date null$absolute true)
  3777.     {
  3778.         return (int) ($this->diffInSeconds($date$absolute) / static::SECONDS_PER_MINUTE);
  3779.     }
  3780.     /**
  3781.      * Get the difference in minutes using timestamps.
  3782.      *
  3783.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3784.      * @param bool                                          $absolute Get the absolute of the difference
  3785.      *
  3786.      * @return int
  3787.      */
  3788.     public function diffInRealMinutes($date null$absolute true)
  3789.     {
  3790.         return (int) ($this->diffInRealSeconds($date$absolute) / static::SECONDS_PER_MINUTE);
  3791.     }
  3792.     /**
  3793.      * Get the difference in seconds.
  3794.      *
  3795.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3796.      * @param bool                                          $absolute Get the absolute of the difference
  3797.      *
  3798.      * @return int
  3799.      */
  3800.     public function diffInSeconds($date null$absolute true)
  3801.     {
  3802.         $diff $this->diff($this->resolveCarbon($date));
  3803.         if (!$diff->days && version_compare(PHP_VERSION'5.4.0-dev''>=')) {
  3804.             $diff = static::fixDiffInterval($diff$absolutefalse);
  3805.         }
  3806.         $value $diff->days * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE +
  3807.             $diff->* static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE +
  3808.             $diff->* static::SECONDS_PER_MINUTE +
  3809.             $diff->s;
  3810.         return $absolute || !$diff->invert $value : -$value;
  3811.     }
  3812.     /**
  3813.      * Get the difference in seconds using timestamps.
  3814.      *
  3815.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3816.      * @param bool                                          $absolute Get the absolute of the difference
  3817.      *
  3818.      * @return int
  3819.      */
  3820.     public function diffInRealSeconds($date null$absolute true)
  3821.     {
  3822.         $date $this->resolveCarbon($date);
  3823.         $value $date->getTimestamp() - $this->getTimestamp();
  3824.         return $absolute abs($value) : $value;
  3825.     }
  3826.     /**
  3827.      * Get the difference in milliseconds.
  3828.      *
  3829.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3830.      * @param bool                                          $absolute Get the absolute of the difference
  3831.      *
  3832.      * @return int
  3833.      */
  3834.     public function diffInMilliseconds($date null$absolute true)
  3835.     {
  3836.         return (int) ($this->diffInMicroseconds($date$absolute) / static::MICROSECONDS_PER_MILLISECOND);
  3837.     }
  3838.     /**
  3839.      * Get the difference in milliseconds using timestamps.
  3840.      *
  3841.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3842.      * @param bool                                          $absolute Get the absolute of the difference
  3843.      *
  3844.      * @return int
  3845.      */
  3846.     public function diffInRealMilliseconds($date null$absolute true)
  3847.     {
  3848.         return (int) ($this->diffInRealMicroseconds($date$absolute) / static::MICROSECONDS_PER_MILLISECOND);
  3849.     }
  3850.     /**
  3851.      * Get the difference in microseconds.
  3852.      *
  3853.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3854.      * @param bool                                          $absolute Get the absolute of the difference
  3855.      *
  3856.      * @return int
  3857.      */
  3858.     public function diffInMicroseconds($date null$absolute true)
  3859.     {
  3860.         $diff $this->diff($this->resolveCarbon($date));
  3861.         $micro = isset($diff->f) ? $diff->0;
  3862.         $value = (int) round((((($diff->days * static::HOURS_PER_DAY) +
  3863.             $diff->h) * static::MINUTES_PER_HOUR +
  3864.             $diff->i) * static::SECONDS_PER_MINUTE +
  3865.             ($micro $diff->s)) * static::MICROSECONDS_PER_SECOND);
  3866.         return $absolute || !$diff->invert $value : -$value;
  3867.     }
  3868.     /**
  3869.      * Get the difference in microseconds using timestamps.
  3870.      *
  3871.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  3872.      * @param bool                                          $absolute Get the absolute of the difference
  3873.      *
  3874.      * @return int
  3875.      */
  3876.     public function diffInRealMicroseconds($date null$absolute true)
  3877.     {
  3878.         /** @var Carbon $date */
  3879.         $date $this->resolveCarbon($date);
  3880.         $value = ($date->timestamp $this->timestamp) * static::MICROSECONDS_PER_SECOND +
  3881.             $date->micro $this->micro;
  3882.         return $absolute abs($value) : $value;
  3883.     }
  3884.     /**
  3885.      * The number of seconds since midnight.
  3886.      *
  3887.      * @return int
  3888.      */
  3889.     public function secondsSinceMidnight()
  3890.     {
  3891.         return $this->diffInSeconds($this->copy()->startOfDay());
  3892.     }
  3893.     /**
  3894.      * The number of seconds until 23:59:59.
  3895.      *
  3896.      * @return int
  3897.      */
  3898.     public function secondsUntilEndOfDay()
  3899.     {
  3900.         return $this->diffInSeconds($this->copy()->endOfDay());
  3901.     }
  3902.     /**
  3903.      * Get the difference in a human readable format in the current locale.
  3904.      *
  3905.      * When comparing a value in the past to default now:
  3906.      * 1 hour ago
  3907.      * 5 months ago
  3908.      *
  3909.      * When comparing a value in the future to default now:
  3910.      * 1 hour from now
  3911.      * 5 months from now
  3912.      *
  3913.      * When comparing a value in the past to another value:
  3914.      * 1 hour before
  3915.      * 5 months before
  3916.      *
  3917.      * When comparing a value in the future to another value:
  3918.      * 1 hour after
  3919.      * 5 months after
  3920.      *
  3921.      * @param Carbon|null $other
  3922.      * @param bool        $absolute removes time difference modifiers ago, after, etc
  3923.      * @param bool        $short    displays short format of time units
  3924.      * @param int         $parts    displays number of parts in the interval
  3925.      *
  3926.      * @return string
  3927.      */
  3928.     public function diffForHumans($other null$absolute false$short false$parts 1)
  3929.     {
  3930.         $isNow $other === null;
  3931.         $relativeToNow $isNow;
  3932.         if ($absolute === static::DIFF_RELATIVE_TO_NOW) {
  3933.             $absolute false;
  3934.             $relativeToNow true;
  3935.         } elseif ($absolute === static::DIFF_RELATIVE_TO_OTHER) {
  3936.             $absolute false;
  3937.             $relativeToNow false;
  3938.         }
  3939.         $interval = array();
  3940.         $parts min(6max(1, (int) $parts));
  3941.         $count 1;
  3942.         $unit $short 's' 'second';
  3943.         if ($isNow) {
  3944.             $other $this->nowWithSameTz();
  3945.         } elseif (!$other instanceof DateTime && !$other instanceof DateTimeInterface) {
  3946.             $other = static::parse($other);
  3947.         }
  3948.         $diffInterval $this->diff($other);
  3949.         $diffIntervalArray = array(
  3950.             array('value' => $diffInterval->y'unit' => 'year',    'unitShort' => 'y'),
  3951.             array('value' => $diffInterval->m'unit' => 'month',   'unitShort' => 'm'),
  3952.             array('value' => $diffInterval->d'unit' => 'day',     'unitShort' => 'd'),
  3953.             array('value' => $diffInterval->h'unit' => 'hour',    'unitShort' => 'h'),
  3954.             array('value' => $diffInterval->i'unit' => 'minute',  'unitShort' => 'min'),
  3955.             array('value' => $diffInterval->s'unit' => 'second',  'unitShort' => 's'),
  3956.         );
  3957.         foreach ($diffIntervalArray as $diffIntervalData) {
  3958.             if ($diffIntervalData['value'] > 0) {
  3959.                 $unit $short $diffIntervalData['unitShort'] : $diffIntervalData['unit'];
  3960.                 $count $diffIntervalData['value'];
  3961.                 if ($diffIntervalData['unit'] === 'day' && $count >= static::DAYS_PER_WEEK) {
  3962.                     $unit $short 'w' 'week';
  3963.                     $count = (int) ($count / static::DAYS_PER_WEEK);
  3964.                     $interval[] = static::translator()->transChoice($unit$count, array(':count' => $count));
  3965.                     // get the count days excluding weeks (might be zero)
  3966.                     $numOfDaysCount = (int) ($diffIntervalData['value'] - ($count * static::DAYS_PER_WEEK));
  3967.                     if ($numOfDaysCount && count($interval) < $parts) {
  3968.                         $unit $short 'd' 'day';
  3969.                         $count $numOfDaysCount;
  3970.                         $interval[] = static::translator()->transChoice($unit$count, array(':count' => $count));
  3971.                     }
  3972.                 } else {
  3973.                     $interval[] = static::translator()->transChoice($unit$count, array(':count' => $count));
  3974.                 }
  3975.             }
  3976.             // break the loop after we get the required number of parts in array
  3977.             if (count($interval) >= $parts) {
  3978.                 break;
  3979.             }
  3980.         }
  3981.         if (count($interval) === 0) {
  3982.             if ($isNow && static::getHumanDiffOptions() & self::JUST_NOW) {
  3983.                 $key 'diff_now';
  3984.                 $translation = static::translator()->trans($key);
  3985.                 if ($translation !== $key) {
  3986.                     return $translation;
  3987.                 }
  3988.             }
  3989.             $count = static::getHumanDiffOptions() & self::NO_ZERO_DIFF 0;
  3990.             $unit $short 's' 'second';
  3991.             $interval[] = static::translator()->transChoice($unit$count, array(':count' => $count));
  3992.         }
  3993.         // join the interval parts by a space
  3994.         $time implode(' '$interval);
  3995.         unset($diffIntervalArray$interval);
  3996.         if ($absolute) {
  3997.             return $time;
  3998.         }
  3999.         $isFuture $diffInterval->invert === 1;
  4000.         $transId $relativeToNow ? ($isFuture 'from_now' 'ago') : ($isFuture 'after' 'before');
  4001.         if ($parts === 1) {
  4002.             if ($isNow && $unit === 'day') {
  4003.                 if ($count === && static::getHumanDiffOptions() & self::ONE_DAY_WORDS) {
  4004.                     $key $isFuture 'diff_tomorrow' 'diff_yesterday';
  4005.                     $translation = static::translator()->trans($key);
  4006.                     if ($translation !== $key) {
  4007.                         return $translation;
  4008.                     }
  4009.                 }
  4010.                 if ($count === && static::getHumanDiffOptions() & self::TWO_DAY_WORDS) {
  4011.                     $key $isFuture 'diff_after_tomorrow' 'diff_before_yesterday';
  4012.                     $translation = static::translator()->trans($key);
  4013.                     if ($translation !== $key) {
  4014.                         return $translation;
  4015.                     }
  4016.                 }
  4017.             }
  4018.             // Some languages have special pluralization for past and future tense.
  4019.             $key $unit.'_'.$transId;
  4020.             if ($key !== static::translator()->transChoice($key$count)) {
  4021.                 $time = static::translator()->transChoice($key$count, array(':count' => $count));
  4022.             }
  4023.         }
  4024.         return static::translator()->trans($transId, array(':time' => $time));
  4025.     }
  4026.     /**
  4027.      * @alias diffForHumans
  4028.      *
  4029.      * Get the difference in a human readable format in the current locale.
  4030.      *
  4031.      * When comparing a value in the past to default now:
  4032.      * 1 hour ago
  4033.      * 5 months ago
  4034.      *
  4035.      * When comparing a value in the future to default now:
  4036.      * 1 hour from now
  4037.      * 5 months from now
  4038.      *
  4039.      * When comparing a value in the past to another value:
  4040.      * 1 hour before
  4041.      * 5 months before
  4042.      *
  4043.      * When comparing a value in the future to another value:
  4044.      * 1 hour after
  4045.      * 5 months after
  4046.      *
  4047.      * @param Carbon|null $other
  4048.      * @param bool        $absolute removes time difference modifiers ago, after, etc
  4049.      * @param bool        $short    displays short format of time units
  4050.      * @param int         $parts    displays number of parts in the interval
  4051.      *
  4052.      * @return string
  4053.      */
  4054.     public function from($other null$absolute false$short false$parts 1)
  4055.     {
  4056.         if (!$other && !$absolute) {
  4057.             $absolute = static::DIFF_RELATIVE_TO_NOW;
  4058.         }
  4059.         return $this->diffForHumans($other$absolute$short$parts);
  4060.     }
  4061.     /**
  4062.      * @alias diffForHumans
  4063.      *
  4064.      * Get the difference in a human readable format in the current locale.
  4065.      *
  4066.      * When comparing a value in the past to default now:
  4067.      * 1 hour ago
  4068.      * 5 months ago
  4069.      *
  4070.      * When comparing a value in the future to default now:
  4071.      * 1 hour from now
  4072.      * 5 months from now
  4073.      *
  4074.      * When comparing a value in the past to another value:
  4075.      * 1 hour before
  4076.      * 5 months before
  4077.      *
  4078.      * When comparing a value in the future to another value:
  4079.      * 1 hour after
  4080.      * 5 months after
  4081.      *
  4082.      * @param Carbon|null $other
  4083.      * @param bool        $absolute removes time difference modifiers ago, after, etc
  4084.      * @param bool        $short    displays short format of time units
  4085.      * @param int         $parts    displays number of parts in the interval
  4086.      *
  4087.      * @return string
  4088.      */
  4089.     public function since($other null$absolute false$short false$parts 1)
  4090.     {
  4091.         return $this->diffForHumans($other$absolute$short$parts);
  4092.     }
  4093.     /**
  4094.      * Get the difference in a human readable format in the current locale from an other
  4095.      * instance given (or now if null given) to current instance.
  4096.      *
  4097.      * When comparing a value in the past to default now:
  4098.      * 1 hour from now
  4099.      * 5 months from now
  4100.      *
  4101.      * When comparing a value in the future to default now:
  4102.      * 1 hour ago
  4103.      * 5 months ago
  4104.      *
  4105.      * When comparing a value in the past to another value:
  4106.      * 1 hour after
  4107.      * 5 months after
  4108.      *
  4109.      * When comparing a value in the future to another value:
  4110.      * 1 hour before
  4111.      * 5 months before
  4112.      *
  4113.      * @param Carbon|null $other
  4114.      * @param bool        $absolute removes time difference modifiers ago, after, etc
  4115.      * @param bool        $short    displays short format of time units
  4116.      * @param int         $parts    displays number of parts in the interval
  4117.      *
  4118.      * @return string
  4119.      */
  4120.     public function to($other null$absolute false$short false$parts 1)
  4121.     {
  4122.         if (!$other && !$absolute) {
  4123.             $absolute = static::DIFF_RELATIVE_TO_NOW;
  4124.         }
  4125.         return $this->resolveCarbon($other)->diffForHumans($this$absolute$short$parts);
  4126.     }
  4127.     /**
  4128.      * @alias to
  4129.      *
  4130.      * Get the difference in a human readable format in the current locale from an other
  4131.      * instance given (or now if null given) to current instance.
  4132.      *
  4133.      * @param Carbon|null $other
  4134.      * @param bool        $absolute removes time difference modifiers ago, after, etc
  4135.      * @param bool        $short    displays short format of time units
  4136.      * @param int         $parts    displays number of parts in the interval
  4137.      *
  4138.      * @return string
  4139.      */
  4140.     public function until($other null$absolute false$short false$parts 1)
  4141.     {
  4142.         return $this->to($other$absolute$short$parts);
  4143.     }
  4144.     /**
  4145.      * Get the difference in a human readable format in the current locale from current
  4146.      * instance to now.
  4147.      *
  4148.      * @param bool $absolute removes time difference modifiers ago, after, etc
  4149.      * @param bool $short    displays short format of time units
  4150.      * @param int  $parts    displays number of parts in the interval
  4151.      *
  4152.      * @return string
  4153.      */
  4154.     public function fromNow($absolute null$short false$parts 1)
  4155.     {
  4156.         $other null;
  4157.         if ($absolute instanceof DateTimeInterface) {
  4158.             list($other$absolute$short$parts) = array_pad(func_get_args(), 5null);
  4159.         }
  4160.         return $this->from($other$absolute$short$parts);
  4161.     }
  4162.     /**
  4163.      * Get the difference in a human readable format in the current locale from an other
  4164.      * instance given to now
  4165.      *
  4166.      * @param bool $absolute removes time difference modifiers ago, after, etc
  4167.      * @param bool $short    displays short format of time units
  4168.      * @param int  $parts    displays number of parts in the interval
  4169.      *
  4170.      * @return string
  4171.      */
  4172.     public function toNow($absolute null$short false$parts 1)
  4173.     {
  4174.         return $this->to(null$absolute$short$parts);
  4175.     }
  4176.     /**
  4177.      * Get the difference in a human readable format in the current locale from an other
  4178.      * instance given to now
  4179.      *
  4180.      * @param bool $absolute removes time difference modifiers ago, after, etc
  4181.      * @param bool $short    displays short format of time units
  4182.      * @param int  $parts    displays number of parts in the interval
  4183.      *
  4184.      * @return string
  4185.      */
  4186.     public function ago($absolute null$short false$parts 1)
  4187.     {
  4188.         $other null;
  4189.         if ($absolute instanceof DateTimeInterface) {
  4190.             list($other$absolute$short$parts) = array_pad(func_get_args(), 5null);
  4191.         }
  4192.         return $this->from($other$absolute$short$parts);
  4193.     }
  4194.     ///////////////////////////////////////////////////////////////////
  4195.     //////////////////////////// MODIFIERS ////////////////////////////
  4196.     ///////////////////////////////////////////////////////////////////
  4197.     /**
  4198.      * Resets the time to 00:00:00 start of day
  4199.      *
  4200.      * @return static
  4201.      */
  4202.     public function startOfDay()
  4203.     {
  4204.         return $this->modify('00:00:00.000000');
  4205.     }
  4206.     /**
  4207.      * Resets the time to 23:59:59 end of day
  4208.      *
  4209.      * @return static
  4210.      */
  4211.     public function endOfDay()
  4212.     {
  4213.         return $this->modify('23:59:59.999999');
  4214.     }
  4215.     /**
  4216.      * Resets the date to the first day of the month and the time to 00:00:00
  4217.      *
  4218.      * @return static
  4219.      */
  4220.     public function startOfMonth()
  4221.     {
  4222.         return $this->setDate($this->year$this->month1)->startOfDay();
  4223.     }
  4224.     /**
  4225.      * Resets the date to end of the month and time to 23:59:59
  4226.      *
  4227.      * @return static
  4228.      */
  4229.     public function endOfMonth()
  4230.     {
  4231.         return $this->setDate($this->year$this->month$this->daysInMonth)->endOfDay();
  4232.     }
  4233.     /**
  4234.      * Resets the date to the first day of the quarter and the time to 00:00:00
  4235.      *
  4236.      * @return static
  4237.      */
  4238.     public function startOfQuarter()
  4239.     {
  4240.         $month = ($this->quarter 1) * static::MONTHS_PER_QUARTER 1;
  4241.         return $this->setDate($this->year$month1)->startOfDay();
  4242.     }
  4243.     /**
  4244.      * Resets the date to end of the quarter and time to 23:59:59
  4245.      *
  4246.      * @return static
  4247.      */
  4248.     public function endOfQuarter()
  4249.     {
  4250.         return $this->startOfQuarter()->addMonths(static::MONTHS_PER_QUARTER 1)->endOfMonth();
  4251.     }
  4252.     /**
  4253.      * Resets the date to the first day of the year and the time to 00:00:00
  4254.      *
  4255.      * @return static
  4256.      */
  4257.     public function startOfYear()
  4258.     {
  4259.         return $this->setDate($this->year11)->startOfDay();
  4260.     }
  4261.     /**
  4262.      * Resets the date to end of the year and time to 23:59:59
  4263.      *
  4264.      * @return static
  4265.      */
  4266.     public function endOfYear()
  4267.     {
  4268.         return $this->setDate($this->year1231)->endOfDay();
  4269.     }
  4270.     /**
  4271.      * Resets the date to the first day of the decade and the time to 00:00:00
  4272.      *
  4273.      * @return static
  4274.      */
  4275.     public function startOfDecade()
  4276.     {
  4277.         $year $this->year $this->year % static::YEARS_PER_DECADE;
  4278.         return $this->setDate($year11)->startOfDay();
  4279.     }
  4280.     /**
  4281.      * Resets the date to end of the decade and time to 23:59:59
  4282.      *
  4283.      * @return static
  4284.      */
  4285.     public function endOfDecade()
  4286.     {
  4287.         $year $this->year $this->year % static::YEARS_PER_DECADE + static::YEARS_PER_DECADE 1;
  4288.         return $this->setDate($year1231)->endOfDay();
  4289.     }
  4290.     /**
  4291.      * Resets the date to the first day of the century and the time to 00:00:00
  4292.      *
  4293.      * @return static
  4294.      */
  4295.     public function startOfCentury()
  4296.     {
  4297.         $year $this->year - ($this->year 1) % static::YEARS_PER_CENTURY;
  4298.         return $this->setDate($year11)->startOfDay();
  4299.     }
  4300.     /**
  4301.      * Resets the date to end of the century and time to 23:59:59
  4302.      *
  4303.      * @return static
  4304.      */
  4305.     public function endOfCentury()
  4306.     {
  4307.         $year $this->year - ($this->year 1) % static::YEARS_PER_CENTURY + static::YEARS_PER_CENTURY;
  4308.         return $this->setDate($year1231)->endOfDay();
  4309.     }
  4310.     /**
  4311.      * Resets the date to the first day of the century and the time to 00:00:00
  4312.      *
  4313.      * @return static
  4314.      */
  4315.     public function startOfMillennium()
  4316.     {
  4317.         $year $this->year - ($this->year 1) % static::YEARS_PER_MILLENNIUM;
  4318.         return $this->setDate($year11)->startOfDay();
  4319.     }
  4320.     /**
  4321.      * Resets the date to end of the century and time to 23:59:59
  4322.      *
  4323.      * @return static
  4324.      */
  4325.     public function endOfMillennium()
  4326.     {
  4327.         $year $this->year - ($this->year 1) % static::YEARS_PER_MILLENNIUM + static::YEARS_PER_MILLENNIUM;
  4328.         return $this->setDate($year1231)->endOfDay();
  4329.     }
  4330.     /**
  4331.      * Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
  4332.      *
  4333.      * @return static
  4334.      */
  4335.     public function startOfWeek()
  4336.     {
  4337.         while ($this->dayOfWeek !== static::$weekStartsAt) {
  4338.             $this->subDay();
  4339.         }
  4340.         return $this->startOfDay();
  4341.     }
  4342.     /**
  4343.      * Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
  4344.      *
  4345.      * @return static
  4346.      */
  4347.     public function endOfWeek()
  4348.     {
  4349.         while ($this->dayOfWeek !== static::$weekEndsAt) {
  4350.             $this->addDay();
  4351.         }
  4352.         return $this->endOfDay();
  4353.     }
  4354.     /**
  4355.      * Modify to start of current hour, minutes and seconds become 0
  4356.      *
  4357.      * @return static
  4358.      */
  4359.     public function startOfHour()
  4360.     {
  4361.         return $this->setTime($this->hour00);
  4362.     }
  4363.     /**
  4364.      * Modify to end of current hour, minutes and seconds become 59
  4365.      *
  4366.      * @return static
  4367.      */
  4368.     public function endOfHour()
  4369.     {
  4370.         return $this->modify("$this->hour:59:59.999999");
  4371.     }
  4372.     /**
  4373.      * Modify to start of current minute, seconds become 0
  4374.      *
  4375.      * @return static
  4376.      */
  4377.     public function startOfMinute()
  4378.     {
  4379.         return $this->setTime($this->hour$this->minute0);
  4380.     }
  4381.     /**
  4382.      * Modify to end of current minute, seconds become 59
  4383.      *
  4384.      * @return static
  4385.      */
  4386.     public function endOfMinute()
  4387.     {
  4388.         return $this->modify("$this->hour:$this->minute:59.999999");
  4389.     }
  4390.     /**
  4391.      * Modify to start of current minute, seconds become 0
  4392.      *
  4393.      * @return static
  4394.      */
  4395.     public function startOfSecond()
  4396.     {
  4397.         return $this->modify("$this->hour:$this->minute:$this->second.0");
  4398.     }
  4399.     /**
  4400.      * Modify to end of current minute, seconds become 59
  4401.      *
  4402.      * @return static
  4403.      */
  4404.     public function endOfSecond()
  4405.     {
  4406.         return $this->modify("$this->hour:$this->minute:$this->second.999999");
  4407.     }
  4408.     /**
  4409.      * Modify to midday, default to self::$midDayAt
  4410.      *
  4411.      * @return static
  4412.      */
  4413.     public function midDay()
  4414.     {
  4415.         return $this->setTime(self::$midDayAt00);
  4416.     }
  4417.     /**
  4418.      * Modify to the next occurrence of a given day of the week.
  4419.      * If no dayOfWeek is provided, modify to the next occurrence
  4420.      * of the current day of the week.  Use the supplied constants
  4421.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4422.      *
  4423.      * @param int|null $dayOfWeek
  4424.      *
  4425.      * @return static
  4426.      */
  4427.     public function next($dayOfWeek null)
  4428.     {
  4429.         if ($dayOfWeek === null) {
  4430.             $dayOfWeek $this->dayOfWeek;
  4431.         }
  4432.         return $this->startOfDay()->modify('next '.static::$days[$dayOfWeek]);
  4433.     }
  4434.     /**
  4435.      * Go forward or backward to the next week- or weekend-day.
  4436.      *
  4437.      * @param bool $weekday
  4438.      * @param bool $forward
  4439.      *
  4440.      * @return $this
  4441.      */
  4442.     private function nextOrPreviousDay($weekday true$forward true)
  4443.     {
  4444.         $step $forward : -1;
  4445.         do {
  4446.             $this->addDay($step);
  4447.         } while ($weekday $this->isWeekend() : $this->isWeekday());
  4448.         return $this;
  4449.     }
  4450.     /**
  4451.      * Go forward to the next weekday.
  4452.      *
  4453.      * @return $this
  4454.      */
  4455.     public function nextWeekday()
  4456.     {
  4457.         return $this->nextOrPreviousDay();
  4458.     }
  4459.     /**
  4460.      * Go backward to the previous weekday.
  4461.      *
  4462.      * @return $this
  4463.      */
  4464.     public function previousWeekday()
  4465.     {
  4466.         return $this->nextOrPreviousDay(truefalse);
  4467.     }
  4468.     /**
  4469.      * Go forward to the next weekend day.
  4470.      *
  4471.      * @return $this
  4472.      */
  4473.     public function nextWeekendDay()
  4474.     {
  4475.         return $this->nextOrPreviousDay(false);
  4476.     }
  4477.     /**
  4478.      * Go backward to the previous weekend day.
  4479.      *
  4480.      * @return $this
  4481.      */
  4482.     public function previousWeekendDay()
  4483.     {
  4484.         return $this->nextOrPreviousDay(falsefalse);
  4485.     }
  4486.     /**
  4487.      * Modify to the previous occurrence of a given day of the week.
  4488.      * If no dayOfWeek is provided, modify to the previous occurrence
  4489.      * of the current day of the week.  Use the supplied constants
  4490.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4491.      *
  4492.      * @param int|null $dayOfWeek
  4493.      *
  4494.      * @return static
  4495.      */
  4496.     public function previous($dayOfWeek null)
  4497.     {
  4498.         if ($dayOfWeek === null) {
  4499.             $dayOfWeek $this->dayOfWeek;
  4500.         }
  4501.         return $this->startOfDay()->modify('last '.static::$days[$dayOfWeek]);
  4502.     }
  4503.     /**
  4504.      * Modify to the first occurrence of a given day of the week
  4505.      * in the current month. If no dayOfWeek is provided, modify to the
  4506.      * first day of the current month.  Use the supplied constants
  4507.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4508.      *
  4509.      * @param int|null $dayOfWeek
  4510.      *
  4511.      * @return static
  4512.      */
  4513.     public function firstOfMonth($dayOfWeek null)
  4514.     {
  4515.         $this->startOfDay();
  4516.         if ($dayOfWeek === null) {
  4517.             return $this->day(1);
  4518.         }
  4519.         return $this->modify('first '.static::$days[$dayOfWeek].' of '.$this->format('F').' '.$this->year);
  4520.     }
  4521.     /**
  4522.      * Modify to the last occurrence of a given day of the week
  4523.      * in the current month. If no dayOfWeek is provided, modify to the
  4524.      * last day of the current month.  Use the supplied constants
  4525.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4526.      *
  4527.      * @param int|null $dayOfWeek
  4528.      *
  4529.      * @return static
  4530.      */
  4531.     public function lastOfMonth($dayOfWeek null)
  4532.     {
  4533.         $this->startOfDay();
  4534.         if ($dayOfWeek === null) {
  4535.             return $this->day($this->daysInMonth);
  4536.         }
  4537.         return $this->modify('last '.static::$days[$dayOfWeek].' of '.$this->format('F').' '.$this->year);
  4538.     }
  4539.     /**
  4540.      * Modify to the given occurrence of a given day of the week
  4541.      * in the current month. If the calculated occurrence is outside the scope
  4542.      * of the current month, then return false and no modifications are made.
  4543.      * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY.
  4544.      *
  4545.      * @param int $nth
  4546.      * @param int $dayOfWeek
  4547.      *
  4548.      * @return mixed
  4549.      */
  4550.     public function nthOfMonth($nth$dayOfWeek)
  4551.     {
  4552.         $date $this->copy()->firstOfMonth();
  4553.         $check $date->format('Y-m');
  4554.         $date->modify('+'.$nth.' '.static::$days[$dayOfWeek]);
  4555.         return $date->format('Y-m') === $check $this->modify($date) : false;
  4556.     }
  4557.     /**
  4558.      * Modify to the first occurrence of a given day of the week
  4559.      * in the current quarter. If no dayOfWeek is provided, modify to the
  4560.      * first day of the current quarter.  Use the supplied constants
  4561.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4562.      *
  4563.      * @param int|null $dayOfWeek day of the week default null
  4564.      *
  4565.      * @return static
  4566.      */
  4567.     public function firstOfQuarter($dayOfWeek null)
  4568.     {
  4569.         return $this->setDate($this->year$this->quarter * static::MONTHS_PER_QUARTER 21)->firstOfMonth($dayOfWeek);
  4570.     }
  4571.     /**
  4572.      * Modify to the last occurrence of a given day of the week
  4573.      * in the current quarter. If no dayOfWeek is provided, modify to the
  4574.      * last day of the current quarter.  Use the supplied constants
  4575.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4576.      *
  4577.      * @param int|null $dayOfWeek day of the week default null
  4578.      *
  4579.      * @return static
  4580.      */
  4581.     public function lastOfQuarter($dayOfWeek null)
  4582.     {
  4583.         return $this->setDate($this->year$this->quarter * static::MONTHS_PER_QUARTER1)->lastOfMonth($dayOfWeek);
  4584.     }
  4585.     /**
  4586.      * Modify to the given occurrence of a given day of the week
  4587.      * in the current quarter. If the calculated occurrence is outside the scope
  4588.      * of the current quarter, then return false and no modifications are made.
  4589.      * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY.
  4590.      *
  4591.      * @param int $nth
  4592.      * @param int $dayOfWeek
  4593.      *
  4594.      * @return mixed
  4595.      */
  4596.     public function nthOfQuarter($nth$dayOfWeek)
  4597.     {
  4598.         $date $this->copy()->day(1)->month($this->quarter * static::MONTHS_PER_QUARTER);
  4599.         $lastMonth $date->month;
  4600.         $year $date->year;
  4601.         $date->firstOfQuarter()->modify('+'.$nth.' '.static::$days[$dayOfWeek]);
  4602.         return ($lastMonth $date->month || $year !== $date->year) ? false $this->modify($date);
  4603.     }
  4604.     /**
  4605.      * Modify to the first occurrence of a given day of the week
  4606.      * in the current year. If no dayOfWeek is provided, modify to the
  4607.      * first day of the current year.  Use the supplied constants
  4608.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4609.      *
  4610.      * @param int|null $dayOfWeek day of the week default null
  4611.      *
  4612.      * @return static
  4613.      */
  4614.     public function firstOfYear($dayOfWeek null)
  4615.     {
  4616.         return $this->month(1)->firstOfMonth($dayOfWeek);
  4617.     }
  4618.     /**
  4619.      * Modify to the last occurrence of a given day of the week
  4620.      * in the current year. If no dayOfWeek is provided, modify to the
  4621.      * last day of the current year.  Use the supplied constants
  4622.      * to indicate the desired dayOfWeek, ex. static::MONDAY.
  4623.      *
  4624.      * @param int|null $dayOfWeek day of the week default null
  4625.      *
  4626.      * @return static
  4627.      */
  4628.     public function lastOfYear($dayOfWeek null)
  4629.     {
  4630.         return $this->month(static::MONTHS_PER_YEAR)->lastOfMonth($dayOfWeek);
  4631.     }
  4632.     /**
  4633.      * Modify to the given occurrence of a given day of the week
  4634.      * in the current year. If the calculated occurrence is outside the scope
  4635.      * of the current year, then return false and no modifications are made.
  4636.      * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY.
  4637.      *
  4638.      * @param int $nth
  4639.      * @param int $dayOfWeek
  4640.      *
  4641.      * @return mixed
  4642.      */
  4643.     public function nthOfYear($nth$dayOfWeek)
  4644.     {
  4645.         $date $this->copy()->firstOfYear()->modify('+'.$nth.' '.static::$days[$dayOfWeek]);
  4646.         return $this->year === $date->year $this->modify($date) : false;
  4647.     }
  4648.     /**
  4649.      * Modify the current instance to the average of a given instance (default now) and the current instance.
  4650.      *
  4651.      * @param \Carbon\Carbon|\DateTimeInterface|string|null $date
  4652.      *
  4653.      * @return static
  4654.      */
  4655.     public function average($date null)
  4656.     {
  4657.         $date $this->resolveCarbon($date);
  4658.         $increment $this->diffInRealSeconds($datefalse) / 2;
  4659.         $intIncrement floor($increment);
  4660.         $microIncrement = (int) (($date->micro $this->micro) / 1000000 * ($increment $intIncrement));
  4661.         $micro = (int) ($this->micro $microIncrement);
  4662.         while ($micro >= 1000000) {
  4663.             $micro -= 1000000;
  4664.             $intIncrement++;
  4665.         }
  4666.         $this->addSeconds($intIncrement);
  4667.         if (version_compare(PHP_VERSION'7.1.8-dev''>=')) {
  4668.             $this->setTime($this->hour$this->minute$this->second$micro);
  4669.         }
  4670.         return $this;
  4671.     }
  4672.     ///////////////////////////////////////////////////////////////////
  4673.     /////////////////////////// SERIALIZATION /////////////////////////
  4674.     ///////////////////////////////////////////////////////////////////
  4675.     /**
  4676.      * Return a serialized string of the instance.
  4677.      *
  4678.      * @return string
  4679.      */
  4680.     public function serialize()
  4681.     {
  4682.         return serialize($this);
  4683.     }
  4684.     /**
  4685.      * Create an instance from a serialized string.
  4686.      *
  4687.      * @param string $value
  4688.      *
  4689.      * @throws \InvalidArgumentException
  4690.      *
  4691.      * @return static
  4692.      */
  4693.     public static function fromSerialized($value)
  4694.     {
  4695.         $instance = @unserialize($value);
  4696.         if (!$instance instanceof static) {
  4697.             throw new InvalidArgumentException('Invalid serialized value.');
  4698.         }
  4699.         return $instance;
  4700.     }
  4701.     /**
  4702.      * The __set_state handler.
  4703.      *
  4704.      * @param array $array
  4705.      *
  4706.      * @return static
  4707.      */
  4708.     public static function __set_state($array)
  4709.     {
  4710.         return static::instance(parent::__set_state($array));
  4711.     }
  4712.     /**
  4713.      * Prepare the object for JSON serialization.
  4714.      *
  4715.      * @return array|string
  4716.      */
  4717.     public function jsonSerialize()
  4718.     {
  4719.         if (static::$serializer) {
  4720.             return call_user_func(static::$serializer$this);
  4721.         }
  4722.         $carbon $this;
  4723.         return call_user_func(function () use ($carbon) {
  4724.             return get_object_vars($carbon);
  4725.         });
  4726.     }
  4727.     /**
  4728.      * JSON serialize all Carbon instances using the given callback.
  4729.      *
  4730.      * @param callable $callback
  4731.      *
  4732.      * @return void
  4733.      */
  4734.     public static function serializeUsing($callback)
  4735.     {
  4736.         static::$serializer $callback;
  4737.     }
  4738.     ///////////////////////////////////////////////////////////////////
  4739.     /////////////////////////////// MACRO /////////////////////////////
  4740.     ///////////////////////////////////////////////////////////////////
  4741.     /**
  4742.      * Register a custom macro.
  4743.      *
  4744.      * @param string          $name
  4745.      * @param object|callable $macro
  4746.      *
  4747.      * @return void
  4748.      */
  4749.     public static function macro($name$macro)
  4750.     {
  4751.         static::$localMacros[$name] = $macro;
  4752.     }
  4753.     /**
  4754.      * Remove all macros.
  4755.      */
  4756.     public static function resetMacros()
  4757.     {
  4758.         static::$localMacros = array();
  4759.     }
  4760.     /**
  4761.      * Mix another object into the class.
  4762.      *
  4763.      * @param object $mixin
  4764.      *
  4765.      * @return void
  4766.      */
  4767.     public static function mixin($mixin)
  4768.     {
  4769.         $reflection = new \ReflectionClass($mixin);
  4770.         $methods $reflection->getMethods(
  4771.             \ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED
  4772.         );
  4773.         foreach ($methods as $method) {
  4774.             $method->setAccessible(true);
  4775.             static::macro($method->name$method->invoke($mixin));
  4776.         }
  4777.     }
  4778.     /**
  4779.      * Checks if macro is registered.
  4780.      *
  4781.      * @param string $name
  4782.      *
  4783.      * @return bool
  4784.      */
  4785.     public static function hasMacro($name)
  4786.     {
  4787.         return isset(static::$localMacros[$name]);
  4788.     }
  4789.     /**
  4790.      * Dynamically handle calls to the class.
  4791.      *
  4792.      * @param string $method
  4793.      * @param array  $parameters
  4794.      *
  4795.      * @throws \BadMethodCallException
  4796.      *
  4797.      * @return mixed
  4798.      */
  4799.     public static function __callStatic($method$parameters)
  4800.     {
  4801.         if (!static::hasMacro($method)) {
  4802.             throw new \BadMethodCallException("Method $method does not exist.");
  4803.         }
  4804.         if (static::$localMacros[$method] instanceof Closure && method_exists('Closure''bind')) {
  4805.             return call_user_func_array(Closure::bind(static::$localMacros[$method], nullget_called_class()), $parameters);
  4806.         }
  4807.         return call_user_func_array(static::$localMacros[$method], $parameters);
  4808.     }
  4809.     /**
  4810.      * Dynamically handle calls to the class.
  4811.      *
  4812.      * @param string $method
  4813.      * @param array  $parameters
  4814.      *
  4815.      * @throws \BadMethodCallException|\ReflectionException
  4816.      *
  4817.      * @return mixed
  4818.      */
  4819.     public function __call($method$parameters)
  4820.     {
  4821.         if (!static::hasMacro($method)) {
  4822.             throw new \BadMethodCallException("Method $method does not exist.");
  4823.         }
  4824.         $macro = static::$localMacros[$method];
  4825.         $reflexion = new \ReflectionFunction($macro);
  4826.         $reflectionParameters $reflexion->getParameters();
  4827.         $expectedCount count($reflectionParameters);
  4828.         $actualCount count($parameters);
  4829.         if ($expectedCount $actualCount && $reflectionParameters[$expectedCount 1]->name === 'self') {
  4830.             for ($i $actualCount$i $expectedCount 1$i++) {
  4831.                 $parameters[] = $reflectionParameters[$i]->getDefaultValue();
  4832.             }
  4833.             $parameters[] = $this;
  4834.         }
  4835.         if ($macro instanceof Closure && method_exists($macro'bindTo')) {
  4836.             return call_user_func_array($macro->bindTo($thisget_class($this)), $parameters);
  4837.         }
  4838.         return call_user_func_array($macro$parameters);
  4839.     }
  4840.     /**
  4841.      * Show truthy properties on var_dump().
  4842.      *
  4843.      * @return array
  4844.      */
  4845.     public function __debugInfo()
  4846.     {
  4847.         return array_filter(get_object_vars($this), function ($var) {
  4848.             return $var;
  4849.         });
  4850.     }
  4851.     /**
  4852.      * Cast the current instance into the given class.
  4853.      *
  4854.      * @param string $className The $className::instance() method will be called to cast the current object.
  4855.      *
  4856.      * @return object
  4857.      */
  4858.     public function cast($className)
  4859.     {
  4860.         if (!method_exists($className'instance')) {
  4861.             throw new \InvalidArgumentException("$className has not the instance() method needed to cast the date.");
  4862.         }
  4863.         return $className::instance($this);
  4864.     }
  4865. }