class DateTime extends Base

Properties

protected Generator $generator from  Base
protected UniqueGenerator $unique from  Base
static protected $century
static protected $defaultTimezone

Methods

__construct(Generator $generator)

No description

from  Base
mixed|null
optional(float|int $weight = 0.5, $default = null)

Chainable method for making any formatter optional.

from  Base
unique(bool $reset = false, int $maxRetries = 10000)

Chainable method for making any formatter unique.

from  Base
valid(Closure $validator = null, int $maxRetries = 10000)

Chainable method for forcing any formatter to return only valid values.

from  Base
static int
randomDigit()

Returns a random number between 0 and 9

from  Base
static int
randomDigitNotNull()

Returns a random number between 1 and 9

from  Base
static int
randomDigitNot(int $except)

Generates a random digit, which cannot be $except

from  Base
static int
randomNumber(int $nbDigits = null, bool $strict = false)

Returns a random integer with 0 to $nbDigits digits.

from  Base
static float
randomFloat(int $nbMaxDecimals = null, int|float $min = 0, int|float $max = null)

Return a random float number

from  Base
static int
numberBetween(int $int1 = 0, int $int2 = 2147483647)

Returns a random number between $int1 and $int2 (any order)

from  Base
static mixed
passthrough(mixed $value)

Returns the passed value

from  Base
static string
randomLetter()

Returns a random letter from a to z

from  Base
static 
randomAscii()

Returns a random ASCII character (excluding accents and special chars)

from  Base
static array
randomElements(array $array = ['a', 'b', 'c'], int $count = 1, bool $allowDuplicates = false)

Returns randomly ordered subsequence of $count elements from a provided array

from  Base
static mixed
randomElement(array|Countable $array = ['a', 'b', 'c'])

Returns a random element from a passed array

from  Base
static int|string|null
randomKey(array $array = [])

Returns a random key from a passed associative array

from  Base
static array|string
shuffle(array|string $arg = '')

Returns a shuffled version of the argument.

from  Base
static array
shuffleArray(array $array = [])

Returns a shuffled version of the array.

from  Base
static string
shuffleString(string $string = '', string $encoding = 'UTF-8')

Returns a shuffled version of the string.

from  Base
static string
numerify(string $string = '###')

Replaces all hash sign ('#') occurrences with a random number Replaces all percentage sign ('%') occurrences with a not null number

from  Base
static string
lexify(string $string = '????')

Replaces all question mark ('?') occurrences with a random letter

