trait Options

Trait Options.

Embed base methods to change settings of Carbon classes.

Depends on the following methods:

Traits

Trait Localization.

Properties

staticprotectedTranslatorInterface $translator Default translator. from Localization
protectedTranslatorInterface $localTranslator Specific translator of the current instance. from Localization
staticprotectedint $humanDiffOptions Options for diffForHumans(). from Localization
staticint $PHPIntSize Customizable PHP_INT_SIZE override.
staticprotectedint|string $weekStartsAt First day of week.
staticprotectedint|string $weekEndsAt Last day of week.
staticprotectedarray $weekendDays Days of weekend.
staticprotectedarray $regexFormats Format regex patterns.
staticprotectedarray $regexFormatModifiers Format modifiers (such as available in createFromFormat) regex patterns.
staticprotectedbool $monthsOverflow Indicates if months should be calculated with overflow.
staticprotectedbool $yearsOverflow Indicates if years should be calculated with overflow.
staticprotectedbool $strictModeEnabled Indicates if the strict mode is in use.
staticprotectedstring|callable|null $formatFunction Function to call instead of format.
staticprotectedstring|callable|null $createFromFormatFunction Function to call instead of createFromFormat.
staticprotectedstring|callable|null $parseFunction Function to call instead of parse.
protectedbool|null $localMonthsOverflow Indicates if months should be calculated with overflow.
protectedbool|null $localYearsOverflow Indicates if years should be calculated with overflow.
protectedbool|null $localStrictModeEnabled Indicates if the strict mode is in use.
protectedbool|null $localHumanDiffOptions Options for diffForHumans and forHumans methods.
protectedstring|null $localToStringFormat Format to use on string cast.
protectedstring|null $localSerializer Format to use on JSON serialization.
protectedarray|null $localMacros Instance-specific macros.
protectedarray|null $localGenericMacros Instance-specific generic macros.
protectedstring|callable|null $localFormatFunction Function to call instead of format.

Methods

static 
setHumanDiffOptions(int$humanDiffOptions)deprecated

No description

static 
enableHumanDiffOption(int$humanDiffOption)deprecated

No description

static 
disableHumanDiffOption(int$humanDiffOption)deprecated

No description

static int
getHumanDiffOptions()

Return default humanDiff() options (merged flags as integer).

static TranslatorInterface
getTranslator()

Get the default translator instance in use.

static void
setTranslator(TranslatorInterface$translator)

Set the default translator instance to use.

bool
hasLocalTranslator()

Return true if the current instance has its own translator.

TranslatorInterface
getLocalTranslator()

Get the translator of the current instance or the default if none set.

$this
setLocalTranslator(TranslatorInterface$translator)

Set the translator for the current instance.

static string
getTranslationMessageWith(TranslatorInterface$translator,string$key,string|null$locale =null,string|null$default =null)

Returns raw translation message for a given key.

string
getTranslationMessage(string$key,string|null$locale =null,string|null$default =null,TranslatorInterface$translator =null)

Returns raw translation message for a given key.

static string
translateWith(TranslatorInterface$translator,string$key,array$parameters =[],null$number =null)

Translate using translation string or callback available.

string
translate(string$key,array$parameters =[],string|int|float|null$number =null,TranslatorInterface|null$translator =null,bool$altNumbers =false)

Translate using translation string or callback available.

string
translateNumber(int$number)

Returns the alternative number for a given integer if available in the current locale.

static string
translateTimeString(string$timeString,string|null$from =null,string|null$to =null,int$mode =CarbonInterface::TRANSLATE_ALL)

Translate a time string from a locale to an other.

string
translateTimeStringTo(string$timeString,string|null$to =null)

Translate a time string from the current locale ($date->locale()) to an other.

$this|string
locale(string$locale =null,string...$fallbackLocales)

Get/set the locale for the current instance.

static string
getLocale()

Get the current translator locale.

static bool
setLocale(string$locale)

Set the current translator locale and indicate if the source locale file exists.

