【问题标题】:Changing the format of CakePHP's CakeTime methods更改 CakePHP Cake Time 方法的格式
【发布时间】:2013-09-18 12:28:01
【问题描述】:

OOP 菜鸟在这里。我想利用 CakePHP 2.3 的短日期方法,以便在适当的时候获取“今天”或“昨天”,同时在输出不是“今天”或“昨天”时更改日期格式。

示例视图:echo $this->Time->niceShort($user['User']['last_invited_on'];.

我在lib/Cake/Utility/CakeTime.php 中找到了以下内容(Cake 的TimeHelper 使用CakeTime 实用程序。):

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';

我能否以某种方式覆盖此类的公共属性,以便在视图中使用 Time->niceShort 时更改输出的日期格式? (这是“猴子补丁”吗?)如果是这样,什么是好的/干净的方法?

或者我应该编写一个扩展CakeTime 的新类,这是否意味着必须将视图中的$this->Time 更改为$this->MyNewSpiffingCustomisedTime(我不想像其他习惯使用Cake 的@ 的人那样做987654330@ 正在从事该项目)?我想知道这是否只是为了更改属性而过大。

【问题讨论】:

  • 不适合更改库代码。创建您的课程并基本上扩展
  • 使用 cakephp 内置助手创建新助手并扩展新助手..

标签: php cakephp datetime overriding


【解决方案1】:

旧: 不需要,你可以比较一下:

if (date('Y-m-d') != $date && date('Y-m-d', strtotime('-1 day')) != $date) {
    //something
    echo $this->Time->niceShort($date);
    //something
}

Y-m-d 是您的日期格式,即Y-m-d H:i:s

新功能: 正确的方法是为 date('format', strtotime($date)); 调用 CakePHP 包装器。 CakePHP 包装器定义为$this->Time->format('format', $date);,并调用我列出的前面的 php 函数。

如果您计划随时升级,则不应个性化基本代码。仍然调用 $this->Time 并扩展基本代码的唯一方法(我现在能想到的)是实现一个名为 Time 的插件并包含在您的 $helper 和/或 $component 成员变量中Time.mySpiffyTimeThingo 其中 mySpiffyTimeThingo 是组件或助手的名称。这些可以扩展/覆盖当前的 CakePHP CakeTime 函数。

【讨论】:

  • 这似乎更多地与确定日期是今天/昨天有关,而不是更改输出日期的格式?
  • 您是说您希望它显示得更像今天,时间和昨天,每个实例的时间?例如1周前,时间;去年,时间?
  • 否;请参阅原始问题的第一段。我还改写了最后几段以使其更清楚。谢谢。
  • 我认为我编辑了我的答案以更接近您正在寻找的内容。你的问题仍然(至少对我自己)不完全清楚,所以我在整个主题上都在暗中尝试。
  • 注意:从当前版本的 CakePHP (2.6.x) 开始,TimeHelper::format() 参数的正确顺序与上面显示的相反。应该是`$timestamp, 'format_string`。 book.cakephp.org/2.0/en/core-libraries/helpers/…
【解决方案2】:

为什么不扩展助手?

app/View/Helper/myTimeHelper.php 中创建一个类。在那里添加对旧类的引用,您将像这样扩展:

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.
}

最后你可以将你的助手导入为myTime

public $helpers = array('myTime');

或者甚至将默认的Time 更改为调用myTime,这样您就无需在已经编写的代码中更改任何其他内容:

public $helpers = array('Time' => array('className' => 'myTime'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多