HumHub Documentation (unofficial)

DateTime
in package
implements DateTimeExtension, GeneratorAwareExtension uses GeneratorAwareExtensionTrait

FinalYes
Tags
experimental

This class is experimental and does not fall under our BC promise

since
1.20.0

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

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
string

century()

Get a random century, formatted as Roman numerals.

public century() : string
Return values
string

date()

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
string

dateTime()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeAD()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeBetween()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeInInterval()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeThisCentury()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeThisDecade()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeThisMonth()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeThisWeek()

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 and date_default_timezone_get().

Return values
DateTime

dateTimeThisYear()

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 and date_default_timezone_get().

Return values
DateTime

dayOfMonth()

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
string

dayOfWeek()

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
string

iso8601()

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
string

month()

Get a random month (numbered).

public month([mixed $until = 'now' ]) : string
Parameters
$until : mixed = 'now'

maximum timestamp, defaults to "now"

Return values
string

monthName()

Get a random month.

public monthName([mixed $until = 'now' ]) : string
Parameters
$until : mixed = 'now'

maximum timestamp, defaults to "now"

Return values
string

time()

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
string

timezone()

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
string

unixTime()

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
int

year()

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
string

getTimestamp()

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|int

getTimestampDateTime()

Get a DateTime created based on a POSIX-timestamp.

private getTimestampDateTime(int $timestamp) : DateTime
Parameters
$timestamp : int

the UNIX / POSIX-compatible timestamp

Return values
DateTime

resolveTimezone()

private resolveTimezone(string|null $timezone) : string
Parameters
$timezone : string|null
Return values
string

setTimezone()

Internal method to set the timezone on a DateTime object.

private setTimezone(DateTime $dateTime, string|null $timezone) : DateTime
Parameters
$dateTime : DateTime
$timezone : string|null
Return values
DateTime

        
On this page

Search results