In Magento 1 we are using zend_date in a following manner
new Zend_Date(date, 'MM-dd-Y'); or new Zend_Date(date('M-01-Y', strtotime("+1 months", strtotime(date("Y-m-d")))));
But how can I use above method in Magento 2 ?
In Magento2 - Replaced
Zend_Date with native PHP
So you can use PHP DateTime function in Magento-2
You can get current date by below code.
$currentDate = (new \DateTime()); echo $currentDate->format('Y-m-d');
You can get more help from : http://php.net/manual/en/ref.datetime.php
You can do it following way.
new \Zend_Date($date, 'MM-dd-Y'); or new \Zend_Date(date('M-01-Y', strtotime("+1 months", strtotime(date("Y-m-d")))));
You can also import
Zend_Date class at the top of your class file:
Then, your code will work as normal:
$dateObj = new Zend_Date($date, 'MM-dd-Y');
OR, you can directly instantiate the object of the class. For this, you don't need to import the class but have to use "backslash" infront of the
Zend_Date class name.
$dateObj = new \Zend_Date($date, 'MM-dd-Y');
Reference: PHP Documentation on Using namespaces: Aliasing/Importing