static 
setFallbackLocale(string$locale)

Set the fallback locale.

static string|null
getFallbackLocale()

Get the fallback locale.

static mixed
executeWithLocale(string$locale,callable$func)

Set the current locale to the given, execute the passed function, reset the locale to previous one, then return the result of the closure (or null if the closure was void).

static bool
localeHasShortUnits(string$locale)

Returns true if the given locale is internally supported and has short-units support.

static bool
localeHasDiffSyntax(string$locale)

Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after).

static bool
localeHasDiffOneDayWords(string$locale)

Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow).

static bool
localeHasDiffTwoDayWords(string$locale)

Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow).

static bool
localeHasPeriodSyntax(string$locale)

Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X).

static array
getAvailableLocales()

Returns the list of internally available locales and already loaded custom locales.

static Language[]
getAvailableLocalesInfo()

Returns list of Language object for each available locale. This object allow you to get the ISO name, native name, region and variant of the locale.

static TranslatorInterface
translator()

Initialize the default translator instance if necessary.

string|null
getTranslatorLocale(null$translator =null)

Get the locale of a given translator.

static LocaleAwareInterface|null
getLocaleAwareTranslator(LocaleAwareInterface|null$translator =null)

Throw an error if passed object is not LocaleAwareInterface.

static 
useStrictMode(bool$strictModeEnabled =true)deprecated

No description

static bool
isStrictModeEnabled()

Returns true if the strict mode is globally in use, false else.

static void
useMonthsOverflow(bool$monthsOverflow =true)deprecated

No description

static void
resetMonthsOverflow()deprecated

No description

static bool
shouldOverflowMonths()

Get the month overflow global behavior (can be overridden in specific instances).

static void
useYearsOverflow(bool$yearsOverflow =true)deprecated

No description

static void
resetYearsOverflow()deprecated

No description

static bool
shouldOverflowYears()

Get the month overflow global behavior (can be overridden in specific instances).

$this|Options
settings(array$settings)

Set specific options.

array
getSettings()

Returns current local settings.

array
__debugInfo()

Show truthy properties on var_dump().

addExtraDebugInfos($infos)

No description

CarbonImmutable
shiftTimezone($timezone)

Set the timezone

Details

inLocalizationat line70
static setHumanDiffOptions(int$humanDiffOptions)deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method.

Parameters

int $humanDiffOptions

See also

settings

inLocalizationat line82
static enableHumanDiffOption(int$humanDiffOption)deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method.

Parameters

int $humanDiffOption

See also

settings

inLocalizationat line94
static disableHumanDiffOption(int$humanDiffOption)deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method.

Parameters

int $humanDiffOption

See also

settings

inLocalizationat line104
staticint getHumanDiffOptions()

Return default humanDiff() options (merged flags as integer).

Return Value

int

inLocalizationat line114
staticTranslatorInterface getTranslator()

Get the default translator instance in use.

Return Value

TranslatorInterface

inLocalizationat line126
staticvoid setTranslator(TranslatorInterface$translator)

Set the default translator instance to use.

Parameters

TranslatorInterface $translator

Return Value

void

inLocalizationat line136
bool hasLocalTranslator()

Return true if the current instance has its own translator.

Return Value

bool

inLocalizationat line146
TranslatorInterface getLocalTranslator()

Get the translator of the current instance or the default if none set.

Return Value

TranslatorInterface

inLocalizationat line158
$this setLocalTranslator(TranslatorInterface$translator)

Set the translator for the current instance.

Parameters

TranslatorInterface $translator

Return Value

$this

inLocalizationat line175
staticstring getTranslationMessageWith(TranslatorInterface$translator,string$key,string|null$locale =null,string|null$default =null)

Returns raw translation message for a given key.

Parameters

TranslatorInterface $translator the translator to use
string $key key to find
string|null $locale current locale used if null
string|null $default default value if translation returns the key

Return Value

string

