1

I am getting date by injecting this class in constructor \Magento\Framework\Stdlib\DateTime\DateTime as:

protected $date;
public function __construct(
....
\Magento\Framework\Stdlib\DateTime\DateTime $date,
....
) {
....
$this->date = $date;
....
}

and then, in class this:

$date = $this->date->gmtDate();

But how can I add or subtract month to this date for Expiration feature ?

3 Answers 3

1

You should try :

$date = $this->date->gmtDate('Y-m-d\TH:i:s\Z', strtotime('+1 month'));

0

Try this way

//Our dates
$date1 = "2013-03-01 19:12:45";
$date2 = "2014-03-01 06:37:04";

//Convert them to timestamps.
$date1Timestamp = strtotime($date1);
$date2Timestamp = strtotime($date2);

//Calculate the difference.
$difference = $date2Timestamp - $date1Timestamp;

echo $difference;
0

Try This

protected $_date;

public function __construct(
    ..........................................
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date,
    ..........................................
) {
    ..........................................
    $this->_date = $date;
    ..........................................
}

Difference Between Two Dates

public function getDifference()
{
    /* Check Days after Cart Update */
    $currentDate = date_create($this->_date->date()->format('Y-m-d H:i:s'));
    $updateData = date_create($this->_date->date("2021-03-05 06:30:00")->format('Y-m-d H:i:s'));
    $interval = date_diff($updateData, $currentDate);
    $diffDate = (int) $interval->days;
    echo "Date Difference is :".$diffDate;
    return $diffDate;
}

Add Minutes, Hours, Day, Week, Month, Year in Date

public function createSendDateTime($whenSendMail, $sendDateTime, $cartDate)
{
    $whenSendMail = "after_minutes"; // value like after_minutes, after_hours, after_day, after_week, after_month. after_year
    $sendDateTime = "2021-03-05 06:30:00";
    $cartDate = "2021-04-05 06:30:00";

    $time = new \DateTime($cartDate);
    $updatedCartDate = '';
    switch ($whenSendMail) {
        case "after_minutes":
            $time->add(new \DateInterval('PT' . $sendDateTime . 'M'));
            $updatedCartDate = $time->format('Y-m-d H:i:s');
            break;
        case "after_hours":
            $time->add(new \DateInterval('PT' . $sendDateTime . 'H'));
            $updatedCartDate = $time->format('Y-m-d H:i:s');
            break;
        case "after_day":
            $time->add(new \DateInterval('P' . $sendDateTime . 'D'));
            $updatedCartDate = $time->format('Y-m-d H:i:s');
            break;
        case "after_week":
            $time->add(new \DateInterval('P' . $sendDateTime . 'W'));
            $updatedCartDate = $time->format('Y-m-d H:i:s');
            break;
        case "after_month":
            $time->add(new \DateInterval('P' . $sendDateTime . 'M'));
            $updatedCartDate = $time->format('Y-m-d H:i:s');
            break;
        case "after_year":
            $time->add(new \DateInterval('P' . $sendDateTime . 'Y'));
            $updatedCartDate = $time->format('Y-m-d H:i:s');
            break;
        default:
            $updatedCartDate = "";
    }
    echo "<br/>";
    echo "Updated Date ".$updatedCartDate;
    return $updatedCartDate;
}

Your Answer

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

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