How to format dates and times in Drupal
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:
| Output | Code |
|---|---|
| 2007-01-31 | Y-m-d |
| Wednesday 31 January 2007 | D d M Y |
| 06:30 pm | h:i a |
| 18:30:59 | H:i:s |
Post new comment