inLocalizationat line203
string getTranslationMessage(string$key,string|null$locale =null,string|null$default =null,TranslatorInterface$translator =null)

Returns raw translation message for a given key.

Parameters

string $key key to find
string|null $locale current locale used if null
string|null $default default value if translation returns the key
TranslatorInterface $translator an optional translator to use

Return Value

string

inLocalizationat line218
staticstring translateWith(TranslatorInterface$translator,string$key,array$parameters =[],null$number =null)

Translate using translation string or callback available.

Parameters

TranslatorInterface $translator
string $key
array $parameters
null $number

Return Value

string

inLocalizationat line252
string translate(string$key,array$parameters =[],string|int|float|null$number =null,TranslatorInterface|null$translator =null,bool$altNumbers =false)

Translate using translation string or callback available.

Parameters

string $key
array $parameters
string|int|float|null $number
TranslatorInterface|null $translator
bool $altNumbers

Return Value

string

inLocalizationat line270
string translateNumber(int$number)

Returns the alternative number for a given integer if available in the current locale.

Parameters

int $number

Return Value

string

inLocalizationat line329
staticstring translateTimeString(string$timeString,string|null$from =null,string|null$to =null,int$mode =CarbonInterface::TRANSLATE_ALL)

Translate a time string from a locale to an other.

Parameters

string $timeString date/time/duration string to translate (may also contain English)
string|null $from input locale of the $timeString parameter (Carbon::getLocale() by default)
string|null $to output locale of the result returned ("en" by default)
int $mode specify what to translate with options: - CarbonInterface::TRANSLATE_ALL (default) - CarbonInterface::TRANSLATE_MONTHS - CarbonInterface::TRANSLATE_DAYS - CarbonInterface::TRANSLATE_UNITS - CarbonInterface::TRANSLATE_MERIDIEM You can use pipe to group: CarbonInterface::TRANSLATE_MONTHS | CarbonInterface::TRANSLATE_DAYS

Return Value

string

inLocalizationat line425
string translateTimeStringTo(string$timeString,string|null$to =null)

Translate a time string from the current locale ($date->locale()) to an other.

Parameters

string $timeString time string to translate
string|null $to output locale of the result returned ("en" by default)

Return Value

string

inLocalizationat line438
$this|string locale(string$locale =null,string...$fallbackLocales)

Get/set the locale for the current instance.

Parameters

string $locale
string ...$fallbackLocales

Return Value

$this|string

inLocalizationat line470
staticstring getLocale()

Get the current translator locale.

Return Value

string

inLocalizationat line483
staticbool setLocale(string$locale)

Set the current translator locale and indicate if the source locale file exists.

Pass 'auto' as locale to use closest language from the current LC_TIME locale.

Parameters

string $locale locale ex. en

Return Value

bool

inLocalizationat line495
static setFallbackLocale(string$locale)

Set the fallback locale.

inLocalizationat line520
staticstring|null getFallbackLocale()

Get the fallback locale.

inLocalizationat line540
staticmixed executeWithLocale(string$locale,callable$func)

Set the current locale to the given, execute the passed function, reset the locale to previous one, then return the result of the closure (or null if the closure was void).

Parameters

string $locale locale ex. en
callable $func

Return Value

mixed

inLocalizationat line557
staticbool localeHasShortUnits(string$locale)

Returns true if the given locale is internally supported and has short-units support.

Support is considered enabled if either year, day or hour has a short variant translated.

Parameters

string $locale locale ex. en

Return Value

bool

inLocalizationat line578
staticbool localeHasDiffSyntax(string$locale)

Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after).

Support is considered enabled if the 4 sentences are translated in the given locale.

Parameters

string $locale locale ex. en

Return Value

bool

inLocalizationat line609
staticbool localeHasDiffOneDayWords(string$locale)

Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow).

Support is considered enabled if the 3 words are translated in the given locale.

Parameters

string $locale locale ex. en

Return Value

bool

inLocalizationat line627
staticbool localeHasDiffTwoDayWords(string$locale)

Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow).

