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 ?
Thank you.
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 communityIn Magento2 - Replaced Zend_Date
with native PHP DateTime
object/functions.
So you can use PHP DateTime function in Magento-2
You can get current date by below code.
For Example.
$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")))));
\DataTime
over \Zend_Date
for perfomance reasons. And, if you need something in M2, it's performance.
Apr 16, 2016 at 8:57
You can also import Zend_Date
class at the top of your class file:
use Zend_Date;
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