class DateFactory

Single point of creation for all date objects in the framework.

Every date Chappy creates should come from this class so that the application timezone, locale, and choice of mutable or immutable Carbon are applied consistently. Because "now" always comes from Carbon, Carbon::setTestNow() freezes time for everything built on this factory.

Properties

static protected CarbonInterface> $class

The Carbon class used to build new instances.

Methods

static void
use(string $class)

Sets the Carbon class used for all new instances.

static void
boot()

Sets the Carbon class from the DATE_CLASS environment value.

static string
getClass()

Returns the Carbon class currently in use.

static CarbonInterface
now(string|null $timezone = null)

Returns the current time.

static CarbonInterface
today(string|null $timezone = null)

Returns midnight of the current day.

static CarbonInterface
parse(mixed $value, string $fromTimezone = 'UTC')

Normalizes any supported input into a Carbon instance in the application timezone.

static CarbonInterface
configure(CarbonInterface $date)

Applies framework-wide settings to an instance.

static string
timezone()

Returns the application timezone.

Details

at line 36
static void use(string $class)

Sets the Carbon class used for all new instances.

Call once during bootstrap, for example: DateFactory::use(CarbonImmutable::class);

Parameters

string $class

Carbon::class, CarbonImmutable::class, or a subclass.

Return Value

void

Exceptions

InvalidArgumentException

at line 49
static void boot()

Sets the Carbon class from the DATE_CLASS environment value.

Accepts "immutable" or "mutable". Defaults to mutable.

Return Value

void

at line 59
static string getClass()

Returns the Carbon class currently in use.

Return Value

string

at line 69
static CarbonInterface now(string|null $timezone = null)

Returns the current time.

Parameters

string|null $timezone

Falls back to TIME_ZONE when null.

Return Value

CarbonInterface

at line 79
static CarbonInterface today(string|null $timezone = null)

Returns midnight of the current day.

Parameters

string|null $timezone

Falls back to TIME_ZONE when null.

Return Value

CarbonInterface

at line 96
static CarbonInterface parse(mixed $value, string $fromTimezone = 'UTC')

Normalizes any supported input into a Carbon instance in the application timezone.

Accepts:

  • DateTimeInterface instances
  • Integer Unix timestamps (always treated as UTC)
  • Date strings, interpreted in $fromTimezone

Parameters

mixed $value

The value to convert.

string $fromTimezone

Timezone the input string is stored in.

Return Value

CarbonInterface

at line 112
static protected CarbonInterface configure(CarbonInterface $date)

Applies framework-wide settings to an instance.

Parameters

CarbonInterface $date

Return Value

CarbonInterface

at line 121
static string timezone()

Returns the application timezone.

Return Value

string