From 515f7b1587f7739f6a6f9b674a3e025f0964f77a Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Mon, 20 Apr 2009 22:32:13 +0000 Subject: [PATCH] ENHANCEMENT Allow calling Format() on a Time DB field type to specify the formatting type MINOR Code formatting fixes for Date and Time classes MINOR Added phpDoc comments to methods git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@74786 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/Date.php | 13 +++++++----- core/model/fieldtypes/Time.php | 36 ++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/core/model/fieldtypes/Date.php b/core/model/fieldtypes/Date.php index ffe96549a..b5a1e4432 100644 --- a/core/model/fieldtypes/Date.php +++ b/core/model/fieldtypes/Date.php @@ -79,10 +79,13 @@ class Date extends DBField { } /** - * Return the date formatted using the given PHP formatting string + * Return the date using a particular formatting string. + * + * @param string $format Format code string. e.g. "d M Y" + * @return string The date in the requested format */ - function Format($formattingString) { - if($this->value) return date($formattingString, strtotime($this->value)); + function Format($format) { + if($this->value) return date($format, strtotime($this->value)); } /** @@ -304,6 +307,6 @@ class Date extends DBField { public function scaffoldFormField($title = null, $params = null) { return new DateField($this->name, $title); } + } - -?> +?> \ No newline at end of file diff --git a/core/model/fieldtypes/Time.php b/core/model/fieldtypes/Time.php index 373bb38a1..92d4baf01 100755 --- a/core/model/fieldtypes/Time.php +++ b/core/model/fieldtypes/Time.php @@ -13,17 +13,40 @@ class Time extends DBField { if($value) { if(preg_match( '/(\d{1,2})[:.](\d{2})([ap]m)/', $value, $match )) $this->TwelveHour( $match ); else $this->value = date('H:i:s', strtotime($value)); - } else $value = null; + } else { + $value = null; + } } + /** + * Return a user friendly format for time + * in a 12 hour format. + * + * @return string Time in 12 hour format + */ function Nice() { return date('g:ia', strtotime($this->value)); } - + + /** + * Return a user friendly format for time + * in a 24 hour format. + * + * @return string Time in 24 hour format + */ function Nice24() { return date('H:i', strtotime($this->value)); } - + + /** + * Return the time using a particular formatting string. + * + * @param string $format Format code string. e.g. "g:ia" + * @return string The date in the requested format + */ + function Format($format) { + if($this->value) return date($format, strtotime($this->value)); + } function TwelveHour( $parts ) { $hour = $parts[1]; @@ -40,7 +63,8 @@ class Time extends DBField { } public function scaffoldFormField($title = null, $params = null) { - return new TimeField($this->name, $title); - } + return new TimeField($this->name, $title); + } + } -?> +?> \ No newline at end of file