asDate() public méthode

Formats the value as a date.
See also: dateFormat
public asDate ( integer | string | DateTim\DateTime $value, string $format = null ) : string
$value integer | string | DateTim\DateTime the value to be formatted. The following types of value are supported: - an integer representing a UNIX timestamp - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object
$format string the format used to convert the value into a date string. If null, [[dateFormat]] will be used. This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the PHP [date()](http://php.net/manual/en/function.date.php)-function.
Résultat string the formatted result.
 public function testDate()
 {
     $time = time();
     $this->assertSame(date('n/j/y', $time), $this->formatter->asDate($time));
     $this->assertSame(date('F j, Y', $time), $this->formatter->asDate($time, 'long'));
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
 }
Exemple #2
0
 public function testIntlUtf8Ru()
 {
     $this->assertEquals('d M Y \\г.', FormatConverter::convertDateIcuToPhp('dd MMM y \'г\'.', 'date', 'ru-RU'));
     $this->assertEquals('dd M yy \'г\'.', FormatConverter::convertDateIcuToJui('dd MMM y \'г\'.', 'date', 'ru-RU'));
     $formatter = new Formatter(['locale' => 'ru-RU']);
     $this->assertEquals('24 авг 2014 г.', $formatter->asDate('2014-8-24', 'dd MMM y \'г\'.'));
 }
Exemple #3
0
 public function asDate($value, $format = null)
 {
     if ($value instanceof \MongoDate) {
         $value = $value->sec;
     }
     return parent::asDate($value, $format);
 }
Exemple #4
0
 /**
  * Test for dates after 2038
  * https://github.com/yiisoft/yii2/issues/3126
  */
 public function testDateRangeHigh()
 {
     if (PHP_INT_SIZE < 8) {
         $this->markTestSkipped('Dates > 2038 only work on PHP compiled with 64bit support.');
     }
     $this->assertSame('17-12-2048', $this->formatter->asDate('2048-12-17', 'dd-MM-yyyy'));
 }
 public function asDate($value, $format = null)
 {
     if ($value == 0) {
         return '-';
     }
     return parent::asDate($value, $format);
 }
Exemple #6
0
 public function asDate($value, $format = null)
 {
     if ($this->calendar->code == 'gregorian') {
         return parent::asDate($value, $format);
     }
     if ($format === null) {
         $format = $this->dateFormat;
     }
     return $this->formatDateTimeValue($value, $format, 'date');
 }
 public function testDateOnlyValues()
 {
     date_default_timezone_set('Pacific/Kiritimati');
     // timzones with exactly 24h difference, ensure this test does not fail on a certain time
     $this->formatter->defaultTimeZone = 'Pacific/Kiritimati';
     // always UTC+14
     $this->formatter->timeZone = 'Pacific/Honolulu';
     // always UTC-10
     // when timezone conversion is made on this date, it will result in 2014-07-31 to be returned.
     // ensure this does not happen on date only values
     $this->assertSame('2014-08-01', $this->formatter->asDate('2014-08-01', 'yyyy-MM-dd'));
     date_default_timezone_set('Pacific/Honolulu');
     $this->formatter->defaultTimeZone = 'Pacific/Honolulu';
     // always UTC-10
     $this->formatter->timeZone = 'Pacific/Kiritimati';
     // always UTC+14
     $this->assertSame('2014-08-01', $this->formatter->asDate('2014-08-01', 'yyyy-MM-dd'));
 }
Exemple #8
0
 /**
  * Test timezones with input date and time in other timezones
  * @dataProvider provideTimesAndTz
  */
 public function testTimezoneInput($defaultTz, $inputTimeDst, $inputTimeNonDst)
 {
     date_default_timezone_set($defaultTz);
     // formatting has to be independent of the default timezone set by PHP
     $this->formatter->datetimeFormat = 'yyyy-MM-dd HH:mm:ss';
     $this->formatter->dateFormat = 'yyyy-MM-dd';
     $this->formatter->timeFormat = 'HH:mm:ss';
     // daylight saving time
     $this->formatter->timeZone = 'UTC';
     $this->assertSame('2014-08-10 12:41:00', $this->formatter->asDatetime($inputTimeDst));
     $this->assertSame('2014-08-10', $this->formatter->asDate($inputTimeDst));
     $this->assertSame('12:41:00', $this->formatter->asTime($inputTimeDst));
     $this->assertSame('1407674460', $this->formatter->asTimestamp($inputTimeDst));
     $this->formatter->timeZone = 'Europe/Berlin';
     $this->assertSame('2014-08-10 14:41:00', $this->formatter->asDatetime($inputTimeDst));
     $this->assertSame('2014-08-10', $this->formatter->asDate($inputTimeDst));
     $this->assertSame('14:41:00', $this->formatter->asTime($inputTimeDst));
     $this->assertSame('1407674460', $this->formatter->asTimestamp($inputTimeDst));
     // non daylight saving time
     $this->formatter->timeZone = 'UTC';
     $this->assertSame('2014-01-01 12:41:00', $this->formatter->asDatetime($inputTimeNonDst));
     $this->assertSame('2014-01-01', $this->formatter->asDate($inputTimeNonDst));
     $this->assertSame('12:41:00', $this->formatter->asTime($inputTimeNonDst));
     $this->assertSame('1388580060', $this->formatter->asTimestamp($inputTimeNonDst));
     $this->formatter->timeZone = 'Europe/Berlin';
     $this->assertSame('2014-01-01 13:41:00', $this->formatter->asDatetime($inputTimeNonDst));
     $this->assertSame('2014-01-01', $this->formatter->asDate($inputTimeNonDst));
     $this->assertSame('13:41:00', $this->formatter->asTime($inputTimeNonDst));
     $this->assertSame('1388580060', $this->formatter->asTimestamp($inputTimeNonDst));
     // tests for relative time
     if ($inputTimeDst !== 1407674460) {
         $this->assertSame('3 hours ago', $this->formatter->asRelativeTime($inputTimeDst, $relativeTime = str_replace(['14:41', '12:41'], ['17:41', '15:41'], $inputTimeDst)));
         $this->assertSame('in 3 hours', $this->formatter->asRelativeTime($relativeTime, $inputTimeDst));
         $this->assertSame('3 hours ago', $this->formatter->asRelativeTime($inputTimeNonDst, $relativeTime = str_replace(['13:41', '12:41'], ['16:41', '15:41'], $inputTimeNonDst)));
         $this->assertSame('in 3 hours', $this->formatter->asRelativeTime($relativeTime, $inputTimeNonDst));
     }
 }
 public function testOneDigitIcu()
 {
     $formatter = new Formatter(['locale' => 'en-US']);
     $this->assertEquals('24.8.2014', $formatter->asDate('2014-8-24', 'php:d.n.Y'));
     $this->assertEquals('24.8.2014', $formatter->asDate('2014-8-24', 'd.M.yyyy'));
 }
 /**
  * Formats the value as a date.
  * @param integer|string|DateTime $value the value to be formatted. The following
  * types of value are supported:
  *
  * - an integer representing a UNIX timestamp
  * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php).
  *   The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.
  * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object
  *
  * @param string $format the format used to convert the value into a date string.
  * If null, [[dateFormat]] will be used.
  *
  * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
  * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime).
  *
  * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the
  * PHP [date()](http://php.net/manual/en/function.date.php)-function.
  *
  * @return string the formatted result.
  * @throws InvalidParamException if the input value can not be evaluated as a date value.
  * @throws InvalidConfigException if the date format is invalid.
  * @see dateFormat
  */
 public function asDate($value, $format = null)
 {
     if ($format == 'human') {
         switch ($this->locale) {
             case 'ru':
                 return $this->datetimeAsHumanRu($value, false);
                 break;
             case 'en':
                 return $this->datetimeAsHumanEn($value, false);
                 break;
         }
     }
     return parent::asDate($value, $format);
 }
Exemple #11
0
 public function asDateFiltered($value, $format = null)
 {
     return $value ? parent::asDate($value, $format) : null;
 }
 /**
  * Возвращает список месяцев
  *
  * @param bool $declension
  * @return array
  */
 public static function getMonthsList($declension = false)
 {
     $_months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
     $months = [];
     if (!$declension) {
         foreach ($_months as $key => $month) {
             $months[++$key] = Yii::t('basis', $month);
         }
         return $months;
     }
     $formatter = new Formatter();
     foreach ($_months as $key => $month) {
         $months[++$key] = $formatter->asDate($month, 'php:F');
     }
     return $months;
 }
Exemple #13
0
 /**
  * @inheritdoc
  * Adds support for [-]infinity.
  */
 public function asDate($value, $format = null)
 {
     if (($label = $this->isInfinity($value)) !== null) {
         return $label;
     }
     return parent::asDate($value, $format);
 }