1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-19 23:09:47 +01:00

Add DateTime provider.

Closes #2
This commit is contained in:
Francois Zaninotto 2011-10-16 22:07:44 +02:00
parent 8b07a3b5e8
commit faca7e31db
4 changed files with 123 additions and 1 deletions

View File

@ -38,6 +38,26 @@ echo $faker->lorem;
Each of the generator properties (like `name`, `address`, and `lorem`) are called "formatters". A faker generator has many of them, packaged in "providers". Here is a list of the bundled formatters in the default locale.
### `Faker\Provider\DateTime`
amPm() // 'am'
century() // 'V'
date() // '2008-11-27'
dateTime() // 2005-08-16 20:39:21
dateTimeBetween() // 1999-02-02 11:42:52
dateTimeThisCentury() // 1964-04-04 11:02:02
dateTimeThisDecade() // 2010-03-10 05:18:58
dateTimeThisMonth() // 2011-10-05 12:51:46
dateTimeThisYear() // 2011-09-19 09:24:37
dayOfMonth() // '29'
dayOfWeek() // 'Thursday'
iso8601() // '2003-10-21T16:05:52+0000'
month() // '06'
monthName() // 'November'
time() // '15:02:34'
unixTime() // 1061306726
year() // '1991'
### `Faker\Provider\Internet`
domainName() // 'mueller.info'

View File

@ -26,6 +26,8 @@ class Documentor
$example = $this->generator->format($methodName);
if (is_array($example)) {
$example = "array('". join("', '", $example) . "')";
} elseif($example instanceof \DateTime) {
$example = $example->format('Y-m-d H:i:s');
} elseif (is_string($example)) {
$example = var_export($example, true);
}

View File

@ -8,7 +8,7 @@ class Factory
{
const DEFAULT_LOCALE = 'en_US';
protected static $defaultProviders = array('Name', 'Address', 'PhoneNumber', 'Company', 'Lorem', 'Internet');
protected static $defaultProviders = array('Name', 'Address', 'PhoneNumber', 'Company', 'Lorem', 'Internet', 'DateTime');
public static function create($locale = self::DEFAULT_LOCALE)
{

100
src/Provider/DateTime.php Normal file
View File

@ -0,0 +1,100 @@
<?php
namespace Faker\Provider;
require_once __DIR__ . '/Base.php';
class DateTime extends \Faker\Provider\Base
{
protected static $century = array('I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII','XIII','XIV','XV','XVI','XVII','XVIII','XIX','XX','XX1');
public static function unixTime()
{
return mt_rand(0, time());
}
public static function dateTime()
{
return new \DateTime('@' . static::unixTime());
}
public static function iso8601()
{
return static::date(\DateTime::ISO8601);
}
public static function date($format = 'Y-m-d')
{
return static::dateTime()->format($format);
}
public static function time($format = 'H:i:s')
{
return static::dateTime()->format($format);
}
public static function dateTimeBetween($startDate = "-30 years", $endDate = "now")
{
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
$timestamp = mt_rand($startTimestamp, $endTimestamp);
return new \DateTime('@' . $timestamp);
}
public static function dateTimeThisCentury()
{
return static::dateTimeBetween("-100 year");
}
public static function dateTimeThisDecade()
{
return static::dateTimeBetween("-10 year");
}
public static function dateTimeThisYear()
{
return static::dateTimeBetween("-1 year");
}
public static function dateTimeThisMonth()
{
return static::dateTimeBetween("-1 month");
}
public static function amPm()
{
return static::dateTime()->format('a');
}
public static function dayOfMonth()
{
return static::dateTime()->format('d');
}
public static function dayOfWeek()
{
return static::dateTime()->format('l');
}
public static function month()
{
return static::dateTime()->format('m');
}
public static function monthName()
{
return static::dateTime()->format('F');
}
public static function year()
{
return static::dateTime()->format('Y');
}
public static function century()
{
return static::randomElement(static::$century);
}
}