Support is considered enabled if the 2 words are translated in the given locale.

Parameters

string $locale locale ex. en

Return Value

bool

inLocalizationat line644
staticbool localeHasPeriodSyntax(string$locale)

Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X).

Support is considered enabled if the 4 sentences are translated in the given locale.

Parameters

string $locale locale ex. en

Return Value

bool

inLocalizationat line661
staticarray getAvailableLocales()

Returns the list of internally available locales and already loaded custom locales.

(It will ignore custom translator dynamic loading.)

Return Value

array

inLocalizationat line676
staticLanguage[] getAvailableLocalesInfo()

Returns list of Language object for each available locale. This object allow you to get the ISO name, native name, region and variant of the locale.

Return Value

Language[]

inLocalizationat line691
staticprotectedTranslatorInterface translator()

Initialize the default translator instance if necessary.

Return Value

TranslatorInterface

inLocalizationat line710
protectedstring|null getTranslatorLocale(null$translator =null)

Get the locale of a given translator.

If null or omitted, current local translator is used. If no local translator is in use, current global translator is used.

Parameters

null $translator

Return Value

string|null

inLocalizationat line728
staticprotectedLocaleAwareInterface|null getLocaleAwareTranslator(LocaleAwareInterface|null$translator =null)

Throw an error if passed object is not LocaleAwareInterface.

Parameters

LocaleAwareInterface|null $translator

Return Value

LocaleAwareInterface|null

at line246
static useStrictMode(bool$strictModeEnabled =true)deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method.

Parameters

bool $strictModeEnabled

See also

settings Enable the strict mode (or disable with passing false).

at line257
staticbool isStrictModeEnabled()

Returns true if the strict mode is globally in use, false else.

(It can be overridden in specific instances.)

Return Value

bool

at line275
staticvoid useMonthsOverflow(bool$monthsOverflow =true)deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method. Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants are available for quarters, years, decade, centuries, millennia (singular and plural forms).

Parameters

bool $monthsOverflow

Return Value

void

See also

settings Indicates if months should be calculated with overflow.

at line291
staticvoid resetMonthsOverflow()deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method. Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants are available for quarters, years, decade, centuries, millennia (singular and plural forms).

Return Value

void

See also

settings Reset the month overflow behavior.

at line301
staticbool shouldOverflowMonths()

Get the month overflow global behavior (can be overridden in specific instances).

Return Value

bool

at line319
staticvoid useYearsOverflow(bool$yearsOverflow =true)deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method. Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants are available for quarters, years, decade, centuries, millennia (singular and plural forms).

Parameters

bool $yearsOverflow

Return Value

void

See also

settings Indicates if years should be calculated with overflow.

at line335
staticvoid resetYearsOverflow()deprecated

deprecated To avoid conflict between different third-party libraries, static setters should not be used. You should rather use the ->settings() method. Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants are available for quarters, years, decade, centuries, millennia (singular and plural forms).

Return Value

void

See also

settings Reset the month overflow behavior.

at line345
staticbool shouldOverflowYears()

Get the month overflow global behavior (can be overridden in specific instances).

Return Value

bool

at line367
$this|Options settings(array$settings)

Set specific options.

  • strictMode: true|false|null
    • monthOverflow: true|false|null
    • yearOverflow: true|false|null
    • humanDiffOptions: int|null
    • toStringFormat: string|Closure|null
    • toJsonFormat: string|Closure|null
    • locale: string|null
    • timezone: \DateTimeZone|string|int|null
    • macros: array|null
    • genericMacros: array|null

Parameters

array $settings

Return Value

$this|Options

at line405
array getSettings()

Returns current local settings.

Return Value

array

at line438
array __debugInfo()

Show truthy properties on var_dump().

Return Value

array

at line455
protected addExtraDebugInfos($infos)

Parameters

$infos

at line27
CarbonImmutable shiftTimezone($timezone)

Set the timezone

Parameters

$timezone

Return Value

CarbonImmutable