DateTime
in package
implements
DateTimeExtension, GeneratorAwareExtension
uses
GeneratorAwareExtensionTrait
Tags
Table of Contents
Interfaces
- DateTimeExtension
- FakerPHP extension for Date-related randomization.
- GeneratorAwareExtension
- An extension is the only way to add new functionality to Faker.
Properties
- $centuries : array<string|int, string>
- $defaultTimezone : string|null
- $generator : Generator|null
Methods
- amPm() : string
- Get a string containing either "am" or "pm".
- century() : string
- Get a random century, formatted as Roman numerals.
- date() : string
- Get a date string between January 1, 1970, and `$until`.
- dateTime() : DateTime
- Get a DateTime object between January 1, 1970, and `$until` (defaults to "now").
- dateTimeAD() : DateTime
- Get a DateTime object for a date between January 1, 0001, and now.
- dateTimeBetween() : DateTime
- Get a DateTime object a random date between `$from` and `$until`.
- dateTimeInInterval() : DateTime
- Get a DateTime object based on a random date between `$from` and an interval.
- dateTimeThisCentury() : DateTime
- Get a date time object somewhere inside the current century.
- dateTimeThisDecade() : DateTime
- Get a date time object somewhere inside the current decade.
- dateTimeThisMonth() : DateTime
- Get a date time object somewhere inside the current month.
- dateTimeThisWeek() : DateTime
- Get a date time object somewhere inside the current week.
- dateTimeThisYear() : DateTime
- Get a date time object somewhere inside the current year.
- dayOfMonth() : string
- Get a localized random day of the month.
- dayOfWeek() : string
- Get a localized random day of the week.
- iso8601() : string
- Get a date string according to the ISO-8601 standard.
- month() : string
- Get a random month (numbered).
- monthName() : string
- Get a random month.
- time() : string
- Get a time string (24h format by default).
- timezone() : string
- Get a random timezone, uses `\DateTimeZone::listIdentifiers()` internally.
- unixTime() : int
- Get a UNIX (POSIX-compatible) timestamp between January 1, 1970, and `$until`.
- withGenerator() : static
- year() : string
- Get a random year between 1970 and `$until`.
- getTimestamp() : false|int
- Get the POSIX-timestamp of a DateTime, int or string.
- getTimestampDateTime() : DateTime
- Get a DateTime created based on a POSIX-timestamp.
- resolveTimezone() : string
- setTimezone() : DateTime
- Internal method to set the timezone on a DateTime object.
Properties
$centuries
private
array<string|int, string>
$centuries
= ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX', 'XXI']
$defaultTimezone
private
string|null
$defaultTimezone
= null
$generator
private
Generator|null
$generator
Methods
amPm()
Get a string containing either "am" or "pm".
public
amPm([mixed $until = 'now' ]) : string
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringcentury()
Get a random century, formatted as Roman numerals.
public
century() : string
Return values
stringdate()
Get a date string between January 1, 1970, and `$until`.
public
date([string $format = 'Y-m-d' ][, mixed $until = 'now' ]) : string
Parameters
- $format : string = 'Y-m-d'
-
DateTime format
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringdateTime()
Get a DateTime object between January 1, 1970, and `$until` (defaults to "now").
public
dateTime([mixed $until = 'now' ][, string $timezone = null ]) : DateTime
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeAD()
Get a DateTime object for a date between January 1, 0001, and now.
public
dateTimeAD([mixed $until = 'now' ][, string $timezone = null ]) : DateTime
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeBetween()
Get a DateTime object a random date between `$from` and `$until`.
public
dateTimeBetween([mixed $from = '-30 years' ][, mixed $until = 'now' ][, string $timezone = null ]) : DateTime
Accepts date strings that can be recognized by strtotime()
.
Parameters
- $from : mixed = '-30 years'
-
defaults to 30 years ago
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeInInterval()
Get a DateTime object based on a random date between `$from` and an interval.
public
dateTimeInInterval([mixed $from = '-30 years' ][, string $interval = '+5 days' ][, string $timezone = null ]) : DateTime
Accepts date string that can be recognized by strtotime()
.
Parameters
- $from : mixed = '-30 years'
-
defaults to 30 years ago
- $interval : string = '+5 days'
-
defaults to 5 days after
- $timezone : string = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeThisCentury()
Get a date time object somewhere inside the current century.
public
dateTimeThisCentury([mixed $until = 'now' ][, string $timezone = null ]) : DateTime
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeThisDecade()
Get a date time object somewhere inside the current decade.
public
dateTimeThisDecade([mixed $until = 'now' ][, string $timezone = null ]) : DateTime
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeThisMonth()
Get a date time object somewhere inside the current month.
public
dateTimeThisMonth([mixed $until = 'last day of this month' ][, string $timezone = null ]) : DateTime
Parameters
- $until : mixed = 'last day of this month'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeThisWeek()
Get a date time object somewhere inside the current week.
public
dateTimeThisWeek([mixed $until = 'sunday this week' ][, string $timezone = null ]) : DateTime
Parameters
- $until : mixed = 'sunday this week'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedateTimeThisYear()
Get a date time object somewhere inside the current year.
public
dateTimeThisYear([mixed $until = 'last day of december' ][, string $timezone = null ]) : DateTime
Parameters
- $until : mixed = 'last day of december'
-
maximum timestamp, defaults to "now"
- $timezone : string = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Return values
DateTimedayOfMonth()
Get a localized random day of the month.
public
dayOfMonth([mixed $until = 'now' ]) : string
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringdayOfWeek()
Get a localized random day of the week.
public
dayOfWeek([mixed $until = 'now' ]) : string
Uses internal DateTime formatting, hence PHP's internal locale will be used (change using setlocale()
).
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringiso8601()
Get a date string according to the ISO-8601 standard.
public
iso8601([mixed $until = 'now' ]) : string
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringmonth()
Get a random month (numbered).
public
month([mixed $until = 'now' ]) : string
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringmonthName()
Get a random month.
public
monthName([mixed $until = 'now' ]) : string
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringtime()
Get a time string (24h format by default).
public
time([string $format = 'H:i:s' ][, mixed $until = 'now' ]) : string
Parameters
- $format : string = 'H:i:s'
-
DateTime format
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringtimezone()
Get a random timezone, uses `\DateTimeZone::listIdentifiers()` internally.
public
timezone([string $countryCode = null ]) : string
Parameters
- $countryCode : string = null
-
two-letter ISO 3166-1 compatible country code
Return values
stringunixTime()
Get a UNIX (POSIX-compatible) timestamp between January 1, 1970, and `$until`.
public
unixTime([mixed $until = 'now' ]) : int
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
intwithGenerator()
public
withGenerator(Generator $generator) : static
Parameters
- $generator : Generator
Return values
staticyear()
Get a random year between 1970 and `$until`.
public
year([mixed $until = 'now' ]) : string
Parameters
- $until : mixed = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringgetTimestamp()
Get the POSIX-timestamp of a DateTime, int or string.
private
getTimestamp([DateTime|float|int|string $until = 'now' ]) : false|int
Parameters
- $until : DateTime|float|int|string = 'now'
Return values
false|intgetTimestampDateTime()
Get a DateTime created based on a POSIX-timestamp.
private
getTimestampDateTime(int $timestamp) : DateTime
Parameters
- $timestamp : int
-
the UNIX / POSIX-compatible timestamp
Return values
DateTimeresolveTimezone()
private
resolveTimezone(string|null $timezone) : string
Parameters
- $timezone : string|null
Return values
stringsetTimezone()
Internal method to set the timezone on a DateTime object.
private
setTimezone(DateTime $dateTime, string|null $timezone) : DateTime
Parameters
- $dateTime : DateTime
- $timezone : string|null