lost password?

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

rss
Tag this page
   

» Blogs that link here
Jul 10, 2006
(1241 Reads)
keywords: date articles
Permalink

Date Shorthands

A method of displaying date information on articles with less granularity.

We wanted a way to specify dates with less granularity on articles. For example, instead of the full date like "July 11, 2006", sometimes a document just has a month, like "July, 2006", or even just a year "2006". We never need to show the time-of-day of the publication.

The technique I chose is to specify the Article Pulication Date as follows:

  • If date is January 1, and the time of day is 00:00:00, then only show the year ("1/1/2006 00:00:00" displays as "2006").
  • If date is the first of the month, and the time of day is 00:00:00 then don't show the day of the month ("7/1/2006 00:00:00" displays as "July, 2006").
  • If the date is the first of the month, but any other time, then show it as the first ("7/1/2006 10:21:00" displays as "July 1, 2006").

Any other date shows as "month day, year".

Here's the template code that goes into the pubtype summary and display templates:

 

<xar:if condition="date('j',$pubdate) eq '1' and date('H',$pubdate) eq '00'">
    <xar:if condition="date('n',$pubdate) eq '1'">
        #xarLocaleFormatDate('%Y', $pubdate)#
    <xar:else />
        #xarLocaleFormatDate('%B, %Y', $pubdate)#
    </xar:if>
<xar:else />
     #xarLocaleFormatDate('%B %e, %Y', $pubdate)#
</xar:if>

So, if you want just July, 2006 to display, then enter the Pubication Date of July 1, 2006 00:00:00. And if you want just "2006" to display, enter the Publication Date of January 1, 2006 00:00:00.

Well, that worked just great, for a while. Then I upgraded to a newer version of Xaraya, and moved my site to a new server. The new server was set to a different time zone. And it is shared so I could not change the zone. Also, I think the new Xaraya version changed the way dates and locale are GMT is stored or calculated. Anyway, after a couple hours of tinkering with the Xaraya Base Config time zone, using gmdate() instead of date(), and some other futzing around, I decided it's enough to ignore the time, and check if the minutes:seconds are 00:00 instead. So, it becomes

 

<xar:if condition="date('j',$pubdate) eq '1' and date('i',$pubdate) eq '00' and date('s',$pubdate) eq '00'">
<xar:if condition="date('n',$pubdate) eq '1'">
#xarLocaleFormatDate('%Y', $pubdate)#
<xar:else />
#xarLocaleFormatDate('%B, %Y', $pubdate)#
</xar:if>
<xar:else />
#xarLocaleFormatDate('%B %e, %Y', $pubdate)#
</xar:if>

Then I put it all into a php function under my "misctools" utility module, so it becomes instead,

 

<xar:set name="datestr">xarModApiFunc('misctools','user','shortdate',array('date'=>$pubdate))</xar:set>
#$datestr#

And added the file modules/misctools/xaruserapi/shortdate.php :

<?php
/**
* return date string Month D, YYYY
* but if minutes and seconds are zero then
* and its the first of the month, just return Month, YYYY
* or if its January 1, just return YYYY
* @param $args['date'] date (aka articles $pubdate)
returns formatted date string
*/
function misctools_userapi_shortdate($args) {
extract($args);
if (!isset($date)) {
$msg = xarML('Invalid #(1) for #(2) function #(3)() in module #(4)',
'string', 'user', 'shortdate', 'misctools');
xarErrorSet(XAR_USER_EXCEPTION, 'BAD_PARAM',
new SystemException($msg));
return;
}

if (gmdate('j',$date) == '1' && gmdate('i',$date) == '00' && gmdate('s',$date) == '00') {
if (date('n',$date) == '1')
$s = xarLocaleFormatDate('%Y', $date);
else
$s = xarLocaleFormatDate('%B, %Y', $date);
}
else {
$s = xarLocaleFormatDate('%B %e, %Y', $date);
}
return $s;
}
?>

 

 

 

 

 

There are no comments attached to this item.

Post a new comment

How many days in a week?

Name :