lost password?

home
•  xaraya +
•  rails
•  django
•  webdev
•  xamp
•  musings

rss
Tag this page
   

ยป Blogs that link here
last modified: Nov 27, 2006
(first posted: Nov 27, 2006)
(1580 Reads)
keywords: date php time
Permalink

Reformatting Date and Time

Here's a useful snippet for reformatting date and time strings when you only have a pre-formatted string and don't have the Unix timestamp.

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.

Post a new comment

How many days in a week?

Name :