From 58d48583a9953d0aa3eee8757927d5a884cdfff9 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Fri, 24 Feb 2012 16:49:41 +1300 Subject: [PATCH] ENHANCEMENT Date::DayOfMonth() now supports ordinal argument, so you can get somehing like "10th" or "2nd". Also supported in Date::RangeString --- model/fieldtypes/Date.php | 12 +++++++----- tests/model/DateTest.php | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/model/fieldtypes/Date.php b/model/fieldtypes/Date.php index 3defd4579..f8b9ec279 100644 --- a/model/fieldtypes/Date.php +++ b/model/fieldtypes/Date.php @@ -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(); diff --git a/tests/model/DateTest.php b/tests/model/DateTest.php index f14f09dfc..f09da2c86 100644 --- a/tests/model/DateTest.php +++ b/tests/model/DateTest.php @@ -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); + } }