mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
This commit is contained in:
parent
4d2fdcd15e
commit
a8ac288d1c
2 changed files with 41 additions and 2 deletions
|
@ -27,6 +27,31 @@
|
||||||
*/
|
*/
|
||||||
class TimeHelper extends AppHelper {
|
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
|
* Converts a string representing the format for the function strftime and returns a
|
||||||
* windows safe and i18n aware format.
|
* windows safe and i18n aware format.
|
||||||
|
@ -186,19 +211,26 @@ class TimeHelper extends AppHelper {
|
||||||
/**
|
/**
|
||||||
* Returns a nicely formatted date string for given Datetime string.
|
* 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 string $dateString Datetime string or Unix timestamp
|
||||||
* @param int $userOffset User's offset from GMT (in hours)
|
* @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
|
* @return string Formatted date string
|
||||||
* @access public
|
* @access public
|
||||||
* @link http://book.cakephp.org/view/1471/Formatting
|
* @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) {
|
if ($dateString != null) {
|
||||||
$date = $this->fromString($dateString, $userOffset);
|
$date = $this->fromString($dateString, $userOffset);
|
||||||
} else {
|
} else {
|
||||||
$date = time();
|
$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);
|
return strftime($format, $date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -324,6 +324,13 @@ class TimeHelperTest extends CakeTestCase {
|
||||||
|
|
||||||
$time = null;
|
$time = null;
|
||||||
$this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue