at following path i'm trying to change date format /app/code/Magento/Sales/view/frontend/templates/order/history.phtml https://imgur.com/jOQNshk
from 3/6/22 to 03/06/2022 How can i edit following code?
formatDate($_order->getCreatedAt()) ?>Thanks
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityat following path i'm trying to change date format /app/code/Magento/Sales/view/frontend/templates/order/history.phtml https://imgur.com/jOQNshk
from 3/6/22 to 03/06/2022 How can i edit following code?
formatDate($_order->getCreatedAt()) ?>Thanks
As quick solution you can create helper in your module
<?php
declare(strict_types=1);
namespace Acme\StackExchange\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
class Date extends AbstractHelper
{
/**
* @var TimezoneInterface
*/
protected $timezone;
/**
* @var string
*/
protected $locale;
public function __construct(Context $context, TimezoneInterface $timezone, ResolverInterface $localeResolver)
{
parent::__construct($context);
$this->timezone = $timezone;
$this->locale = $localeResolver->getLocale();
}
public function formatDate(string $value, string $format): string
{
$convertedDate = $this->timezone->date(
new \DateTime($value, new \DateTimeZone('UTC')),
$this->locale,
true
);
return $convertedDate->format($format);
}
}
and use this helper in template
// define it in the top
$_dateHelper = $this->helper(\Acme\StackExchange\Helper\Date::class);
// use
echo $_dateHelper->formatDate($_order->getCreatedAt(), 'm/d/Y');
<?= $_dateHelper->formatDate($_order->getCreatedAt(), 'm/d/Y'); ?>
. <?=
equal <?php echo
Jun 4 at 11:27
MakeDo\DateTime
and make sure you've specified correct namespace and class name for helper
Jun 4 at 19:01