Complete and flexible blogging platform for ProcessWire

Format Date

The method formatDate() is a small utility method that helps format your Blog's Post's date according to the specification you want (e.g. MM-dd-YY, YY-MM-dd, DD-MM-YYYY HH:mm, etc). Note that in PHP a date can include time as well.

<?php
 /**
 * Return a date formatted as specified in the Blog post's 'blog_date' field.
 *
 * @access public
 * @param int|string $date If given a timestamp, it will be automatically formatted according to the 'blog_date' field
 * If given a string, then whatever format it is in will be kept. 
 * @return $date string
 *
 */
 public function formatDate($date)

The method accepts one argument, $date. This can either be an integer or a string. If given a timestamp, it will automaticlaly format this in line with what has been specified in the Post's 'blog_date' field. If given a string, then whatever format that is in will be preserved. The method does not output any markup.

The method is straightforward to use as shown below. In the first example, we pass the argument $date as a string.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

echo $blog->formatDate('22 January 2014');

This above will output '22 January 2014'. In the following example we pass $date as a timestamp.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

echo $blog->formatDate(1409156970);

The above will output '27 August 2014 5:29 pm'. In these final two examples, we pass $date using ProcessWire API.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');
//grab a single post and output its formatted post date
echo $blog->formatDate($pages->get('template=blog-post, title=Imagine')->blog_date);

Here we grab a couple of posts and loop through them outputting their dates.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');
$posts = $pages->find('template=blog-post, limit=10, sort=-blog_date');

$out = '';

$out .='<ol>';
foreach ($posts as $post) {
  $out .= '<li>' . $blog->formatDate($post->blog_date) . '</li>';
}

$out .='</ol>';

echo $out;

Summary

In this short lesson we have seen how to use the method formatDate($date) to return a date formatted to our specification.