from  Base
static string
bothify(string $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

from  Base
static string
asciify(string $string = '****')

Replaces * signs with random numbers and letters and special characters

from  Base
static string
regexify(string $regex = '')

Transforms a basic regular expression into a random string satisfying the expression.

from  Base
static string
toLower(string $string = '')

Converts string to lowercase.

from  Base
static string
toUpper(string $string = '')

Converts string to uppercase.

from  Base
static int
unixTime(DateTime|int|string $max = 'now')

Get a timestamp between January 1, 1970 and now

static DateTime
dateTime(DateTime|int|string $max = 'now', string $timezone = null)

Get a datetime object for a date between January 1, 1970 and now

static DateTime
dateTimeAD(DateTime|int|string $max = 'now', string|null $timezone = null)

Get a datetime object for a date between January 1, 001 and now

static string
iso8601(DateTime|int|string $max = 'now')

get a date string formatted with ISO8601

static string
date(string $format = 'Y-m-d', DateTime|int|string $max = 'now')

Get a date string between January 1, 1970 and now

static string
time(string $format = 'H:i:s', DateTime|int|string $max = 'now')

Get a time string (24h format by default)

static DateTime
dateTimeBetween(DateTime|string $startDate = '-30 years', DateTime|string $endDate = 'now', string|null $timezone = null)

Get a DateTime object based on a random date between two given dates.

static DateTime
dateTimeInInterval(DateTime|string $date = '-30 years', string $interval = '+5 days', string|null $timezone = null)

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

static DateTime
dateTimeThisCentury(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

static DateTime
dateTimeThisDecade(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

static DateTime
dateTimeThisYear(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

static DateTime
dateTimeThisMonth(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

static string
amPm(DateTime|int|string $max = 'now')

No description

static string
dayOfMonth(DateTime|int|string $max = 'now')

No description

static string
dayOfWeek(DateTime|int|string $max = 'now')

No description

static string
month(DateTime|int|string $max = 'now')

No description

static string
monthName(DateTime|int|string $max = 'now')

No description

static string
year(DateTime|int|string $max = 'now')

No description

static string
century()

No description

static string
timezone()

No description

static string|null
getDefaultTimezone()

Gets default time zone.

static void
setDefaultTimezone(string $timezone = null)

Sets default time zone.

static int|false
getMaxTimestamp(DateTime|string|float|int $max = 'now')

No description

Details

in Base at line 31
__construct(Generator $generator)

No description

Parameters

Generator $generator

in Base at line 45
mixed|null optional(float|int $weight = 0.5, $default = null)

Chainable method for making any formatter optional.

Parameters

float|int $weight

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

Return Value

mixed|null

in Base at line 76
UniqueGenerator unique(bool $reset = false, int $maxRetries = 10000)

Chainable method for making any formatter unique.

// will never return twice the same value
$faker->unique()->randomElement(array(1, 2, 3));

Parameters

bool $reset

If set to true, resets the list of existing values

int $maxRetries

Maximum number of retries to find a unique value, After which an OverflowException is thrown.

Return Value

UniqueGenerator

A proxy class returning only non-existing values

Exceptions

OverflowException

in Base at line 108
ValidGenerator valid(Closure $validator = null, int $maxRetries = 10000)

Chainable method for forcing any formatter to return only valid values.

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

Closure $validator

A function returning true for valid values

int $maxRetries

Maximum number of retries to find a unique value, After which an OverflowException is thrown.

Return Value

ValidGenerator

A proxy class returning only valid values

Exceptions

OverflowException

in Base at line 118
static int randomDigit()

Returns a random number between 0 and 9

Return Value

int

in Base at line 128
static int randomDigitNotNull()

Returns a random number between 1 and 9

Return Value

int

in Base at line 139
static int randomDigitNot(int $except)

Generates a random digit, which cannot be $except

Parameters

int $except

Return Value

int

in Base at line 159
static int randomNumber(int $nbDigits = null, bool $strict = false)

Returns a random integer with 0 to $nbDigits digits.

The maximum value returned is mt_getrandmax()

Parameters

int $nbDigits

Defaults to a random number between 1 and 9

bool $strict

Whether the returned number should have exactly $nbDigits

Return Value

int

Examples

79907610

in Base at line 188
static float randomFloat(int $nbMaxDecimals = null, int|float $min = 0, int|float $max = null)

Return a random float number

Parameters

int $nbMaxDecimals
int|float $min
int|float $max

Return Value

float

Examples

48.8932

in Base at line 219
static int numberBetween(int $int1 = 0, int $int2 = 2147483647)

Returns a random number between $int1 and $int2 (any order)

Parameters

int $int1

default to 0

int $int2

defaults to 32 bit max integer, ie 2147483647

Return Value

int

Examples

79907610

in Base at line 233
static mixed passthrough(mixed $value)

Returns the passed value

Parameters

mixed $value

Return Value

mixed

in Base at line 243
static string randomLetter()

Returns a random letter from a to z

Return Value

string

in Base at line 251
static randomAscii()

Returns a random ASCII character (excluding accents and special chars)

in Base at line 266
static array randomElements(array $array = ['a', 'b', 'c'], int $count = 1, bool $allowDuplicates = false)

Returns randomly ordered subsequence of $count elements from a provided array

Parameters

array $array

Array to take elements from. Defaults to a-c

int $count

Number of elements to take.

bool $allowDuplicates

Allow elements to be picked several times. Defaults to false

Return Value

array

New array with $count elements from $array

Exceptions

LengthException

in Base at line 312
static mixed randomElement(array|Countable $array = ['a', 'b', 'c'])

Returns a random element from a passed array

Parameters

array|Countable $array

Return Value

mixed

in Base at line 328
static int|string|null randomKey(array $array = [])

Returns a random key from a passed associative array

Parameters

array $array

Return Value

int|string|null

in Base at line 353
static array|string shuffle(array|string $arg = '')

Returns a shuffled version of the argument.

This function accepts either an array, or a string.

Parameters

array|string $arg

The set to shuffle

Return Value

array|string

The shuffled set

See also

shuffleArray()
shuffleString()

Examples

$faker->shuffle([1, 2, 3]); // [2, 1, 3]
$faker->shuffle('hello, world'); // 'rlo,h eold!lw'

in Base at line 379
static array shuffleArray(array $array = [])

Returns a shuffled version of the array.

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

The set to shuffle

Return Value

array

The shuffled set

Examples

$faker->shuffleArray([1, 2, 3]); // [2, 1, 3]

in Base at line 420
static string shuffleString(string $string = '', string $encoding = 'UTF-8')

Returns a shuffled version of the 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

string $encoding

The string encoding (defaults to UTF-8)

Return Value

string

The shuffled set

Examples

$faker->shuffleString('hello, world'); // 'rlo,h eold!lw'

in Base at line 443
static string numerify(string $string = '###')

Replaces all hash sign ('#') occurrences with a random number Replaces all percentage sign ('%') occurrences with a not null number

Parameters

string $string

String that needs to bet parsed

Return Value

string

in Base at line 477
static string lexify(string $string = '????')

Replaces all question mark ('?') occurrences with a random letter

Parameters

string $string

String that needs to bet parsed

Return Value

string

in Base at line 489
static string bothify(string $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

Parameters

string $string

String that needs to bet parsed

Return Value

string

in Base at line 505
static string asciify(string $string = '****')

Replaces * signs with random numbers and letters and special characters

Parameters

string $string

String that needs to bet parsed

Return Value

string

Examples

$faker->asciify(''********'); // "s5'G!uC3"

in Base at line 536
static string regexify(string $regex = '')

Transforms a basic regular expression into a random string satisfying the expression.

Parameters

string $regex

A regular expression (delimiters are optional)

Return Value

string

See also

https://github.com/icomefromthenet/ReverseRegex for a more robust implementation

Examples

$faker->regexify('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'); // sm0@y8k96a.ej

Regex delimiters '/.../' and begin/end markers '^...$' are ignored.

Only supports a small subset of the regex syntax. For instance,
unicode, negated classes, unbounded ranges, subpatterns, back references,
assertions, recursive patterns, and comments are not supported. Escaping
support is extremely fragile.

This method is also VERY slow. Use it only when no other formatter
can generate the fake data you want. For instance, prefer calling
`$faker->email` rather than `regexify` with the previous regular
expression.

Also note than `bothify` can probably do most of what this method does,
but much faster. For instance, for a dummy email generation, try
`$faker->bothify('?????????@???.???')`.

in Base at line 590
static string toLower(string $string = '')

Converts string to lowercase.

Uses mb_string extension if available.

Parameters

string $string

String that should be converted to lowercase

Return Value

string

in Base at line 602
static string toUpper(string $string = '')

Converts string to uppercase.

Uses mb_string extension if available.

Parameters

string $string

String that should be converted to uppercase

Return Value

string

at line 19
static int unixTime(DateTime|int|string $max = 'now')

Get a timestamp between January 1, 1970 and now

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

int

Examples

1061306726

at line 34
static DateTime dateTime(DateTime|int|string $max = 'now', string $timezone = null)

Get a datetime object for a date between January 1, 1970 and now

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

string $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

See also

http://php.net/manual/en/timezones.php
http://php.net/manual/en/function.date-default-timezone-get.php

Examples

DateTime('2005-08-16 20:39:21')

at line 52
static DateTime dateTimeAD(DateTime|int|string $max = 'now', string|null $timezone = null)

Get a datetime object for a date between January 1, 001 and now

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

string|null $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

See also

http://php.net/manual/en/timezones.php
http://php.net/manual/en/function.date-default-timezone-get.php

Examples

DateTime('1265-03-22 21:15:52')

at line 68
static string iso8601(DateTime|int|string $max = 'now')

get a date string formatted with ISO8601

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'2003-10-21T16:05:52+0000'

at line 81
static string date(string $format = 'Y-m-d', DateTime|int|string $max = 'now')

Get a date string between January 1, 1970 and now

Parameters

string $format
DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'2008-11-27'

at line 94
static string time(string $format = 'H:i:s', DateTime|int|string $max = 'now')

Get a time string (24h format by default)

Parameters

string $format
DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'15:02:34'

at line 111
static DateTime dateTimeBetween(DateTime|string $startDate = '-30 years', DateTime|string $endDate = 'now', string|null $timezone = null)

Get a DateTime object based on a random date between two given dates.

Accepts date strings that can be recognized by strtotime().

Parameters

DateTime|string $startDate

Defaults to 30 years ago

DateTime|string $endDate

Defaults to "now"

string|null $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

See also

http://php.net/manual/en/timezones.php
http://php.net/manual/en/function.date-default-timezone-get.php

Examples

DateTime('1999-02-02 11:42:52')

at line 141
static DateTime dateTimeInInterval(DateTime|string $date = '-30 years', string $interval = '+5 days', string|null $timezone = null)

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

Parameters

DateTime|string $date

Defaults to 30 years ago

string $interval

Defaults to 5 days after

string|null $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

See also

http://php.net/manual/en/timezones.php
http://php.net/manual/en/function.date-default-timezone-get.php

Examples

dateTimeInInterval('1999-02-02 11:42:52', '+ 5 days')

at line 164
static DateTime dateTimeThisCentury(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

string|null $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

Examples

DateTime('1964-04-04 11:02:02')

at line 175
static DateTime dateTimeThisDecade(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

string|null $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

Examples

DateTime('2010-03-10 05:18:58')

at line 186
static DateTime dateTimeThisYear(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

string|null $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

Examples

DateTime('2011-09-19 09:24:37')

at line 197
static DateTime dateTimeThisMonth(DateTime|int|string $max = 'now', string|null $timezone = null)

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

string|null $timezone

time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of date_default_timezone_get

Return Value

DateTime

Examples

DateTime('2011-10-05 12:51:46')

at line 207
static string amPm(DateTime|int|string $max = 'now')

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'am'

at line 217
static string dayOfMonth(DateTime|int|string $max = 'now')

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'22'

at line 227
static string dayOfWeek(DateTime|int|string $max = 'now')

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'Tuesday'

at line 237
static string month(DateTime|int|string $max = 'now')

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'7'

at line 247
static string monthName(DateTime|int|string $max = 'now')

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'September'

at line 257
static string year(DateTime|int|string $max = 'now')

No description

Parameters

DateTime|int|string $max

maximum timestamp used as random end limit, default to "now"

Return Value

string

Examples

'1673'

at line 266
static string century()

No description

Return Value

string

Examples

'XVII'

at line 275
static string timezone()

No description

Return Value

string

Examples

'Europe/Paris'

at line 285
static string|null getDefaultTimezone()

Gets default time zone.

Return Value

string|null

at line 297
static void setDefaultTimezone(string $timezone = null)

Sets default time zone.

Parameters

string $timezone

Return Value

void

at line 306
static protected int|false getMaxTimestamp(DateTime|string|float|int $max = 'now')

No description

Parameters

DateTime|string|float|int $max

Return Value

int|false