Permalink
Browse files

ENHANCEMENT Date::DayOfMonth() now supports ordinal argument, so you …

…can get somehing like "10th" or "2nd". Also supported in Date::RangeString
  • Loading branch information...
halkyon committed Feb 24, 2012
1 parent 8fdc531 commit 58d48583a9953d0aa3eee8757927d5a884cdfff9
Showing with 17 additions and 5 deletions.
  1. +7 −5 model/fieldtypes/Date.php
  2. +10 −0 tests/model/DateTest.php
View
@@ -95,8 +95,10 @@ function ShortMonth() {
/**
* 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 @@ function FormatI18N($formattingString) {
/*
* 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
@@ -114,4 +114,14 @@ function testSetNullAndZeroValues() {
$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);
+ }
}

0 comments on commit 58d4858

Please sign in to comment.