Jump to content

Changing date format


Recommended Posts

#SOLVED#

To set date to DD-MM-YY across the whole site


  1. Edit /classes/Tools.php

  2. Search (Ctrl+F) for → if ($language AND strtolower($language['iso_code'])

  3. On the same line that is now highlighted, change fr to uk

  4. 3 lines below that, replace the existing line with → return ($tmpTab[2].'-'.$tmpTab[1].'-'.$tmpTab[0].($full ? $hour : ''));

Link to comment
Share on other sites

  • 2 months later...

What happens when I upgrade PS with a new release? Will the classes/tools.php file be overwritten by the upgrade procedure?

 

It should be a common custom not to touch any of the core files in PS.

 

Isn't there an alternative way to format dates across the web site? This matter is fundamental for PDF invoices, which often sport date format unsuitable for european customers.

Link to comment
Share on other sites

What happens when I upgrade PS with a new release? Will the classes/tools.php file be overwritten by the upgrade procedure?

 

It should be a common custom not to touch any of the core files in PS.

 

Isn't there an alternative way to format dates across the web site? This matter is fundamental for PDF invoices, which often sport date format unsuitable for european customers.

 

The correct way is to override the class file in the override directory, and only override the function you need to change. This way in the future if the class changes, you still have your customization in the override folder, and only impact the single function you override, and not the entire class.

 

Note: This is not fool proof, since future versions of prestashop may change the functionality you override to the point it needs to be redone. The date format is a good example. In prior versions, the date format was performed within Tools.php. However now (v1.4.6.2) the date format is now defined within the Language definition in the back office.

 

If you want to change the date format for the entire site in v1.4.6.2, just go into the back office, and update the Language date format.

 

However in previous versions, let's say v1.4.4.1, you would do it like this...

 

1) Create a file called Tools.php in the override/classes folder, and add the following content

class Tools extends ToolsCore
{

}

 

2) Now add the function that you want to change to this class file. To change the date format for the entire site you would override the displayDate function.

 

	
public static function displayDate($date, $id_lang, $full = false, $separator = '-')
{
	 if (!$date OR !strtotime($date))
		 return $date;
	if (!Validate::isDate($date) OR !Validate::isBool($full))
		die (self::displayError('Invalid date'));
	 $tmpTab = explode($separator, substr($date, 0, 10));
	 $hour = ' '.substr($date, -8);

	$language = Language::getLanguage((int)($id_lang));
	 if ($language AND strtolower($language['iso_code']) == 'fr')
		 return ($tmpTab[2].'-'.$tmpTab[1].'-'.$tmpTab[0].($full ? $hour : ''));
	 else
		 return ($tmpTab[0].'-'.$tmpTab[1].'-'.$tmpTab[2].($full ? $hour : ''));
}

 

3) Now change the logic so it does what you want it to. You have successfully customized the logic without modifying the core file.

 

If you only want to impact the invoice, following the same steps above, but instead override the PDF.php class file. This is a bit more complicated, because you have to override every single function that uses displayDate, but it is less intrusive and will avoid issues that you are running into with the cart. I suspect the cart made some assumption about the date format, without using the Tools.php class.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...