DateTime
extends Base
in package
Table of Contents
- $century : mixed
- $defaultTimezone : mixed
- $generator : Generator
- $unique : UniqueGenerator
- __construct() : mixed
- amPm() : string
- asciify() : string
- Replaces * signs with random numbers and letters and special characters
- bothify() : string
- Replaces hash signs ('#') and question marks ('?') with random numbers and letters An asterisk ('*') is replaced with either a random number or a random letter
- century() : string
- date() : string
- Get a date string between January 1, 1970 and now
- dateTime() : DateTime
- Get a datetime object for a date between January 1, 1970 and now
- dateTimeAD() : DateTime
- Get a datetime object for a date between January 1, 001 and now
- dateTimeBetween() : DateTime
- Get a DateTime object based on a random date between two given dates.
- dateTimeInInterval() : DateTime
- Get a DateTime object based on a random date between one given date and an interval Accepts date string that can be recognized by strtotime().
- dateTimeThisCentury() : DateTime
- dateTimeThisDecade() : DateTime
- dateTimeThisMonth() : DateTime
- dateTimeThisYear() : DateTime
- dayOfMonth() : string
- dayOfWeek() : string
- getDefaultTimezone() : string|null
- Gets default time zone.
- iso8601() : string
- get a date string formatted with ISO8601
- lexify() : string
- Replaces all question mark ('?') occurrences with a random letter
- month() : string
- monthName() : string
- numberBetween() : int
- Returns a random number between $int1 and $int2 (any order)
- numerify() : string
- Replaces all hash sign ('#') occurrences with a random number Replaces all percentage sign ('%') occurrences with a not null number
- optional() : mixed|null
- Chainable method for making any formatter optional.
- passthrough() : mixed
- Returns the passed value
- randomAscii() : string
- Returns a random ASCII character (excluding accents and special chars)
- randomDigit() : int
- Returns a random number between 0 and 9
- randomDigitNot() : int
- Generates a random digit, which cannot be $except
- randomDigitNotNull() : int
- Returns a random number between 1 and 9
- randomElement() : mixed
- Returns a random element from a passed array
- randomElements() : array<string|int, mixed>
- Returns randomly ordered subsequence of $count elements from a provided array
- randomFloat() : float
- Return a random float number
- randomKey() : int|string|null
- Returns a random key from a passed associative array
- randomLetter() : string
- Returns a random letter from a to z
- randomNumber() : int
- Returns a random integer with 0 to $nbDigits digits.
- regexify() : string
- Transforms a basic regular expression into a random string satisfying the expression.
- setDefaultTimezone() : mixed
- Sets default time zone.
- shuffle() : array<string|int, mixed>|string
- Returns a shuffled version of the argument.
- shuffleArray() : array<string|int, mixed>
- Returns a shuffled version of the array.
- shuffleString() : string
- Returns a shuffled version of the string.
- time() : string
- Get a time string (24h format by default)
- timezone() : string
- toLower() : string
- Converts string to lowercase.
- toUpper() : string
- Converts string to uppercase.
- unique() : UniqueGenerator
- Chainable method for making any formatter unique.
- unixTime() : int
- Get a timestamp between January 1, 1970 and now
- valid() : ValidGenerator
- Chainable method for forcing any formatter to return only valid values.
- year() : string
- getMaxTimestamp() : false|int
- replaceWildcard() : mixed
- resolveTimezone() : string|null
- setTimezone() : DateTime
- Internal method to set the time zone on a DateTime.
Properties
$century
protected
static mixed
$century
= ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX', 'XXI']
$defaultTimezone
protected
static mixed
$defaultTimezone
= null
$generator
protected
Generator
$generator
$unique
protected
UniqueGenerator
$unique
Methods
__construct()
public
__construct(Generator $generator) : mixed
Parameters
- $generator : Generator
Return values
mixed —amPm()
public
static amPm([DateTime|int|string $max = 'now' ]) : string
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —asciify()
Replaces * signs with random numbers and letters and special characters
public
static asciify([string $string = '****' ]) : string
Parameters
- $string : string = '****'
-
String that needs to bet parsed
Tags
Return values
string —bothify()
Replaces hash signs ('#') and question marks ('?') with random numbers and letters An asterisk ('*') is replaced with either a random number or a random letter
public
static bothify([string $string = '## ??' ]) : string
Parameters
- $string : string = '## ??'
-
String that needs to bet parsed
Return values
string —century()
public
static century() : string
Tags
Return values
string —date()
Get a date string between January 1, 1970 and now
public
static date([string $format = 'Y-m-d' ][, DateTime|int|string $max = 'now' ]) : string
Parameters
- $format : string = 'Y-m-d'
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —dateTime()
Get a datetime object for a date between January 1, 1970 and now
public
static dateTime([DateTime|int|string $max = 'now' ][, string $timezone = null ]) : DateTime
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
- $timezone : string = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dateTimeAD()
Get a datetime object for a date between January 1, 001 and now
public
static dateTimeAD([DateTime|int|string $max = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
- $timezone : string|null = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dateTimeBetween()
Get a DateTime object based on a random date between two given dates.
public
static dateTimeBetween([DateTime|string $startDate = '-30 years' ][, DateTime|string $endDate = 'now' ][, string|null $timezone = null ]) : DateTime
Accepts date strings that can be recognized by strtotime().
Parameters
- $startDate : DateTime|string = '-30 years'
-
Defaults to 30 years ago
- $endDate : DateTime|string = 'now'
-
Defaults to "now"
- $timezone : string|null = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dateTimeInInterval()
Get a DateTime object based on a random date between one given date and an interval Accepts date string that can be recognized by strtotime().
public
static dateTimeInInterval([DateTime|string $date = '-30 years' ][, string $interval = '+5 days' ][, string|null $timezone = null ]) : DateTime
Parameters
- $date : DateTime|string = '-30 years'
-
Defaults to 30 years ago
- $interval : string = '+5 days'
-
Defaults to 5 days after
- $timezone : string|null = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dateTimeThisCentury()
public
static dateTimeThisCentury([DateTime|int|string $max = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
- $timezone : string|null = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dateTimeThisDecade()
public
static dateTimeThisDecade([DateTime|int|string $max = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
- $timezone : string|null = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dateTimeThisMonth()
public
static dateTimeThisMonth([DateTime|int|string $max = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
- $timezone : string|null = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dateTimeThisYear()
public
static dateTimeThisYear([DateTime|int|string $max = 'now' ][, string|null $timezone = null ]) : DateTime
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
- $timezone : string|null = null
-
time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of
date_default_timezone_get
Tags
Return values
DateTime —dayOfMonth()
public
static dayOfMonth([DateTime|int|string $max = 'now' ]) : string
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —dayOfWeek()
public
static dayOfWeek([DateTime|int|string $max = 'now' ]) : string
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —getDefaultTimezone()
Gets default time zone.
public
static getDefaultTimezone() : string|null
Return values
string|null —iso8601()
get a date string formatted with ISO8601
public
static iso8601([DateTime|int|string $max = 'now' ]) : string
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —lexify()
Replaces all question mark ('?') occurrences with a random letter
public
static lexify([string $string = '????' ]) : string
Parameters
- $string : string = '????'
-
String that needs to bet parsed
Return values
string —month()
public
static month([DateTime|int|string $max = 'now' ]) : string
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —monthName()
public
static monthName([DateTime|int|string $max = 'now' ]) : string
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —numberBetween()
Returns a random number between $int1 and $int2 (any order)
public
static numberBetween(int $int1[, int $int2 = 2147483647 ]) : int
Parameters
- $int1 : int
-
default to 0
- $int2 : int = 2147483647
-
defaults to 32 bit max integer, ie 2147483647
Tags
Return values
int —numerify()
Replaces all hash sign ('#') occurrences with a random number Replaces all percentage sign ('%') occurrences with a not null number
public
static numerify([string $string = '###' ]) : string
Parameters
- $string : string = '###'
-
String that needs to bet parsed
Return values
string —optional()
Chainable method for making any formatter optional.
public
optional([float|int $weight = 0.5 ][, mixed $default = null ]) : mixed|null
Parameters
- $weight : float|int = 0.5
-
Set the probability of receiving a null value. "0" will always return null, "1" will always return the generator. If $weight is an integer value, then the same system works between 0 (always get false) and 100 (always get true).
- $default : mixed = null
Return values
mixed|null —passthrough()
Returns the passed value
public
static passthrough(mixed $value) : mixed
Parameters
- $value : mixed
Return values
mixed —randomAscii()
Returns a random ASCII character (excluding accents and special chars)
public
static randomAscii() : string
Return values
string —randomDigit()
Returns a random number between 0 and 9
public
static randomDigit() : int
Return values
int —randomDigitNot()
Generates a random digit, which cannot be $except
public
static randomDigitNot(int $except) : int
Parameters
- $except : int
Return values
int —randomDigitNotNull()
Returns a random number between 1 and 9
public
static randomDigitNotNull() : int
Return values
int —randomElement()
Returns a random element from a passed array
public
static randomElement([array<string|int, mixed> $array = ['a', 'b', 'c'] ]) : mixed
Parameters
- $array : array<string|int, mixed> = ['a', 'b', 'c']
Return values
mixed —randomElements()
Returns randomly ordered subsequence of $count elements from a provided array
public
static randomElements([array<string|int, mixed> $array = ['a', 'b', 'c'] ][, int $count = 1 ][, bool $allowDuplicates = false ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed> = ['a', 'b', 'c']
-
Array to take elements from. Defaults to a-c
- $count : int = 1
-
Number of elements to take.
- $allowDuplicates : bool = false
-
Allow elements to be picked several times. Defaults to false
Tags
Return values
array<string|int, mixed> —New array with $count elements from $array
randomFloat()
Return a random float number
public
static randomFloat([int $nbMaxDecimals = null ], float|int $min[, float|int $max = null ]) : float
Parameters
- $nbMaxDecimals : int = null
- $min : float|int
- $max : float|int = null
Tags
Return values
float —randomKey()
Returns a random key from a passed associative array
public
static randomKey([array<string|int, mixed> $array = [] ]) : int|string|null
Parameters
- $array : array<string|int, mixed> = []
Return values
int|string|null —randomLetter()
Returns a random letter from a to z
public
static randomLetter() : string
Return values
string —randomNumber()
Returns a random integer with 0 to $nbDigits digits.
public
static randomNumber([int $nbDigits = null ][, bool $strict = false ]) : int
The maximum value returned is mt_getrandmax()
Parameters
- $nbDigits : int = null
-
Defaults to a random number between 1 and 9
- $strict : bool = false
-
Whether the returned number should have exactly $nbDigits
Tags
Return values
int —regexify()
Transforms a basic regular expression into a random string satisfying the expression.
public
static regexify([string $regex = '' ]) : string
Parameters
- $regex : string = ''
-
A regular expression (delimiters are optional)
Tags
Return values
string —setDefaultTimezone()
Sets default time zone.
public
static setDefaultTimezone([string $timezone = null ]) : mixed
Parameters
- $timezone : string = null
Return values
mixed —shuffle()
Returns a shuffled version of the argument.
public
static shuffle([array<string|int, mixed>|string $arg = '' ]) : array<string|int, mixed>|string
This function accepts either an array, or a string.
Parameters
- $arg : array<string|int, mixed>|string = ''
-
The set to shuffle
Tags
Return values
array<string|int, mixed>|string —The shuffled set
shuffleArray()
Returns a shuffled version of the array.
public
static shuffleArray([array<string|int, mixed> $array = [] ]) : array<string|int, mixed>
This function does not mutate the original array. It uses the Fisher–Yates algorithm, which is unbiased, together with a Mersenne twister random generator. This function is therefore more random than PHP's shuffle() function, and it is seedable.
Parameters
- $array : array<string|int, mixed> = []
-
The set to shuffle
Tags
Return values
array<string|int, mixed> —The shuffled set
shuffleString()
Returns a shuffled version of the string.
public
static shuffleString([string $string = '' ][, string $encoding = 'UTF-8' ]) : string
This function does not mutate the original string. It uses the Fisher–Yates algorithm, which is unbiased, together with a Mersenne twister random generator. This function is therefore more random than PHP's shuffle() function, and it is seedable. Additionally, it is UTF8 safe if the mb extension is available.
Parameters
- $string : string = ''
-
The set to shuffle
- $encoding : string = 'UTF-8'
-
The string encoding (defaults to UTF-8)
Tags
Return values
string —The shuffled set
time()
Get a time string (24h format by default)
public
static time([string $format = 'H:i:s' ][, DateTime|int|string $max = 'now' ]) : string
Parameters
- $format : string = 'H:i:s'
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —timezone()
public
static timezone() : string
Tags
Return values
string —toLower()
Converts string to lowercase.
public
static toLower([string $string = '' ]) : string
Uses mb_string extension if available.
Parameters
- $string : string = ''
-
String that should be converted to lowercase
Return values
string —toUpper()
Converts string to uppercase.
public
static toUpper([string $string = '' ]) : string
Uses mb_string extension if available.
Parameters
- $string : string = ''
-
String that should be converted to uppercase
Return values
string —unique()
Chainable method for making any formatter unique.
public
unique([bool $reset = false ][, int $maxRetries = 10000 ]) : UniqueGenerator
// will never return twice the same value
$faker->unique()->randomElement(array(1, 2, 3));
Parameters
- $reset : bool = false
-
If set to true, resets the list of existing values
- $maxRetries : int = 10000
-
Maximum number of retries to find a unique value, After which an OverflowException is thrown.
Tags
Return values
UniqueGenerator —A proxy class returning only non-existing values
unixTime()
Get a timestamp between January 1, 1970 and now
public
static unixTime([DateTime|int|string $max = 'now' ]) : int
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
int —valid()
Chainable method for forcing any formatter to return only valid values.
public
valid([Closure $validator = null ][, int $maxRetries = 10000 ]) : ValidGenerator
The value validity is determined by a function passed as first argument.
$values = array();
$evenValidator = function ($digit) {
return $digit % 2 === 0;
};
for ($i=0; $i < 10; $i++) {
$values []= $faker->valid($evenValidator)->randomDigit;
}
print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6]
Parameters
- $validator : Closure = null
-
A function returning true for valid values
- $maxRetries : int = 10000
-
Maximum number of retries to find a unique value, After which an OverflowException is thrown.
Tags
Return values
ValidGenerator —A proxy class returning only valid values
year()
public
static year([DateTime|int|string $max = 'now' ]) : string
Parameters
- $max : DateTime|int|string = 'now'
-
maximum timestamp used as random end limit, default to "now"
Tags
Return values
string —getMaxTimestamp()
protected
static getMaxTimestamp([DateTime|float|int|string $max = 'now' ]) : false|int
Parameters
- $max : DateTime|float|int|string = 'now'
Return values
false|int —replaceWildcard()
private
static replaceWildcard(mixed $string[, mixed $wildcard = '#' ][, mixed $callback = 'static::randomDigit' ]) : mixed
Parameters
- $string : mixed
- $wildcard : mixed = '#'
- $callback : mixed = 'static::randomDigit'
Return values
mixed —resolveTimezone()
private
static resolveTimezone(string|null $timezone) : string|null
Parameters
- $timezone : string|null
Return values
string|null —setTimezone()
Internal method to set the time zone on a DateTime.
private
static setTimezone(DateTime $dt, string|null $timezone) : DateTime
Parameters
- $dt : DateTime
- $timezone : string|null