4

I need to save to db current date & "current date + 7days" values. For this I'm using following function. But how to add 7 days to this.

 Mage::getModel('core/date')->date('Y-m-d H:i:s');

Thanks in advance.

Update:

$res = Mage::getModel('rad_inventorypieces/reserve');
$date = new Zend_Date(Mage::getModel('core/date')->timestamp());
$res->setCreatedDate($date);
$date->addDay('7');
$res->setExpiryDate($date);

4 Answers 4

11

In OOP style:

$date = new Zend_Date(Mage::getModel('core/date')->timestamp());
$date->addDay('7');
$date->toString('YYYY-MM-dd HH:mm:ss'); //Returns representation of date 
8
  • its giving result fine when echoing..but when i saving to db its saving like this 2014-11-07 02:58:41
    – sivakumar
    Oct 24, 2014 at 10:00
  • when echo..Oct 24, 2014 2:48:44 AM,Oct 31, 2014 2:48:44 AM fine.
    – sivakumar
    Oct 24, 2014 at 10:04
  • could post the lines please?
    – Detzler
    Oct 24, 2014 at 10:08
  • Yep go with this one, cleaner solution than mine. Oct 24, 2014 at 10:10
  • 1
    it must be setCreatedDate($date->toString('Y-M-d H:m:s'))
    – Detzler
    Oct 24, 2014 at 10:25
1

Try this :

$date = Mage::getModel('core/date')->date('Y-m-d h:i:s');
echo $date . '<br>';

$nextDate =  Mage::getModel('core/date')->date('Y-m-d h:i:s', strtotime($date." +7 days"));

echo $nextDate . '<br>';

$res=Mage::getModel('rad_inventorypieces/reserve');
$res->setExpiryDate($nextDate);
0

You can use the underlying Zend methods:

$now = new Zend_Date(Mage::getModel('core/date')->timestamp(), Zend_Date::TIMESTAMP);
$now->set(strtotime('+7 days', $now->toString(Zend_Date::TIMESTAMP)), Zend_Date::TIMESTAMP);
$now = $estimate->toString(Zend_Date::TIMESTAMP);

This will give you an epoch time output of 7 days from the time the code runs, you can use this in date() or wherever you need to.

3
  • Call to a member function toString() on a non-object
    – sivakumar
    Oct 24, 2014 at 9:45
  • I can assure you the above works, where exactly are you running the code? Oct 24, 2014 at 10:01
  • in observer file.place order after event
    – sivakumar
    Oct 24, 2014 at 10:20
0

here's mine

$Date = Mage::getModel('core/date')->date('Y-m-d H:i:s');
echo date('Y-m-d H:i:s', strtotime($Date. ' + 1 days'));
echo date('Y-m-d H:i:s', strtotime($Date. ' + 7 days'));

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.