diff --git a/src/ORM/FieldType/DBDate.php b/src/ORM/FieldType/DBDate.php index 71cca7d10..8e63e72be 100644 --- a/src/ORM/FieldType/DBDate.php +++ b/src/ORM/FieldType/DBDate.php @@ -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()); diff --git a/tests/php/ORM/DBDateTest.php b/tests/php/ORM/DBDateTest.php index 6e00803dc..001cc1ffb 100644 --- a/tests/php/ORM/DBDateTest.php +++ b/tests/php/ORM/DBDateTest.php @@ -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');