Magento 2: format var invoice.created_at
-
18-01-2021 - |
Question
I'm using EaDesgin/magento2-pdf-generator2 to format the customer's invoice. This works nice. But in this template I use the magento 2 variable {{var invoice.created_at}}, what gives me the ugly format 'yyyy-mm-dd h:i:s'
.
I want to reformat this date.
Maybe I could add a custom variable (not with backend, there I cannot use PHP functions), or a filter to invoice.created_at
. Where can I do that?
Solution
I solved the problem by adding a new function to Model/Order/Invoice.php:
public function getInvoiceDate(){
$cdate = $this->getCreatedAt();
$invoiceDate = date('d. M. Y', strtotime($cdate));
return $invoiceDate;
}
Then you can use {{var invoice.invoice_date}} to display the formatted date.
Tested with Magento 2.2.3 and EADesign Invoice PDF Generator 1.2.2
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange