OOP noob here. I'd like to take advantage of CakePHP 2.3's short date methods in order to get "today" or "yesterday" when appropriate while changing the format of the date when the output is not "today" or "yesterday".
Example view: echo $this->Time->niceShort($user['User']['last_invited_on'];
.
I've found the following in lib/Cake/Utility/CakeTime.php
(Cake's TimeHelper
uses the CakeTime
utility.):
class CakeTime {
/**
* The format to use when formatting a time using `CakeTime::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 CakeTime::format()
*/
public static $niceFormat = '%a, %b %eS %Y, %H:%M';
/**
* The format to use when formatting a time using `CakeTime::timeAgoInWords()`
* and the difference is more than `CakeTime::$wordEnd`
*
* @var string
* @see CakeTime::timeAgoInWords()
*/
public static $wordFormat = 'j/n/y';
/**
* The format to use when formatting a time using `CakeTime::niceShort()`
* and the difference is between 3 and 7 days
*
* @var string
* @see CakeTime::niceShort()
*/
public static $niceShortFormat = '%B %d, %H:%M';
Can I somehow override this class's public properties in order to change the outputted date format whenever Time->niceShort
is used in a view? (Is this "monkey patching"?) If so, what would be a good/clean way?
Or should I write a new class that extends CakeTime
, and would this mean having to change $this->Time
to $this->MyNewSpiffingCustomisedTime
in views (which I'd rather not do as other people who are used to using Cake's Time
are working on the project)? I wondered if this would be overkill just to change a property.
Old: No need, you could just compare:
if (date('Y-m-d') != $date && date('Y-m-d', strtotime('-1 day')) != $date) {
//something
echo $this->Time->niceShort($date);
//something
}
Y-m-d
is whatever your date format is i.e. Y-m-d H:i:s
New: The proper way would be to call CakePHP wrapper for date('format', strtotime($date));
. The CakePHP wrapper is defined as $this->Time->format('format', $date);
and calls the previous php function I listed.
You shouldn't personalize base code if you plan to upgrade at any point. The only way (that I can think of right now) to still call $this->Time and extend the base code would be to implement a plugin called Time
and include in your $helper
and/or $component
member variables Time.mySpiffyTimeThingo
where mySpiffyTimeThingo is the name of the component or helper. These could extend/overwrite the current CakePHP CakeTime functions.
Why not extend the helper?
Create a class in app/View/Helper/myTimeHelper.php
. In there add the reference to the old class you'll be extended like:
App::uses('TimeHelper', 'View/Helper');
class myTimeHelper extends TimeHelper {
public function niceShort(<whatever input the original is getting>) {
// your new logic for the things you want to change
// and/or a return TimeHelper::niceShort(<whatever>) for the cases that are
//fine as they are. If you don't need that then you can remove App::uses.
}
Finally you can have your helper imported as myTime
with
public $helpers = array('myTime');
or even change the default Time
to call myTime
so you don't need to change anything else in your already written code:
public $helpers = array('Time' => array('className' => 'myTime'));