DateTimeExtension
in
FakerPHP extension for Date-related randomization.
Functions accepting a date string use the strtotime()
function internally.
Tags
Table of Contents
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`.
- year() : string
- Get a random year between 1970 and `$until`.
Methods
amPm()
Get a string containing either "am" or "pm".
public
amPm([DateTime|int|string $until = 'now' ]) : string
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Tags
Return values
stringcentury()
Get a random century, formatted as Roman numerals.
public
century() : string
Tags
Return values
stringdate()
Get a date string between January 1, 1970, and `$until`.
public
date([string $format = 'Y-m-d' ][, DateTime|int|string $until = 'now' ]) : string
Parameters
- $format : string = 'Y-m-d'
-
DateTime format
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Tags
Return values
stringdateTime()
Get a DateTime object between January 1, 1970, and `$until` (defaults to "now").
public
dateTime([DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeAD()
Get a DateTime object for a date between January 1, 0001, and now.
public
dateTimeAD([DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeBetween()
Get a DateTime object a random date between `$from` and `$until`.
public
dateTimeBetween([DateTime|string $from = '-30 years' ][, DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Accepts date strings that can be recognized by strtotime()
.
Parameters
- $from : DateTime|string = '-30 years'
-
defaults to 30 years ago
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeInInterval()
Get a DateTime object based on a random date between `$from` and an interval.
public
dateTimeInInterval([DateTime|int|string $from = '-30 years' ][, string $interval = '+5 days' ][, string|null $timezone = null ]) : DateTime
Accepts date string that can be recognized by strtotime()
.
Parameters
- $from : DateTime|int|string = '-30 years'
-
defaults to 30 years ago
- $interval : string = '+5 days'
-
defaults to 5 days after
- $timezone : string|null = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeThisCentury()
Get a date time object somewhere inside the current century.
public
dateTimeThisCentury([DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeThisDecade()
Get a date time object somewhere inside the current decade.
public
dateTimeThisDecade([DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeThisMonth()
Get a date time object somewhere inside the current month.
public
dateTimeThisMonth([DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeThisWeek()
Get a date time object somewhere inside the current week.
public
dateTimeThisWeek([DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
zone timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedateTimeThisYear()
Get a date time object somewhere inside the current year.
public
dateTimeThisYear([DateTime|int|string $until = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
- $timezone : string|null = null
-
timezone for generated date, fallback to
DateTime::$defaultTimezone
anddate_default_timezone_get()
.
Tags
Return values
DateTimedayOfMonth()
Get a localized random day of the month.
public
dayOfMonth([DateTime|int|string $until = 'now' ]) : string
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Tags
Return values
stringdayOfWeek()
Get a localized random day of the week.
public
dayOfWeek([DateTime|int|string $until = 'now' ]) : string
Uses internal DateTime formatting, hence PHP's internal locale will be used (change using setlocale()
).
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Tags
Return values
stringiso8601()
Get a date string according to the ISO-8601 standard.
public
iso8601([DateTime|int|string $until = 'now' ]) : string
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Return values
stringmonth()
Get a random month (numbered).
public
month([DateTime|int|string $until = 'now' ]) : string
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Tags
Return values
stringmonthName()
Get a random month.
public
monthName([DateTime|int|string $until = 'now' ]) : string
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Tags
Return values
stringtime()
Get a time string (24h format by default).
public
time([string $format = 'H:i:s' ][, DateTime|int|string $until = 'now' ]) : string
Parameters
- $format : string = 'H:i:s'
-
DateTime format
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Tags
Return values
stringtimezone()
Get a random timezone, uses `\DateTimeZone::listIdentifiers()` internally.
public
timezone([string|null $countryCode = null ]) : string
Parameters
- $countryCode : string|null = null
-
two-letter ISO 3166-1 compatible country code
Tags
Return values
stringunixTime()
Get a UNIX (POSIX-compatible) timestamp between January 1, 1970, and `$until`.
public
unixTime([DateTime|int|string $until = 'now' ]) : int
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"
Return values
intyear()
Get a random year between 1970 and `$until`.
public
year([DateTime|int|string $until = 'now' ]) : string
Parameters
- $until : DateTime|int|string = 'now'
-
maximum timestamp, defaults to "now"