I wrote myself a little snippet to do this, as it comes up so often:
<?php
/*
Name: DateFormatter
Description: <strong>0.1</strong> Outputs the formatted version of the passed in UnixTimeStamp, according to the format string passed in FormatString
Version: 0.2
Author: Duncan Lock <[email protected]>
Parameters:
$FormatString PHP format string appropriate for using with the date function (http://www.php.net/date)
$DateTime Unixtimestamp or a date. If it's is_numeric, we assume it's a unix timestamp.
eg:
[[DateFormatter? &formatString=`l dS \of F Y h:i:s A` &dateTime=`[*pub_date*]` ]]
Returns something like: 'Monday 15th of August 2005 03:12:46 PM'
[[DateFormatter? &formatString=`d/m/Y` &dateTime=`1134086400` ]]
Returns something like: '09/12/2005'
*/
$formatString = isset($formatString)? $formatString: 'l jS \of F Y h:i:s A';
# Convert $dateTime into a unix timestamp, if it isn't already
$unixTimeStamp = is_numeric($dateTime)? $dateTime: strtotime($dateTime);
if (trim($unixTimeStamp) == ''):
# empty timestamp, bail
return '';
else:
# return the formatted datetime
return date ($formatString, $unixTimeStamp);
endif;
?>
As previously mentioned, you can use PHX for this, if you’re using it - and I’m sure there’s stuff in the repository that does this too. I think that I originally wrote this because I couldn’t find one that used date() - which allows you to output stuff that looks like this: ’Monday 15th of August 2005 03:12:46 PM’ - using st/th/rd for days - something you can get out of strftime(), afaik. Hope else someone finds it useful.