mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #7794 from kinglozzer/id-like-to-place-an-ordinal
NEW: Add support for ordinals in DBDate::Format()
This commit is contained in:
commit
76d2db12b0
@ -217,7 +217,8 @@ class DBDate extends DBField
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the date using a particular formatting string.
|
* Return the date using a particular formatting string. Use {o} to include an ordinal representation
|
||||||
|
* for the day of the month ("1st", "2nd", "3rd" etc)
|
||||||
*
|
*
|
||||||
* @param string $format Format code string. See http://userguide.icu-project.org/formatparse/datetime
|
* @param string $format Format code string. See http://userguide.icu-project.org/formatparse/datetime
|
||||||
* @return string The date in the requested format
|
* @return string The date in the requested format
|
||||||
@ -227,6 +228,12 @@ class DBDate extends DBField
|
|||||||
if (!$this->value) {
|
if (!$this->value) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Replace {o} with ordinal representation of day of the month
|
||||||
|
if (strpos($format, '{o}') !== false) {
|
||||||
|
$format = str_replace('{o}', "'{$this->DayOfMonth(true)}'", $format);
|
||||||
|
}
|
||||||
|
|
||||||
$formatter = $this->getFormatter();
|
$formatter = $this->getFormatter();
|
||||||
$formatter->setPattern($format);
|
$formatter->setPattern($format);
|
||||||
return $formatter->format($this->getTimestamp());
|
return $formatter->format($this->getTimestamp());
|
||||||
|
@ -238,6 +238,21 @@ class DBDateTest extends SapphireTest
|
|||||||
$this->assertEquals('10th - 20th Oct 2000', $range);
|
$this->assertEquals('10th - 20th Oct 2000', $range);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFormatReplacesOrdinals()
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'20th October 2000',
|
||||||
|
DBField::create_field('Date', '2000-10-20')->Format('{o} MMMM YYYY'),
|
||||||
|
'Ordinal day of month was not injected'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'20th is the 20th day of the month',
|
||||||
|
DBField::create_field('Date', '2000-10-20')->Format("{o} 'is the' {o} 'day of the month'"),
|
||||||
|
'Failed to inject multiple ordinal values into one string'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testExtendedDates()
|
public function testExtendedDates()
|
||||||
{
|
{
|
||||||
$date = DBField::create_field('Date', '1800-10-10');
|
$date = DBField::create_field('Date', '1800-10-10');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user