Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BUGFIX Changed Date.php and DateTime.php to use DateTime->Format inst…

…ead of strtotime to handle extended dates on 32bit environment(fixes #7311)
  • Loading branch information...
commit c04b01fc2ac555bc49b7bf90a33210b9610ad291 1 parent 7dc2d61
@yukiawano yukiawano authored
View
32 model/fieldtypes/Date.php
@@ -46,7 +46,14 @@ function setValue($value, $record = null) {
if(is_numeric($value)) {
$this->value = date('Y-m-d', $value);
} elseif(is_string($value)) {
- $this->value = date('Y-m-d', strtotime($value));
+ try{
+ $date = new DateTime($value);
+ $this->value = $date->Format('Y-m-d');
+ return;
+ }catch(Exception $e){
+ $this->value = null;
+ return;
+ }
}
}
@@ -54,42 +61,42 @@ function setValue($value, $record = null) {
* Returns the date in the format dd/mm/yy
*/
function Nice() {
- if($this->value) return date('d/m/Y', strtotime($this->value));
+ if($this->value) return $this->Format('d/m/Y');
}
/**
* Returns the date in US format: “01/18/2006”
*/
function NiceUS() {
- if($this->value) return date('m/d/Y', strtotime($this->value));
+ if($this->value) return $this->Format('m/d/Y');
}
/**
* Returns the year from the given date
*/
function Year() {
- if($this->value) return date('Y', strtotime($this->value));
+ if($this->value) return $this->Format('Y');
}
/**
* Returns the Full day, of the given date.
*/
function Day(){
- if($this->value) return date('l', strtotime($this->value));
+ if($this->value) return $this->Format('l');
}
/**
* Returns a full textual representation of a month, such as January.
*/
function Month() {
- if($this->value) return date('F', strtotime($this->value));
+ if($this->value) return $this->Format('F');
}
/**
* Returns the short version of the month such as Jan
*/
function ShortMonth() {
- if($this->value) return date('M', strtotime($this->value));
+ if($this->value) return $this->Format('M');
}
/**
@@ -101,7 +108,7 @@ function DayOfMonth($includeOrdinal = false) {
if($this->value) {
$format = 'j';
if ($includeOrdinal) $format .= 'S';
- return date($format, strtotime($this->value));
+ return $this->Format($format);
}
}
@@ -109,14 +116,14 @@ function DayOfMonth($includeOrdinal = false) {
* Returns the date in the format 24 December 2006
*/
function Long() {
- if($this->value) return date('j F Y', strtotime($this->value));
+ if($this->value) return $this->Format('j F Y');
}
/**
* Returns the date in the format 24 Dec 2006
*/
function Full() {
- if($this->value) return date('j M Y', strtotime($this->value));
+ if($this->value) return $this->Format('j M Y');
}
/**
@@ -126,7 +133,10 @@ function Full() {
* @return string The date in the requested format
*/
function Format($format) {
- if($this->value) return date($format, strtotime($this->value));
+ if($this->value){
+ $date = new DateTime($this->value);
+ return $date->Format($format);
+ }
}
/**
View
22 model/fieldtypes/Datetime.php
@@ -40,7 +40,15 @@ function setValue($value, $record = null) {
if(is_numeric($value)) {
$this->value = date('Y-m-d H:i:s', $value);
} elseif(is_string($value)) {
- $this->value = date('Y-m-d H:i:s', strtotime($value));
+ // $this->value = date('Y-m-d H:i:s', strtotime($value));
+ try{
+ $date = new DateTime($value);
+ $this->value = $date->Format('Y-m-d H:i:s');
+ return;
+ }catch(Exception $e){
+ $this->value = null;
+ return;
+ }
}
}
@@ -48,23 +56,23 @@ function setValue($value, $record = null) {
* Returns the date in the raw SQL-format, e.g. “2006-01-18 16:32:04”
*/
function Nice() {
- if($this->value) return date('d/m/Y g:ia', strtotime($this->value));
+ if($this->value) return $this->Format('d/m/Y g:ia');
}
function Nice24() {
- if($this->value) return date('d/m/Y H:i', strtotime($this->value));
+ if($this->value) return $this->Format('d/m/Y H:i');
}
function Date() {
- if($this->value) return date('d/m/Y', strtotime($this->value));
+ if($this->value) return $this->Format('d/m/Y');
}
function Time() {
- if($this->value) return date('g:ia', strtotime($this->value));
+ if($this->value) return $this->Format('g:ia');
}
function Time24() {
- if($this->value) return date('H:i', strtotime($this->value));
+ if($this->value) return $this->Format('H:i');
}
function requireField() {
@@ -74,7 +82,7 @@ function requireField() {
}
function URLDatetime() {
- if($this->value) return date('Y-m-d%20H:i:s', strtotime($this->value));
+ if($this->value) return $this->Format('Y-m-d%20H:i:s');
}
public function scaffoldFormField($title = null, $params = null) {
View
2  tests/forms/GridFieldTest.php
@@ -265,7 +265,7 @@ public function testGetCastedValue() {
public function testGetCastedValueObject() {
$obj = new GridField('testfield', 'testfield');
$value = $obj->getCastedValue('This is a sentance. This ia another.', 'Date');
- $this->assertEquals('1970-01-01', $value);
+ $this->assertEquals(null, $value);
}
/**
View
36 tests/model/DateTest.php
@@ -61,6 +61,36 @@ function testNiceDate() {
);
}
+ function testNiceUS(){
+ $this->assertEquals('03/31/2008', DBField::create_field('Date', 1206968400)->NiceUs(),
+ "Date->NiceUs() works with timestamp integers"
+ );
+ }
+
+ function testYear(){
+ $this->assertEquals('2008', DBField::create_field('Date', 1206968400)->Year(),
+ "Date->Year() works with timestamp integers"
+ );
+ }
+
+ function testDay(){
+ $this->assertEquals('Monday', DBField::create_field('Date', 1206968400)->Day(),
+ "Date->Day() works with timestamp integers"
+ );
+ }
+
+ function testMonth(){
+ $this->assertEquals('March', DBField::create_field('Date', 1206968400)->Month(),
+ "Date->Month() works with timestamp integers"
+ );
+ }
+
+ function testShortMonth(){
+ $this->assertEquals('Mar', DBField::create_field('Date', 1206968400)->ShortMonth(),
+ "Date->ShortMonth() works with timestamp integers"
+ );
+ }
+
function testLongDate() {
$this->assertEquals('31 March 2008', DBField::create_field('Date', 1206968400)->Long(),
"Date->Long() works with numeric timestamp"
@@ -81,6 +111,12 @@ function testLongDate() {
"Date->Long() works with D/M/YYYY"
);
}
+
+ function testFull(){
+ $this->assertEquals('31 Mar 2008', DBField::create_field('Date', 1206968400)->Full(),
+ "Date->Full() works with timestamp integers"
+ );
+ }
function testSetNullAndZeroValues() {
$date = DBField::create_field('Date', '');
View
38 tests/model/DatetimeTest.php
@@ -50,5 +50,43 @@ function testSetNullAndZeroValues() {
$date = DBField::create_field('SS_Datetime', 0);
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
}
+
+ function testExtendedDateTimes() {
+ $date = DBField::create_field('SS_Datetime', '1500-10-10 15:32:24');
+ $this->assertEquals('10 Oct 1500 15 32 24', $date->Format('d M Y H i s'));
+
+ $date = DBField::create_field('SS_Datetime', '3000-10-10 15:32:24');
+ $this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d M Y H i s'));
+ }
+
+ function testNice() {
+ $date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
+ $this->assertEquals('31/12/2001 10:10pm', $date->Nice());
+ }
+
+ function testNice24() {
+ $date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
+ $this->assertEquals('31/12/2001 22:10', $date->Nice24());
+ }
+
+ function testDate() {
+ $date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
+ $this->assertEquals('31/12/2001', $date->Date());
+ }
+
+ function testTime() {
+ $date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
+ $this->assertEquals('10:10pm', $date->Time());
+ }
+
+ function testTime24() {
+ $date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
+ $this->assertEquals('22:10', $date->Time24());
+ }
+
+ function testURLDateTime(){
+ $date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
+ $this->assertEquals('2001-12-31%2022:10:59', $date->URLDateTime());
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.