1

I'm attempting to debug a Timezone issue in a custom Magento module. One of the tests I would like to try is to ADD/REMOVE 1hr from whatever is output from this line of code.

I suspect ADDING 1hr could be as simple as implementing a "+1" somehow. And perhaps REMOVING 1hr might involve going back 1 Day and forward 23 hours. But honestly, I don't know what to start with this.

Line

$estimatedDeliveryDate = $this->localeDate->date()->setTime(0,0,0);

Context

class Estimator
{
    private $localeDate;

    public function __construct(
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
    ) {
        $this->localeDate = $localeDate;
    }

    public function addEstimatedDeliveryDate(&$items)
    {
        $now = $this->localeDate->date();
        $todayDate = $this->localeDate->date();

         foreach ($items as &$item) {
            $estimatedDeliveryDate = $this->localeDate->date()->setTime(0,0,0);

...
9
  • this setTime(Hours,Minutes,Seconds); , what do you want to do?
    – fmsthird
    Mar 29, 2019 at 16:52
  • With Daylight Savings on Sunday, my Delivery Date selector seems to break when trying to choose a date during that period of time. It appears the code was never written to take Daylight into account. So, to test and debug I wanted to see what would happen if I adjusted the code +/- 1hr to see what effect that would have.
    – Craig
    Mar 29, 2019 at 16:59
  • setTime(Hours,Minutes,Seconds); might work for +1 but not -1.
    – Craig
    Mar 29, 2019 at 17:00
  • since it is for testing purpose only, you can try like this $estimatedDeliveryDate = $this->localeDate->date('2019-03-30')->setTime(0,0,0)->format('d-m-Y H:i:s');
    – fmsthird
    Mar 29, 2019 at 17:12
  • 1
    Gotcha. I'll try that out in a bit, cheers
    – Craig
    Mar 29, 2019 at 17:33

1 Answer 1

0

With the help of code below, I' able to add number of days/hours to current date and time.

<?php

namespace Namespace\Modulename\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

class Data extends AbstractHelper
{
    /**
     * @var TimezoneInterface
     */
    protected $_timezoneInterface;

    /**
     * Data constructor.
     * @param Context $context
     * @param TimezoneInterface $timezoneInterface
     */
    public function __construct(
        Context $context,
        TimezoneInterface $timezoneInterface
    )
    {
        parent::__construct($context);
        $this->_timezoneInterface = $timezoneInterface;
    }


    /**
     * @return string
     */
    public function getOrderScheduledAt(): string
    {
        $x = '2'; // Number of days to be added (same way you can use 'hours')
        $currentDateTime = $this->_timezoneInterface->date()->format('d-m-Y H:i:s');
        return $this->_timezoneInterface->date(strtotime($currentDateTime." +{$x} days"))->format('d-m-Y H:i:s');
    }

}

In the same way, you can use $this->_timezoneInterface->date(strtotime($currentDateTime." +1 days"))->format('d-m-Y H:i:s'); to add one hour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.