4

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.

3 Answers 3

7

In 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

1
  • If answer help for you then please accept this answer for helpful other cummunity users. Apr 15, 2016 at 10:45
2

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")))));

1
  • That's valid code, but even ZF1 maintainers themselves suggest to favour PHP's own \DataTime over \Zend_Date for perfomance reasons. And, if you need something in M2, it's performance.
    – nevvermind
    Apr 16, 2016 at 8:57
2

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

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.