ENHANCEMENT Allow setting date timestamp for Date::setValue()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@71786 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2009-02-12 04:57:17 +00:00
parent 43c96813bf
commit 1d121b7f90
2 changed files with 28 additions and 16 deletions

View File

@ -7,17 +7,21 @@
class Date extends DBField {
function setValue($value) {
if( is_array( $value ) && $value['Day'] && $value['Month'] && $value['Year'] ) {
if(is_array($value) && $value['Day'] && $value['Month'] && $value['Year']) {
$this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day'];
return;
}
// Default to NZ date format - strtotime expects a US date
if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts))
$value = "$parts[2]/$parts[1]/$parts[3]";
if($value && is_string($value)) $this->value = date('Y-m-d', strtotime($value));
else $value = null;
if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
$value = "$parts[2]/$parts[1]/$parts[3]";
}
if(is_numeric($value)) {
$this->value = date('Y-m-d', $value);
} elseif(is_string($value)) {
$this->value = date('Y-m-d', strtotime($value));
}
}
/**

View File

@ -8,15 +8,19 @@ class DateTest extends SapphireTest {
function testNiceDate() {
/* Test the DD/MM/YYYY formatting of Date::Nice() */
$cases = array(
'4/3/03' => '04/03/2003',
'04/03/03' => '04/03/2003',
'4/3/03' => '04/03/2003',
'4/03/03' => '04/03/2003',
'4/3/2003' => '04/03/2003',
'4-3-2003' => '04/03/2003',
'2003-03-04' => '04/03/2003',
'04/03/2003' => '04/03/2003',
'04-03-2003' => '04/03/2003'
1206961200 => '01/04/2008', // timestamp integers work as well!
1206874800 => '31/03/2008',
'1206961200' => '01/04/2008', // a timestamp as a string, not an integer
'1206874800' => '31/03/2008',
'4/3/03' => '04/03/2003', // D/M/YY format
'04/03/03' => '04/03/2003', // DD/MM/YY format
'4/3/03' => '04/03/2003', // D/M/YY format
'4/03/03' => '04/03/2003', // D/MM/YY format
'4/3/2003' => '04/03/2003', // D/M/YYYY format
'4-3-2003' => '04/03/2003', // D-M-YYYY format
'2003-03-04' => '04/03/2003', // YYYY-MM-DD format
'04/03/2003' => '04/03/2003', // DD/MM/YYYY format
'04-03-2003' => '04/03/2003' // DD-MM-YYYY format
);
foreach($cases as $original => $expected) {
@ -29,6 +33,10 @@ class DateTest extends SapphireTest {
function testLongDate() {
/* "24 May 2006" style formatting of Date::Long() */
$cases = array(
1206961200 => '1 April 2008',
'1206961200' => '1 April 2008',
1206874800 => '31 March 2008',
'1206874800' => '31 March 2008',
'2003-4-3' => '3 April 2003',
'3/4/2003' => '3 April 2003',
);