FactoryTrait
Provides a number of datetime related factory methods.
Table of Contents
Properties
- $_lastErrors : array<string|int, mixed>
- Holds the last error generated by createFromFormat
Methods
- create() : static
- Create a new ChronosInterface instance from a specific date and time.
- createFromArray() : static
- Creates a ChronosInterface instance from an array of date and time values.
- createFromDate() : static
- Create a ChronosInterface instance from just a date. The time portion is set to now.
- createFromFormat() : static
- Create a ChronosInterface instance from a specific format
- createFromTime() : static
- Create a ChronosInterface instance from just a time. The date portion is set to today.
- createFromTimestamp() : static
- Create a ChronosInterface instance from a timestamp
- createFromTimestampUTC() : static
- Create a ChronosInterface instance from an UTC timestamp
- getLastErrors() : array<string|int, mixed>
- Returns any errors or warnings that were found during the parsing of the last object created by this class.
- instance() : static
- Create a ChronosInterface instance from a DateTimeInterface one
- maxValue() : ChronosInterface
- Create a ChronosInterface instance for the greatest supported date.
- minValue() : ChronosInterface
- Create a ChronosInterface instance for the lowest supported date.
- now() : static
- Get a ChronosInterface instance for the current date and time
- parse() : static
- Create a ChronosInterface instance from a string. This is an alias for the constructor that allows better fluent syntax as it allows you to do ChronosInterface::parse('Monday next week')->fn() rather than (new Chronos('Monday next week'))->fn()
- today() : static
- Create a ChronosInterface instance for today
- tomorrow() : static
- Create a ChronosInterface instance for tomorrow
- yesterday() : static
- Create a ChronosInterface instance for yesterday
- safeCreateDateTimeZone() : DateTimeZone
- Creates a DateTimeZone from a string or a DateTimeZone
Properties
$_lastErrors
Holds the last error generated by createFromFormat
protected
static array<string|int, mixed>
$_lastErrors
= []
Methods
create()
Create a new ChronosInterface instance from a specific date and time.
public
static create([int|null $year = null ][, int|null $month = null ][, int|null $day = null ][, int|null $hour = null ][, int|null $minute = null ][, int|null $second = null ][, int|null $microsecond = null ][, DateTimeZone|string|null $tz = null ]) : static
If any of $year, $month or $day are set to null their now() values will be used.
If $hour is null it will be set to its now() value and the default values for $minute, $second and $microsecond will be their now() values. If $hour is not null then the default values for $minute, $second and $microsecond will be 0.
Parameters
- $year : int|null = null
-
The year to create an instance with.
- $month : int|null = null
-
The month to create an instance with.
- $day : int|null = null
-
The day to create an instance with.
- $hour : int|null = null
-
The hour to create an instance with.
- $minute : int|null = null
-
The minute to create an instance with.
- $second : int|null = null
-
The second to create an instance with.
- $microsecond : int|null = null
-
The microsecond to create an instance with.
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name the new instance should use.
Return values
staticcreateFromArray()
Creates a ChronosInterface instance from an array of date and time values.
public
static createFromArray(array<string|int, int|string> $values) : static
The 'year', 'month' and 'day' values must all be set for a date. The time values all default to 0.
The 'timezone' value can be any format supported by \DateTimeZone
.
Allowed values:
- year
- month
- day
- hour
- minute
- second
- microsecond
- meridian ('am' or 'pm')
- timezone
Parameters
- $values : array<string|int, int|string>
-
Array of date and time values.
Return values
staticcreateFromDate()
Create a ChronosInterface instance from just a date. The time portion is set to now.
public
static createFromDate([int|null $year = null ][, int|null $month = null ][, int|null $day = null ][, DateTimeZone|string|null $tz = null ]) : static
Parameters
- $year : int|null = null
-
The year to create an instance with.
- $month : int|null = null
-
The month to create an instance with.
- $day : int|null = null
-
The day to create an instance with.
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name the new instance should use.
Return values
staticcreateFromFormat()
Create a ChronosInterface instance from a specific format
public
static createFromFormat(string $format, string $time[, DateTimeZone|string|null $tz = null ]) : static
Parameters
- $format : string
-
The date() compatible format string.
- $time : string
-
The formatted date string to interpret.
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name the new instance should use.
Tags
Attributes
- #[ReturnTypeWillChange]
Return values
staticcreateFromTime()
Create a ChronosInterface instance from just a time. The date portion is set to today.
public
static createFromTime([int|null $hour = null ][, int|null $minute = null ][, int|null $second = null ][, int|null $microsecond = null ][, DateTimeZone|string|null $tz = null ]) : static
Parameters
- $hour : int|null = null
-
The hour to create an instance with.
- $minute : int|null = null
-
The minute to create an instance with.
- $second : int|null = null
-
The second to create an instance with.
- $microsecond : int|null = null
-
The microsecond to create an instance with.
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name the new instance should use.
Return values
staticcreateFromTimestamp()
Create a ChronosInterface instance from a timestamp
public
static createFromTimestamp(int $timestamp[, DateTimeZone|string|null $tz = null ]) : static
Parameters
- $timestamp : int
-
The timestamp to create an instance from.
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name the new instance should use.
Return values
staticcreateFromTimestampUTC()
Create a ChronosInterface instance from an UTC timestamp
public
static createFromTimestampUTC(int $timestamp) : static
Parameters
- $timestamp : int
-
The UTC timestamp to create an instance from.
Return values
staticgetLastErrors()
Returns any errors or warnings that were found during the parsing of the last object created by this class.
public
static getLastErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>instance()
Create a ChronosInterface instance from a DateTimeInterface one
public
static instance(DateTimeInterface $dt) : static
Parameters
- $dt : DateTimeInterface
-
The datetime instance to convert.
Return values
staticmaxValue()
Create a ChronosInterface instance for the greatest supported date.
public
static maxValue() : ChronosInterface
Return values
ChronosInterfaceminValue()
Create a ChronosInterface instance for the lowest supported date.
public
static minValue() : ChronosInterface
Return values
ChronosInterfacenow()
Get a ChronosInterface instance for the current date and time
public
static now([DateTimeZone|string|null $tz = null ]) : static
Parameters
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name.
Return values
staticparse()
Create a ChronosInterface instance from a string. This is an alias for the constructor that allows better fluent syntax as it allows you to do ChronosInterface::parse('Monday next week')->fn() rather than (new Chronos('Monday next week'))->fn()
public
static parse([DateTimeInterface|string|int $time = 'now' ][, DateTimeZone|string|null $tz = null ]) : static
Parameters
- $time : DateTimeInterface|string|int = 'now'
-
The strtotime compatible string to parse
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name.
Return values
statictoday()
Create a ChronosInterface instance for today
public
static today([DateTimeZone|string|null $tz = null ]) : static
Parameters
- $tz : DateTimeZone|string|null = null
-
The timezone to use.
Return values
statictomorrow()
Create a ChronosInterface instance for tomorrow
public
static tomorrow([DateTimeZone|string|null $tz = null ]) : static
Parameters
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name the new instance should use.
Return values
staticyesterday()
Create a ChronosInterface instance for yesterday
public
static yesterday([DateTimeZone|string|null $tz = null ]) : static
Parameters
- $tz : DateTimeZone|string|null = null
-
The DateTimeZone object or timezone name the new instance should use.
Return values
staticsafeCreateDateTimeZone()
Creates a DateTimeZone from a string or a DateTimeZone
protected
static safeCreateDateTimeZone(DateTimeZone|string|null $object) : DateTimeZone
Parameters
- $object : DateTimeZone|string|null
-
The value to convert.