unplugged_web Posted July 24, 2014 Share Posted July 24, 2014 I'm trying to get estimated delivery dates for various products but rather than working it out for each product it adds the previous delivery time onto the next product before working it out. This is what I've got so far public function getDeliveryDate() { $ndd = $this->getPhysicalOrderProducts(); $arrive = $this->getPostDate(); $arrive->add(new DateInterval('P'.$ndd[0]->getDeliveryTime().'D')); return $arrive; } I want it get a product, add the DeliveryTime to PostDate then return that as $date I then want it to go on to the next product and do the same thing. At the moment what's happening is it's getting the date, adding the delivery time. Then with the next date it's adding the previous delivery time to the result of $date from the previous product. Is there anyway to get it to recalculate it from fresh for every product? Link to comment Share on other sites More sharing options...
thescientist Posted July 24, 2014 Share Posted July 24, 2014 it would help to show that code that is actually calling this function. Likely you are using one data variable and it keeps getting overwritten in each iteration of your loop. That's just a guess though. Link to comment Share on other sites More sharing options...
unplugged_web Posted July 25, 2014 Author Share Posted July 25, 2014 it would help to show that code that is actually calling this function. Likely you are using one data variable and it keeps getting overwritten in each iteration of your loop. That's just a guess though. It's being called from a twig file (I'm using Silex) but the code is: {% for op in products %} <li>{{ entry.getDeliveryDate|date("d/m/Y") }}</li> {% endfor %} Link to comment Share on other sites More sharing options...
thescientist Posted July 28, 2014 Share Posted July 28, 2014 what is the output of this? all the products? $ndd = $this->getPhysicalOrderProducts(); you are always referencing it from the first index everytime you try and get a date. $arrive->add(new DateInterval('P'.$ndd[0]->getDeliveryTime().'D')); Link to comment Share on other sites More sharing options...
unplugged_web Posted July 29, 2014 Author Share Posted July 29, 2014 Thank you, I added this to the code and now it works: public function getDeliveryDates() { $ops = $this->getPhysicalOrderProducts(); $dates = array(); foreach ($ops as $op) { $date = clone $this->getPostDate(); $date->add(new DateInterval('P'.$op->getDeliveryTime().'M')); $dates[] = $date; } return $dates; } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now