(first posted: Nov 27, 2006)
(1580 Reads)
keywords: date php time
Permalink
Reformatting Date and Time
Some templates do have have the "luxury" of receiving a Unix timestamp for the date/time of an item (such as article date or calendar item). Rather you are given a preformatted string. This snippet converts the preformatted string to a timestamp using strtotime() so you can reformat it as needed.
For formatting options see http://us3.php.net/date
Examples:
From julian/blocks/calevent.xt
<xar:comment> convert mm-dd-yyyy to yyyy-mm-dd and reformat using date( strtotime() ) </xar:comment>
<xar:set name="d">date( 'Y-m-d', strtotime(preg_replace("/^\s*([0-9]{1,2})[-]+([0-9]{1,2})[-]+([0-9]{1,4})/", "\\3-\\1-\\2", $val['startdate'])) )</xar:set>
xar:comment> format time to hh:mmam (eg 01:12pm) 12-hour time </xar:comment>
<xar:set name="t">date( 'g:ia', strtotime(preg_replace("/^\s*([0-9]{1,2})[-]+([0-9]{1,2})[-]+([0-9]{1,4})/", "\\3-\\1-\\2", $val2['time'])) )</xar:set>
From articles/blocks/topitems.xt
<xar:comment> convert mm-dd-yy to Day, Month dd (eg. Monday, June 12</xar:comment>
<xar:set name="d">date( 'l, F j', strtotime(preg_replace("/^\s*([0-9]{1,2})[-]+([0-9]{1,2})[-]+([0-9]{1,4})/", "\\3-\\1-\\2", $item['value')) )</xar:set>
There are no comments attached to this item.



