Hello,
This is not really a bug, more an improvement, but as there is no forum category for improvements or request (maybe an idea to add one?), I decided to put it here.
This improvement is only described here for the grid.datetime.php class, but I expect the same improvements can be made to the other date fields.
For my project I wanted to have the English ordinal suffix for the day of the month and at that point I noticed it was not directly possible. When looking at the code of the grid.datetime.php class I noticed the 'toFormat()' method of the JDate object is used. This uses the format as used in PHP's strftime() function, which does not support this ordinal suffix, whilst the PHP Date() function does support this.
As the toFormat() method will possibly deprecated in the future (by Joomla) and they encourage you to use the format() method (which does uses the Date() format), I decided to fix it as follows:
In my component parameters I created a new field, in my case called: date_format_method, which only has to options: either 'new' for using the Date() format, and consequently will call the format() method to format the date. The other is 'old' which uses the strftime() format, and calls the toFormat() method.
Additionally (which is not really relevant to this issue, but I think it is a nice improvement), I added a another field for 'date format', which specifies the default format for a date (when it is not specified in the call to the DOM element).
The code for the modified grid.datetime.php is below:
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class JDomHtmlGridDatetime extends JDomHtmlGrid
{
var $level = 3; //Namespace position
var $last = true; //This class is last call
var $format;
var $format_method = "new";
/*
* Constuctor
* @namespace : requested class
* @options : Configuration
* @dataKey : database field name
* @dataObject : complete object row (stdClass or Array)
* @dataValue : value default = dataObject->dataKey
* @num : Num position in list
*
* @format : Date format - default='%Y-%m-%d'
*
*/
function __construct($args)
{
parent::__construct($args);
$config = JComponentHelper::getParams('com_slm');
// get the date formatting method
$this->format_method = $config->get("date_format_method","new");
// get the default date format
$default_format = $config->get("date_format");
if ($default_format=="") {
// just put some format if not specified yet
if ($this->format_method=="new") {
$default_format = 'Y-m-d H:i';
} else {
$default_format = '%Y-%m-%d %H:%M';
}
}
$this->arg('dateFormat' , 6, $args, $default_format);
}
function build()
{
$formatedDate = "";
if ($this->dataValue
&& ($this->dataValue != "0000-00-00")
&& ($this->dataValue != "00:00:00")
&& ($this->dataValue != "0000-00-00 00:00:00"))
{
jimport("joomla.utilities.date");
$date = new JDate($this->dataValue);
// Use the correct formatting method according to the component's setting
if ($this->format_method=="old") {
$formatedDate = $date->toFormat($this->dateFormat);
} else {
$formatedDate = $date->format($this->dateFormat);
}
}
$this->addClass('grid-date');
$html = '<span <%STYLE%><%CLASS%><%SELECTORS%>>'
. $formatedDate
. '</span>';
return $html;
}
}
Hopefully this will help other people as well, especially when they (like me) want to use the English ordinal suffix in date formats.
Kind regards,
Misha