mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Date::DayOfMonth() now supports ordinal argument, so you can get somehing like "10th" or "2nd". Also supported in Date::RangeString
This commit is contained in:
parent
8fdc531345
commit
58d48583a9
@ -95,8 +95,10 @@ class Date extends DBField {
|
||||
/**
|
||||
* Returns the date of the month
|
||||
*/
|
||||
function DayOfMonth() {
|
||||
if($this->value) return date('j', strtotime($this->value));
|
||||
function DayOfMonth($includeOrdinal = false) {
|
||||
$format = 'j';
|
||||
if ($includeOrdinal) $format .= 'S';
|
||||
if($this->value) return date($format, strtotime($this->value));
|
||||
}
|
||||
|
||||
|
||||
@ -140,9 +142,9 @@ class Date extends DBField {
|
||||
/*
|
||||
* Return a string in the form "12 - 16 Sept" or "12 Aug - 16 Sept"
|
||||
*/
|
||||
function RangeString($otherDateObj) {
|
||||
$d1 = $this->DayOfMonth();
|
||||
$d2 = $otherDateObj->DayOfMonth();
|
||||
function RangeString($otherDateObj, $includeOrdinals = false) {
|
||||
$d1 = $this->DayOfMonth($includeOrdinals);
|
||||
$d2 = $otherDateObj->DayOfMonth($includeOrdinals);
|
||||
$m1 = $this->ShortMonth();
|
||||
$m2 = $otherDateObj->ShortMonth();
|
||||
$y1 = $this->Year();
|
||||
|
@ -114,4 +114,14 @@ class DateTest extends SapphireTest {
|
||||
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
|
||||
}
|
||||
|
||||
function testDayOfMonth() {
|
||||
$date = DBField::create('Date', '2000-10-10');
|
||||
$this->assertEquals('10', $date->DayOfMonth());
|
||||
$this->assertEquals('10th', $date->DayOfMonth(true));
|
||||
|
||||
$range = $date->RangeString(DBField::create('Date', '2000-10-20'));
|
||||
$this->assertEquals('10 - 20 Oct 2000', $range);
|
||||
$range = $date->RangeString(DBField::create('Date', '2000-10-20'), true);
|
||||
$this->assertEquals('10th - 20th Oct 2000', $range);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user