I have an Integer field in a content type, which contains a unix timestamp. *(It is not a date module field; just a simple Integer field). In a View, I am rendering this field, but obviously it is just going to spit out the long unix timestamp; and I want it to read Feb 20th, 2013. How can I have Views format this field so that it spits out the readable date? I imagine I would create the views-view-field--field-MYTIMESTAMP.tpl.php, and copy in the default code, then use some php logic to convert it, right? But I am not sure how to do that part.

  • why cannot you use date module? Jan 24 '13 at 7:07
  • you could also try views php module. Jan 24 '13 at 7:14
  • It comes through a feed from another site. At this point in time, It can't be changed.
    – joe
    Jan 24 '13 at 7:16
  • Why use another module when you have views field tpl files available?
    – joe
    Jan 24 '13 at 7:17
  • If you are using feeds module, there is a module - drupal.org/project/feeds_tamper - which allows you to modify the feeds data before saving to db.
    – SGhosh
    Jan 24 '13 at 7:17

First you should define a suitable format- Do this in the administrative GUI:

  1. Navigate to: Administration » Configuration » Regional and language » Date and Time
  2. Click the tab "Formats" and click on link "Add format".
  3. Create a new format. You use the same string as you would pass to the PHP date function, i.e.: M jS, Y. Save it.
  4. Click the tab "Types". Click on link "Add date type".
  5. Select the newly created format from the menu, and name it: dateonly, and save it by clicking on "Add date type".

You now have a type of date format named dateonly defined you can use anywhere on your site there is a date field to be displayed without the hour and minutes part.

To format the date in your View, you may should Drupal format_date function with 'dateonly' passed as the type of format to use (second argument). To output this in your view you may have the following in your view-view-field--fieldname.tpl.php:

$nicedate = format_date($output, 'dateonly');
print $nicedate;

IMHO, there is a big advantage in first defining a standard type of date format and giving it a name. This ensures that dates are displayed consistently throughot the site.

However, if you can't be bothered to first define the dateonly type of format, you can use custom as your second argument to format_date and then, as your third argument, pass a string that can be passed on to the PHP date function:

$nicedate = format_date($output, 'custom', 'M jS, Y');
print $nicedate;

You can override the template for that field as you mentioned, and use the date function to output a timestamp with the "Feb 20th, 2013" format:

print date("M jS, Y", $timestamp);
  • How would I wrap the views field output (<?php print $output; ?>) in that?
    – joe
    Jan 24 '13 at 7:19
  • In your view-view-field--fieldname.tpl.php file the $output variable should hold the timestamp, if so you can just pass $output to the date function: <?php print date("M jS, Y", $output); ?>
    – penten
    Jan 24 '13 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.