5

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?

Albert Skibinski
  • 499
  • 4
  • 21
  • If you are displaying this date as a field in a view, then you could use a view's field template file to handle the PHP logic needed to get your desired output. On the flip side, if this isn't a date field in a view, and you are just using the created time for the node, you could use a node template file to format the date. I am not aware of any callback functions that can be used for date formats, that is an interesting idea though and I can definitely see some applications for it. – tyler.frankenstein Jun 30 '11 at 16:56
  • Yeah, I know I could accomplish this at a template/preprocess level, but it would be much cleaner if you could create a real global custom date format... – Albert Skibinski Jul 04 '11 at 08:35

4 Answers4

1

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.

Rudie
  • 52,220
  • 42
  • 131
  • 173
berkes
  • 26,996
  • 27
  • 115
  • 206
0

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

Rinku
  • 1
  • 1
  • I'm aware of the date API module but this is not what I'm looking for. Please review the question: I want to add a format which uses a callback with some php logic. NOT a simple format using php date() syntax. – Albert Skibinski Jun 30 '11 at 15:28
0

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.

Chintan
  • 136
  • 5
  • I'm aware of this, but this is still not what I'm asking for. I want to create a dynamic php custom format which can be used throughout drupal. Your solution is on template level and only in a specific tpl. – Albert Skibinski Jul 06 '11 at 07:15
  • Hey!! you should modify your question, its bit confusing. You are asking to change the date format for site wide nodes, look at the bold part its confusing. – Chintan Jul 06 '11 at 07:30
0

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

Joe Hyde
  • 999
  • 7
  • 17