Handling Date and Time in Drupal 7

Over the years of maintaining Drupal websites I noticed that a lot of developers – and themers – hard code date and time formats. I know this may sound like a rant, but I would like to demonstrate an easier and cleaner way of creating and managing date and time formats.

How often have you seen this format_date($node->changed, 'custom', 'd-m-Y'); inside template files? I'm not saying it's wrong to use the format_date()(API Documentation) function, but if you are defining the date and time format in ten different templates it'll be very difficult to manage if you want to change the formats – because remember clients love to change things just before a launch.

A simple solution to all this madness is to define a Date and Time format and type.

Let's create a date and time type called "Blog Created Short".

Go to Configuration -> "Date and time" (admin/config/regional/date-time).

Go to the "Formats" tab and click on "Add format", define the date/time format and then click on "Add format". For this example enter in "d-m-Y".

Now that our format has been created, go back to the types page and click on "Add date type". Enter in "Blog Created Short" as the "Date type" and select the format that you want to use.

See how simple it is to create a date and time type and best of all we’ll be able to reuse the same "Blog Created Short" type in many places. If you need to output a date via the format_date() function, just specify the type.

For example:

format_date($node->changed, 'blog_created_short');

You can also reuse the type within Views. If you add the "Content: Post date" – or any timestamp type field – field, you’ll be able to reuse any date and time types.

If you have any questions please leave a comment.

Further Resources

 

About The Author

14 thoughts on “Handling Date and Time in Drupal 7”

  1. Still trying to get a hang of this drupal beast. This post helped me take another step in the right direction.

    Thanks a million!
    Cheers!

  2. How do I timestamp a particular field with a date? Let’s say I have a node with a field called ‘status’ that has 2 items; ‘pending’ and ‘live’. When I go back to edit the node and select ‘live’ I want this to be recorded somehow. Tried rules but can’t write to an empty field I created called ‘live date’. Head scratcher!

    1. If I understand your question correctly you want to track the date on when the ‘status’ field is changed?

      I would look at using the Message module (http://drupal.org/project/message). Use Message to create a “log” of when the ‘status’ field was changed.

  3. No idea why I have only just revisited this, thanks very much for the response. I have just tried this out and having some success so thanks very much, was stuck on this for ages. Just going to try in Views now.

  4. Just another quick one if anyone could lend a hand. I have 2 content types; publisher and campaign. The campaign type uses a collection a field called ‘publishers’ which uses an entity reference to the publisher type. Within this field collection I have a field called ‘publisher status’. (This is so I can track each publisher who works on a campaign and see if they are active, paid or unpaid) I now need to timestamp when each status is changed. A little stuck on the complexity of the rule. I got Event: “After updating existing content” The next bit I am stuck on as not sure which content type is being updated, any help received gratefully, this is way over my head!

    1. Could you add a condition that checks the content type? Also, instead of trying to do everything with a single rule, sometimes it’s easier to separate logic into more than one rule.

      If you’re having trouble with Rules in general, I would highly recommend the “Learn the Rules framework” video series from nodeone:

      http://nodeone.se/sv/node/32

  5. Yes, I have watched many of these videos, they are very good. However, this particular issue I cannot even work out what content type I should be checking for! Am I checking for the publisher, or the campaign? I actually need to check for a reference entity field from the campaign to the publisher, the settings to do this are unclear. Thanks for your response.

    1. Within the rule use the “Content is of type” condition to check the content type. Now the tricky part is checking the type of a attached node via an entity reference.

      I have never done this before. However, after a quick google search I found this video:
      http://nodeone.se/sv/node/783

  6. Hi,

    Question might be a little off topic, but there’s something I see in one of your screenshot that I can’t seem to be able to get on my site… In the list of available formats, you seem to have one “Custom”. Is this a format you created, or more of a choice that allows you to specify a date format on the fly? If it’s to specify, could you tell me how you got this in there?

    Thanks in advance,
    Bruno

    1. The screenshot is within Views.

      Just add the “post date” field to a view and you’ll see the drop-down with “custom” as an option.

      1. Thanks for the answer. I was not seeing it simply because I’m trying to do this on a custom date field. It’s quite unfortunate this option is not available for any other date (what I’m trying to do is output the date in a Service view, used by a REST server, but the date keeps coming up with span tags around it that I can’t get rid of).

        Thanks anyways for the fast answer!
        Bruno

  7. How can i get a clean URL in my Exposed Filter and Contextual Filters. I want to create a archive were users can sort the nodes by its posted Date. I used contextual filter to achive this and its working fine when i pass the value in URL like.

    “www.example.com/archive/10/09/2013”.

    But when i use a exposed filter and submit the value the URL became like this

    “www.example.com/archive/archive?date_filter%5Bvalue%5D%5Byear%5D=2013&date_filter%5Bvalue%5D%5Bmonth%5D=9&date_filter%5Bvalue%5D%5Bday%5D=10” and it returns a empty page.

    How can i pass a clean value in Exposed Filter and make it work properly? Thanks in advance.

Comments are closed.

Scroll to Top