0

I'm trying to filter the data collection from 4 hours to now and I have the below code but its not working.

public function getWishlistProduct($wishlistid){

$to = date("Y-m-d h:i:s");
$from = strtotime('-4 hours', strtotime($to));
$from = date('Y-m-d h:i:s', $from); 

$wishlist= $this->getCollection()->addFieldToFilter('wishlist_id', ['eq'=>$wishlistid])
                                    ->addFieldToFilter('added_at', array('from'=>$from, 'to'=>$to));
return $wishlist;
      
}

Could anyone please help?

1
  • Its showing HTTP ERROR 500
    – MagentoDev
    Jun 18, 2020 at 16:42

1 Answer 1

0

I have solved this issue by using the time() function and the updated code as below. Hope it may helpful to someone:

public function getWishlistProduct($wishlistid){

    $time = time();
    $to = date('Y-m-d H:i:s', $time);
    $lastTime = $time - 14400; //60*60*4
    $from = date('Y-m-d H:i:s', $lastTime);

    $wishlist= $this->getCollection()->addFieldToFilter('wishlist_id', ['eq'=>$wishlistid])
                                        ->addFieldToFilter('added_at', array('from'=>$from, 'to'=>$to));
    return $wishlist;
          
    }
2
  • 1
    sound good , Or above my code why showing 500 error because i am missed some column(,) and i am not sure $from = date('Y-m-d H:i:s', strtotime('-4 hour')); $toDate = date('Y-m-d H:i:s',$from); $wishlist= $this->getCollection()->addFieldToFilter('wishlist_id', ['eq'=>$wishlistid]) ->addFieldToFilter('added_at', array('from'=>$from, 'to'=>$toDate,'date'=>true)); return $wishlist; Jun 18, 2020 at 17:18
  • @MohitPatel, Can you please check this magento.stackexchange.com/questions/315373/… and help me out.
    – MagentoDev
    Jun 19, 2020 at 2:24

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.