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:
Sean Harvey 2012-02-24 16:49:41 +13:00 committed by Sean Harvey
parent 8fdc531345
commit 58d48583a9
2 changed files with 17 additions and 5 deletions

View File

@ -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();

View File

@ -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);
}
}