From a8ac288d1c2829eaa8ea447eb4d314c62b57c247 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 5 Jan 2011 19:06:52 -0800 Subject: [PATCH] Added $format option to TimeHelper::nice(). Fixes #433, #638, #291, #801 --- cake/libs/view/helpers/time.php | 36 +++++++++++++++++-- .../cases/libs/view/helpers/time.test.php | 7 ++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/cake/libs/view/helpers/time.php b/cake/libs/view/helpers/time.php index 32e9e3050..163212955 100644 --- a/cake/libs/view/helpers/time.php +++ b/cake/libs/view/helpers/time.php @@ -27,6 +27,31 @@ */ class TimeHelper extends AppHelper { +/** + * The format to use when formatting a time using `TimeHelper::nice()` + * + * The format should use the locale strings as defined in the PHP docs under + * `strftime` (http://php.net/manual/en/function.strftime.php) + * + * @var string + * @see TimeHelper::format() + */ + public $niceFormat = '%a, %b %eS %Y, %H:%M'; + +/** + * Constructor + * + * @param View $View the view object the helper is attached to. + * @param array $settings Settings array Settings array + * @return void + */ + public function __construct(View $View, $settings = array()) { + if (isset($settings['niceFormat'])) { + $this->niceFormat = $settings['niceFormat']; + } + parent::__construct($View, $settings); + } + /** * Converts a string representing the format for the function strftime and returns a * windows safe and i18n aware format. @@ -186,19 +211,26 @@ class TimeHelper extends AppHelper { /** * Returns a nicely formatted date string for given Datetime string. * + * See http://php.net/manual/en/function.strftime.php for information on formatting + * using locale strings. + * * @param string $dateString Datetime string or Unix timestamp * @param int $userOffset User's offset from GMT (in hours) + * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used * @return string Formatted date string * @access public * @link http://book.cakephp.org/view/1471/Formatting */ - public function nice($dateString = null, $userOffset = null) { + public function nice($dateString = null, $userOffset = null, $format = null) { if ($dateString != null) { $date = $this->fromString($dateString, $userOffset); } else { $date = time(); } - $format = $this->convertSpecifiers('%a, %b %eS %Y, %H:%M', $date); + if (!$format) { + $format = $this->niceFormat; + } + $format = $this->convertSpecifiers($format, $date); return strftime($format, $date); } diff --git a/cake/tests/cases/libs/view/helpers/time.test.php b/cake/tests/cases/libs/view/helpers/time.test.php index d71c6cd90..60f61fed2 100644 --- a/cake/tests/cases/libs/view/helpers/time.test.php +++ b/cake/tests/cases/libs/view/helpers/time.test.php @@ -324,6 +324,13 @@ class TimeHelperTest extends CakeTestCase { $time = null; $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); + + $time = time(); + $this->assertEqual(date('D', $time), $this->Time->nice($time, null, '%a')); + $this->assertEqual(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y')); + + $this->Time->niceFormat = '%Y-%d-%m'; + $this->assertEqual(date('Y-d-m', $time), $this->Time->nice($time)); } /**