3
votes

My version is 1.8.1.0 (Community Edition).

I have a problem with the date format in magento frontend especially in the advanced search. I have add a custom attribute "event_date" with input type "Date".

Under Configuration->General I've set the locale to "France" and under Configuration->Catalog->Date & Time Custom Options I've set the Date Fields Order to Day/Month/Year.

But when I select a date in frontend with the datepicker in advanced search it adds it in US format. (Month/Day/Year)

What's really strange: The search only works with US format but the search validator expects the date format in d/m/y. I found out that the date format is hardcoded in "app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php". If I change it in this file the calendar and validator work in the right format but I get no results because the search still needs the US format.

So what's the best way to force Magento to work in a specified date format?

UPDATE:

I also found out, if I set the browser prefered language to en_US the validation works with US (Month/Day/Year) format. So Magento seems to check the client locale and set the date validation to it, but not the date format from calendar or search itself.

UPDATE 2:

After I got no response I posted the same question but with a different description under https://magento.stackexchange.com/questions/16257/date-format-in-advanced-search.

Please have a look there. Finally I could solve 2 of the 3 problems so that my advanced search now runs with en_US date format. The last problem is: Where in code the db query is build for the advanced search and how can I switch to another date format? It seems like en_US date format is hardcoded.

2

2 Answers

2
votes

I have had similar issue with Date filter :)

The way date time instantiated in the form like that

Step 1:-

In Block Form class Mage_CatalogSearch_Block_Advanced_Form

public function getDateInput($attribute, $part = 'from')
{
    $name = $attribute->getAttributeCode() . '[' . $part . ']';
    $value = $this->getAttributeValue($attribute, $part);

    return $this->_getDateBlock()
        ->setName($name)
        ->setId($attribute->getAttributeCode() . ($part == 'from' ? '' : '_' . $part))
        ->setTitle($this->getAttributeLabel($attribute))
        ->setValue($value)
        ->setImage($this->getSkinUrl('images/calendar.gif'))
        ->setFormat('%m/%d/%y') //So you need change the Format here !!!!!
        ->setClass('input-text')
        ->getHtml();
}

So you need to change the Date format for the calendar in the line ->setFormat('%m/%d/%y')

! Take look about the supported datetime format here http://www.dynarch.com/jscal/#sec34

Then its used in the method below to generate the block

$block = $this->getLayout()->createBlock('core/html_date');
$this->setData('_select_block', $block);

and in that block class Mage_Core_Block_Html_Date

Line 40: $displayFormat = Varien_Date::convertZendToStrFtime($this->getFormat(), true, (bool)$this->getTime());
.......
Line53: ifFormat    : "' . $displayFormat . '",

Step 2:-

In the advanced search collection class Mage_CatalogSearch_Model_Resource_Advanced_Collection

You need to modify this method to allow the new locale or the new datatime format you needed !

Line43: public function addFieldsToFilter($fields) {
.......
.......
.......
.......
Line96: if (!Zend_Date::isDate($conditionValue['from'])) {
.......
Line109:if (!Zend_Date::isDate($conditionValue['to'])) {
.......
}

Now You need to modify those 2 lines with the new datetime format you change the search block to allow this method to be validated

For instance i modified the Javascript calendar format to be %d-%m-%Y which will produce Date in the calendar like ( 25-12-2014 )

Then i modified The method in the above collection class to be like this

if (!Zend_Date::isDate($conditionValue['from'], 'd-m-Y' )) { // I added the format for the validation 
.......
.......
if (!Zend_Date::isDate($conditionValue['to'], 'd-m-Y' )) { // same as above one 

step 3:-

Change those value of date inputs in the class Mage_CatalogSearch_Model_Resource_Advanced_Collection to whatever format or locale.

Everything works fine with that.

I have made small module that rewrite the 2 classes you need to modify based on the locale you need

Check it out https://github.com/Meabed/magento-advanced-search-datetime-field

Regards !

0
votes

This issue struck us before.. and this blog post from Inchoo helped us a lot.

http://inchoo.net/ecommerce/magento/magento-date-format/

Also, you can look into getting js/calendar.js fixed or completely replaced (I recommend the latter suggestion). There's alot of FIXME /todo items there that are related to locale and you might want a more modern replacement. Here is a very good and well-made one that we've used for some of our clients: https://github.com/ChiperSoft/Kalendae

validation.js for Date however already validates it for the current browser locale as expected. But it can't hurt either to replace it with something like Abide from Zurb.. however this also entails a lot more work in replacing your DOM as it is not a direct replacement.