How to format dates and times in Drupal

in

A couple of useful Drupal functions for formatting date and time. format_interval

$age = format_interval(time() - $node->created,1) . t(' ago');

The above outputs:

12 weeks ago

You second argument controls how many fields are outputted. If you passed "3" as the granularity option, then the output might look like:

12 weeks 4 days 16 hours ago

format_date

$date = format_date($node->created,'large');

The second argument can be one of: small, medium, large, custom. The third argument specifies a format for use with the "custom" option. Here's a few handy PHP date format strings:

OutputCode
2007-01-31Y-m-d
Wednesday 31 January 2007D d M Y
06:30 pmh:i a
18:30:59H:i:s

 

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • No HTML tags allowed

More information about formatting options

luigi