Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6128693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:39:20+00:00 2026-05-23T16:39:20+00:00

I want to create a dynamic php date format. Example: show the time of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:39:21+00:00Added an answer on May 23, 2026 at 4:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to create dynamic banner rotater wih php ajax i want to pass
I want to create dynamic content based on this. I know it's somewhere, as
I want to create dynamic tabPages in TabControl. In each tabPage I create dataGridView
I want to create a Dynamic aspx Page in current solution through code-behind..forexample i
this table i want to create job_id dynamic Jobs Title text Job Description text
I want to create PayPal button with dynamic price (depending from the item) and
I want to be able to create links looking like buttons with dynamic text
i want create image animation , i have 50 images with png format now
I want to create dynamic XML hieararchy but still couldn't be successful about it.I
I want to create a dynamic object in Python and then add attributes to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.