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.
First you should define a suitable format- Do this in the administrative GUI:
- Navigate to: Administration » Configuration » Regional and language » Date and Time
- Click the tab "Formats" and click on link "Add format".
- Create a new format. You use the same string as you would pass to the PHP
M jS, Y. Save it.
- Click the tab "Types". Click on link "Add date type".
- 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
<?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
<?php $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);