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:
Damian Mooyman 2018-01-26 09:27:46 +13:00 committed by GitHub
commit 76d2db12b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -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
* @return string The date in the requested format
@ -227,6 +228,12 @@ class DBDate extends DBField
if (!$this->value) {
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->setPattern($format);
return $formatter->format($this->getTimestamp());

View File

@ -238,6 +238,21 @@ class DBDateTest extends SapphireTest
$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()
{
$date = DBField::create_field('Date', '1800-10-10');