自定义日期格式(用php逻辑回调)

发布于 2024-11-18 00:42:40 字数 260 浏览 2 评论 0原文

我想创建一个动态的 php 日期格式。示例:如果节点是今天发布的,则显示节点的时间,如果早于今天,则仅显示日期/月份。我希望这种格式在整个 Drupal 中可用,就像 Drupal 中的其他预定义日期格式一样(不仅仅是在主题级别)

我找到了 hook_date_format_types 的(仅限 D7)钩子,但即使是那个钩子似乎也不允许我可以定义此 PHP 逻辑的回调。

有谁知道哪个钩子可以使这成为可能?或者有一个模块可以做到这一点?

I want to create a dynamic php date format. Example: show the time of the node if it was published today and only the day/month when older then today. I want this format to be available throughout Drupal like the other predefined date formats in Drupal (not just on theme level).

I found the (D7 only) hook for hook_date_format_types but even that one doesn't seem to allow for a callback where I could define this PHP logic.

Does anyone know which hook would make this possible? Or a module which does this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷不放纵 2024-11-25 00:42:40

在 Drupal6 中,format_date() 对日期和时间进行硬编码。另外,format_date() 不允许回调,但允许自定义字符串。这就是您可以应用技巧的地方:您可以调用返回字符串的函数,而不是对其中的字符串进行硬编码。

function mydate_format($timestamp) {
  $now = time();
  if (($now - $timestamp) < (60*60*24)) {
    return "H:i";
  }
  else {
    return "d:m:Y";
  }
}

print format_date($timestamp, 'custom', mydate_format($timestamp));

第二种选择是重新定义日期时间戳,但这既简单又有限。日期格式是用variable_get()定义的,但是不传递时间戳;因此,您基于时间戳值切换格式的示例是不可能通过这种方式实现的。

settings.php 中:

$conf['date_format_long'] = $conf['debug'] ? 'r' : 'l, F j, Y - H:i';

这将从一个值切换到另一个值,具体取决于您的 settings.php 是否将“debug”标志设置为 TRUE。如前所述:其用途是有限的,因为您无法获得任何上下文。

第三种选择是使用 Date API,它提供在线可配置的时间格式。但这既笨拙又不安全(在数据库中输入可执行的 PHP)。它还依赖于一个非常大的模块集。与第一个解决方案具有相同的缺点:您不能使用 format_date,但必须使用修改后的函数调用,而不是 format_date()。请参阅Drupal.org 日期主题手册中的所有选项。

GOTCHA 在所有情况下,Drupal 都不会为缓存内容调用此函数。如果你想让日期真正动态化,你要么需要完全避免缓存,要么在客户端 JavaScript 中实现日期格式。

TL;DR:如果不更改主题级别的某些代码,就无法拥有动态日期格式。使用回调函数生成“自定义”日期格式是最简单、有效的解决方案。

In Drupal6, format_date() has the dates and times hardcoded. Also, format_date() does not allow callbacks, but it does allow a custom string. That is where you can apply a trick: instead of hardcoding the string in there, you call a function that returns a string.

function mydate_format($timestamp) {
  $now = time();
  if (($now - $timestamp) < (60*60*24)) {
    return "H:i";
  }
  else {
    return "d:m:Y";
  }
}

print format_date($timestamp, 'custom', mydate_format($timestamp));

The second option is to re-define a date-timestamp, but that is both hackish and limited. Date-formats are defined with variable_get(), but don't pass the timestamp along; so your example of switching formats based on the value of timestamp is not possible this way.

In settings.php:

$conf['date_format_long'] = $conf['debug'] ? 'r' : 'l, F j, Y - H:i';

This will switch from one value to another, based on whether your settings.php has a flag "debug" set to TRUE or not. As mentioned: the use for this is limited, since you cannot get any context.

The third alternative is to use Date API which offers onlinle configurable time-formats. But that is both clumsy and insecure (inputting executable PHP in your database). It also depends on a very large module-set. And has the same downside as the first solution: you cannot use format_date, but must use a modified function call, instead of format_date(). See all the options at The Drupal.org date themeing handbook.

GOTCHA In all cases Drupal will not call this function for cached content. If you want to have the dates really dynamic you either need to avoid caching alltogether, or implement the date-formatting in clientside javascript.

TL;DR: You cannot have dynamic date-formats without changing some of the code on theme-level. Using a callback-function to generate the "custom" dateformat is the simplest, working solution.

阳光的暖冬 2024-11-25 00:42:40

您可以使用 Date API 模块添加自定义日期格式。 Date API 模块位于 Date 模块内部。启用日期 API 模块后,您可以转到路径“admin/settings/date-time/formats/add”来添加自定义格式。

“admin/settings/date-time/formats/configure”是配置日期格式的路径。

看看这些。快乐编码。

谢谢
RT

You can use Date API module to add your custom date formatting. Date API module is inside the Date module. After enabling the Date API module you can go the path "admin/settings/date-time/formats/add" to add your custom format.

"admin/settings/date-time/formats/configure" is the path to configure date formats.

Have a look at these. Happy coding.

Thanks
RT

雨落□心尘 2024-11-25 00:42:40

您可以转到node.tpl.php(可能在sites/all/themes/theme_name/node.tpl.php中)。在这里您可以找到 $created 变量,要重新格式化日期,您可以使用自定义函数并根据需要更改 $created。此后,所有节点都将使用您格式化的日期。

问候,
金坦。

You can go to node.tpl.php(possibly in sites/all/themes/theme_name/node.tpl.php). Here yo can find $created variable, to reformat date you can use your custom function and change the $created as you want.After this all nodes will use your formatted dates.

Regatds,
Chintan.

慕烟庭风 2024-11-25 00:42:40

使用功能模块。创建一个特征。

在生成的功能模块中,在 [feature].module 文件中,创建一个 hook_nodeapi 函数并使用条件语句格式化该字段,该条件语句考虑当前 date() 以了解日期的显示方式并将其输入 $node -> 内容变量。

这就是摇滚明星会做的方式(-;

Use the features module. Create a feature.

In the resulting feature module, on the [feature].module file, create a hook_nodeapi function and format the field with a conditional statement that takes into account the current date() for how the date will be displayed and feed it into the $node->content var.

And that is the way rockstars would do it (-;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文